Clean up lint (#56)

The only real change in this commit is that MaxInt is made private.
Everything else should be gofmt'ing, docs and cleanup of lint.
This commit is contained in:
Cameron Moore
2016-04-18 09:58:23 -05:00
committed by Thomas Pelletier
parent 9d93af61de
commit 6e26017b00
7 changed files with 32 additions and 29 deletions
+1 -1
View File
@@ -299,7 +299,7 @@ func (l *tomlLexer) lexKey() tomlLexStateFn {
func (l *tomlLexer) lexComment() tomlLexStateFn { func (l *tomlLexer) lexComment() tomlLexStateFn {
for next := l.peek(); next != '\n' && next != eof; next = l.peek() { for next := l.peek(); next != '\n' && next != eof; next = l.peek() {
if (next == '\r' && l.follow("\r\n")) { if next == '\r' && l.follow("\r\n") {
break break
} }
l.next() l.next()
-1
View File
@@ -87,7 +87,6 @@ func TestMultipleKeyGroupsComment(t *testing.T) {
}) })
} }
func TestSimpleWindowsCRLF(t *testing.T) { func TestSimpleWindowsCRLF(t *testing.T) {
testFlow(t, "a=4\r\nb=2", []token{ testFlow(t, "a=4\r\nb=2", []token{
token{Position{1, 1}, tokenKey, "a"}, token{Position{1, 1}, tokenKey, "a"},
+3 -3
View File
@@ -109,7 +109,7 @@ func TestPathSliceStart(t *testing.T) {
assertPath(t, assertPath(t,
"$[123:]", "$[123:]",
buildPath( buildPath(
newMatchSliceFn(123, MaxInt, 1), newMatchSliceFn(123, maxInt, 1),
)) ))
} }
@@ -133,7 +133,7 @@ func TestPathSliceStartStep(t *testing.T) {
assertPath(t, assertPath(t,
"$[123::7]", "$[123::7]",
buildPath( buildPath(
newMatchSliceFn(123, MaxInt, 7), newMatchSliceFn(123, maxInt, 7),
)) ))
} }
@@ -149,7 +149,7 @@ func TestPathSliceStep(t *testing.T) {
assertPath(t, assertPath(t,
"$[::7]", "$[::7]",
buildPath( buildPath(
newMatchSliceFn(0, MaxInt, 7), newMatchSliceFn(0, maxInt, 7),
)) ))
} }
+14 -12
View File
@@ -4,7 +4,8 @@ import (
"time" "time"
) )
// Type of a user-defined filter function, for use with Query.SetFilter(). // NodeFilterFn represents a user-defined filter function, for use with
// Query.SetFilter().
// //
// The return value of the function must indicate if 'node' is to be included // The return value of the function must indicate if 'node' is to be included
// at this stage of the TOML path. Returning true will include the node, and // at this stage of the TOML path. Returning true will include the node, and
@@ -14,20 +15,21 @@ import (
// to use from multiple goroutines. // to use from multiple goroutines.
type NodeFilterFn func(node interface{}) bool type NodeFilterFn func(node interface{}) bool
// The result of Executing a Query // QueryResult is the result of Executing a Query.
type QueryResult struct { type QueryResult struct {
items []interface{} items []interface{}
positions []Position positions []Position
} }
// appends a value/position pair to the result set // appends a value/position pair to the result set.
func (r *QueryResult) appendResult(node interface{}, pos Position) { func (r *QueryResult) appendResult(node interface{}, pos Position) {
r.items = append(r.items, node) r.items = append(r.items, node)
r.positions = append(r.positions, pos) r.positions = append(r.positions, pos)
} }
// Set of values within a QueryResult. The order of values is not guaranteed // Values is a set of values within a QueryResult. The order of values is not
// to be in document order, and may be different each time a query is executed. // guaranteed to be in document order, and may be different each time a query is
// executed.
func (r *QueryResult) Values() []interface{} { func (r *QueryResult) Values() []interface{} {
values := make([]interface{}, len(r.items)) values := make([]interface{}, len(r.items))
for i, v := range r.items { for i, v := range r.items {
@@ -41,8 +43,8 @@ func (r *QueryResult) Values() []interface{} {
return values return values
} }
// Set of positions for values within a QueryResult. Each index in Positions() // Positions is a set of positions for values within a QueryResult. Each index
// corresponds to the entry in Value() of the same index. // in Positions() corresponds to the entry in Value() of the same index.
func (r *QueryResult) Positions() []Position { func (r *QueryResult) Positions() []Position {
return r.positions return r.positions
} }
@@ -86,13 +88,13 @@ func (q *Query) appendPath(next pathFn) {
next.setNext(newTerminatingFn()) // init the next functor next.setNext(newTerminatingFn()) // init the next functor
} }
// Compiles a TOML path expression. The returned Query can be used to match // CompileQuery compiles a TOML path expression. The returned Query can be used
// elements within a TomlTree and its descendants. // to match elements within a TomlTree and its descendants.
func CompileQuery(path string) (*Query, error) { func CompileQuery(path string) (*Query, error) {
return parseQuery(lexQuery(path)) return parseQuery(lexQuery(path))
} }
// Executes a query against a TomlTree, and returns the result of the query. // Execute executes a query against a TomlTree, and returns the result of the query.
func (q *Query) Execute(tree *TomlTree) *QueryResult { func (q *Query) Execute(tree *TomlTree) *QueryResult {
result := &QueryResult{ result := &QueryResult{
items: []interface{}{}, items: []interface{}{},
@@ -110,8 +112,8 @@ func (q *Query) Execute(tree *TomlTree) *QueryResult {
return result return result
} }
// Sets a user-defined filter function. These may be used inside "?(..)" query // SetFilter sets a user-defined filter function. These may be used inside
// expressions to filter TOML document elements within a query. // "?(..)" query expressions to filter TOML document elements within a query.
func (q *Query) SetFilter(name string, fn NodeFilterFn) { func (q *Query) SetFilter(name string, fn NodeFilterFn) {
if q.filters == &defaultFilterFunctions { if q.filters == &defaultFilterFunctions {
// clone the static table // clone the static table
+1 -1
View File
@@ -5,7 +5,7 @@ import (
) )
func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects ...interface{}) { func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects ...interface{}) {
if (len(array) != len(objects)) { if len(array) != len(objects) {
t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects)) t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects))
} }
+2 -2
View File
@@ -11,7 +11,7 @@ import (
"fmt" "fmt"
) )
const MaxInt = int(^uint(0) >> 1) const maxInt = int(^uint(0) >> 1)
type queryParser struct { type queryParser struct {
flow chan token flow chan token
@@ -203,7 +203,7 @@ loop: // labeled loop for easy breaking
func (p *queryParser) parseSliceExpr() queryParserStateFn { func (p *queryParser) parseSliceExpr() queryParserStateFn {
// init slice to grab all elements // init slice to grab all elements
start, end, step := 0, MaxInt, 1 start, end, step := 0, maxInt, 1
// parse optional start // parse optional start
tok := p.getToken() tok := p.getToken()
+5 -3
View File
@@ -29,6 +29,7 @@ func newTomlTree() *TomlTree {
} }
} }
// TreeFromMap initializes a new TomlTree object using the given map.
func TreeFromMap(m map[string]interface{}) *TomlTree { func TreeFromMap(m map[string]interface{}) *TomlTree {
return &TomlTree{ return &TomlTree{
values: m, values: m,
@@ -347,12 +348,13 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
return result return result
} }
// Query compiles and executes a query on a tree and returns the query result.
func (t *TomlTree) Query(query string) (*QueryResult, error) { func (t *TomlTree) Query(query string) (*QueryResult, error) {
if q, err := CompileQuery(query); err != nil { q, err := CompileQuery(query)
if err != nil {
return nil, err return nil, err
} else {
return q.Execute(t), nil
} }
return q.Execute(t), nil
} }
// ToString generates a human-readable representation of the current tree. // ToString generates a human-readable representation of the current tree.