diff --git a/lexer.go b/lexer.go index 0bffb0d..3c7641a 100644 --- a/lexer.go +++ b/lexer.go @@ -299,7 +299,7 @@ func (l *tomlLexer) lexKey() tomlLexStateFn { func (l *tomlLexer) lexComment() tomlLexStateFn { 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 } l.next() diff --git a/lexer_test.go b/lexer_test.go index 6183061..151b8bc 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -87,7 +87,6 @@ func TestMultipleKeyGroupsComment(t *testing.T) { }) } - func TestSimpleWindowsCRLF(t *testing.T) { testFlow(t, "a=4\r\nb=2", []token{ token{Position{1, 1}, tokenKey, "a"}, diff --git a/match_test.go b/match_test.go index fc2adc6..b63654a 100644 --- a/match_test.go +++ b/match_test.go @@ -109,7 +109,7 @@ func TestPathSliceStart(t *testing.T) { assertPath(t, "$[123:]", buildPath( - newMatchSliceFn(123, MaxInt, 1), + newMatchSliceFn(123, maxInt, 1), )) } @@ -133,7 +133,7 @@ func TestPathSliceStartStep(t *testing.T) { assertPath(t, "$[123::7]", buildPath( - newMatchSliceFn(123, MaxInt, 7), + newMatchSliceFn(123, maxInt, 7), )) } @@ -149,7 +149,7 @@ func TestPathSliceStep(t *testing.T) { assertPath(t, "$[::7]", buildPath( - newMatchSliceFn(0, MaxInt, 7), + newMatchSliceFn(0, maxInt, 7), )) } diff --git a/query.go b/query.go index e255bbe..f44848b 100644 --- a/query.go +++ b/query.go @@ -4,30 +4,32 @@ import ( "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 -// at this stage of the TOML path. Returning true will include the node, and -// returning false will exclude it. +// 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 +// returning false will exclude it. // -// NOTE: Care should be taken to write script callbacks such that they are safe -// to use from multiple goroutines. +// NOTE: Care should be taken to write script callbacks such that they are safe +// to use from multiple goroutines. type NodeFilterFn func(node interface{}) bool -// The result of Executing a Query +// QueryResult is the result of Executing a Query. type QueryResult struct { items []interface{} 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) { r.items = append(r.items, node) r.positions = append(r.positions, pos) } -// Set of values within a QueryResult. The order of values is not guaranteed -// to be in document order, and may be different each time a query is executed. +// Values is a set of values within a QueryResult. The order of values is not +// guaranteed to be in document order, and may be different each time a query is +// executed. func (r *QueryResult) Values() []interface{} { values := make([]interface{}, len(r.items)) for i, v := range r.items { @@ -41,8 +43,8 @@ func (r *QueryResult) Values() []interface{} { return values } -// Set of positions for values within a QueryResult. Each index in Positions() -// corresponds to the entry in Value() of the same index. +// Positions is a set of positions for values within a QueryResult. Each index +// in Positions() corresponds to the entry in Value() of the same index. func (r *QueryResult) Positions() []Position { return r.positions } @@ -86,13 +88,13 @@ func (q *Query) appendPath(next pathFn) { next.setNext(newTerminatingFn()) // init the next functor } -// Compiles a TOML path expression. The returned Query can be used to match -// elements within a TomlTree and its descendants. +// CompileQuery compiles a TOML path expression. The returned Query can be used +// to match elements within a TomlTree and its descendants. func CompileQuery(path string) (*Query, error) { 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 { result := &QueryResult{ items: []interface{}{}, @@ -110,8 +112,8 @@ func (q *Query) Execute(tree *TomlTree) *QueryResult { return result } -// Sets a user-defined filter function. These may be used inside "?(..)" query -// expressions to filter TOML document elements within a query. +// SetFilter sets a user-defined filter function. These may be used inside +// "?(..)" query expressions to filter TOML document elements within a query. func (q *Query) SetFilter(name string, fn NodeFilterFn) { if q.filters == &defaultFilterFunctions { // clone the static table diff --git a/query_test.go b/query_test.go index 9bf7bad..0d9f383 100644 --- a/query_test.go +++ b/query_test.go @@ -5,7 +5,7 @@ import ( ) 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)) } @@ -23,7 +23,7 @@ func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects .. } } -func TestQueryExample(t *testing.T) { +func TestQueryExample(t *testing.T) { config, _ := Load(` [[book]] title = "The Stand" diff --git a/queryparser.go b/queryparser.go index e7caa1e..1cbfc83 100644 --- a/queryparser.go +++ b/queryparser.go @@ -11,7 +11,7 @@ import ( "fmt" ) -const MaxInt = int(^uint(0) >> 1) +const maxInt = int(^uint(0) >> 1) type queryParser struct { flow chan token @@ -203,7 +203,7 @@ loop: // labeled loop for easy breaking func (p *queryParser) parseSliceExpr() queryParserStateFn { // init slice to grab all elements - start, end, step := 0, MaxInt, 1 + start, end, step := 0, maxInt, 1 // parse optional start tok := p.getToken() diff --git a/toml.go b/toml.go index 7af032a..f3de359 100644 --- a/toml.go +++ b/toml.go @@ -29,6 +29,7 @@ func newTomlTree() *TomlTree { } } +// TreeFromMap initializes a new TomlTree object using the given map. func TreeFromMap(m map[string]interface{}) *TomlTree { return &TomlTree{ values: m, @@ -347,12 +348,13 @@ func (t *TomlTree) toToml(indent, keyspace string) string { return result } +// Query compiles and executes a query on a tree and returns the query result. 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 - } else { - return q.Execute(t), nil } + return q.Execute(t), nil } // ToString generates a human-readable representation of the current tree.