Skip to content

Commit

Permalink
Clean up lint (pelletier#56)
Browse files Browse the repository at this point in the history
The only real change in this commit is that MaxInt is made private.
Everything else should be gofmt'ing, docs and cleanup of lint.
  • Loading branch information
moorereason authored and pelletier committed Apr 18, 2016
1 parent 9d93af6 commit 6e26017
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
6 changes: 3 additions & 3 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestPathSliceStart(t *testing.T) {
assertPath(t,
"$[123:]",
buildPath(
newMatchSliceFn(123, MaxInt, 1),
newMatchSliceFn(123, maxInt, 1),
))
}

Expand All @@ -133,7 +133,7 @@ func TestPathSliceStartStep(t *testing.T) {
assertPath(t,
"$[123::7]",
buildPath(
newMatchSliceFn(123, MaxInt, 7),
newMatchSliceFn(123, maxInt, 7),
))
}

Expand All @@ -149,7 +149,7 @@ func TestPathSliceStep(t *testing.T) {
assertPath(t,
"$[::7]",
buildPath(
newMatchSliceFn(0, MaxInt, 7),
newMatchSliceFn(0, maxInt, 7),
))
}

Expand Down
36 changes: 19 additions & 17 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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{}{},
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions queryparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"
)

const MaxInt = int(^uint(0) >> 1)
const maxInt = int(^uint(0) >> 1)

type queryParser struct {
flow chan token
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6e26017

Please sign in to comment.