gofmt pass
diff --git a/match.go b/match.go index d4f5c01..2b89be9 100644 --- a/match.go +++ b/match.go
@@ -8,18 +8,18 @@ // NOTE: this is done to allow ctx.lastPosition to indicate the start of any // values returned by the query engines func tomlValueCheck(node interface{}, ctx *queryContext) interface{} { - switch castNode := node.(type) { - case *tomlValue: - ctx.lastPosition = castNode.position - return castNode.value - case []*TomlTree: - if len(castNode) > 0 { - ctx.lastPosition = castNode[0].position - } - return node + switch castNode := node.(type) { + case *tomlValue: + ctx.lastPosition = castNode.position + return castNode.value + case []*TomlTree: + if len(castNode) > 0 { + ctx.lastPosition = castNode[0].position + } + return node default: - return node - } + return node + } } // base match @@ -45,15 +45,15 @@ } func (f *terminatingFn) Call(node interface{}, ctx *queryContext) { - switch castNode := node.(type) { - case *TomlTree: - ctx.result.appendResult(node, castNode.position) - case *tomlValue: - ctx.result.appendResult(node, castNode.position) - default: - // use last position for scalars - ctx.result.appendResult(node, ctx.lastPosition) - } + switch castNode := node.(type) { + case *TomlTree: + ctx.result.appendResult(node, castNode.position) + case *tomlValue: + ctx.result.appendResult(node, castNode.position) + default: + // use last position for scalars + ctx.result.appendResult(node, ctx.lastPosition) + } } // match single key @@ -86,11 +86,11 @@ } func (f *matchIndexFn) Call(node interface{}, ctx *queryContext) { - if arr, ok := tomlValueCheck(node, ctx).([]interface{}); ok { - if f.Idx < len(arr) && f.Idx >= 0 { - f.next.Call(arr[f.Idx], ctx) - } - } + if arr, ok := tomlValueCheck(node, ctx).([]interface{}); ok { + if f.Idx < len(arr) && f.Idx >= 0 { + f.next.Call(arr[f.Idx], ctx) + } + } } // filter by slicing @@ -134,7 +134,7 @@ func (f *matchAnyFn) Call(node interface{}, ctx *queryContext) { if tree, ok := node.(*TomlTree); ok { - for _,v := range tree.values { + for _, v := range tree.values { f.next.Call(v, ctx) } }
diff --git a/match_test.go b/match_test.go index c667030..cd0390b 100644 --- a/match_test.go +++ b/match_test.go
@@ -57,7 +57,7 @@ } func assertPath(t *testing.T, query string, ref *Query) { - path, _:= parseQuery(lexQuery(query)) + path, _ := parseQuery(lexQuery(query)) assertPathMatch(t, path, ref) }
diff --git a/parser.go b/parser.go index a67a1e6..60918cc 100644 --- a/parser.go +++ b/parser.go
@@ -154,7 +154,7 @@ p.seenGroupKeys = append(p.seenGroupKeys, key.val) keys := strings.Split(key.val, ".") - if err := p.tree.createSubTree(keys,startToken.Position); err != nil { + if err := p.tree.createSubTree(keys, startToken.Position); err != nil { p.raiseError(key, "%s", err) } p.assume(tokenRightBracket) @@ -274,7 +274,7 @@ func parseToml(flow chan token) *TomlTree { result := newTomlTree() - result.position = Position{1,1} + result.position = Position{1, 1} parser := &tomlParser{ flow: flow, tree: result,
diff --git a/parser_test.go b/parser_test.go index b429b2f..8160905 100644 --- a/parser_test.go +++ b/parser_test.go
@@ -396,7 +396,7 @@ assertPosition(t, "[foo]\nbar=42\nbaz=69", map[string]Position{ - "": Position{1, 1}, + "": Position{1, 1}, "foo": Position{1, 1}, "foo.bar": Position{2, 1}, "foo.baz": Position{3, 1}, @@ -407,7 +407,7 @@ assertPosition(t, " [foo]\n bar=42\n baz=69", map[string]Position{ - "": Position{1, 1}, + "": Position{1, 1}, "foo": Position{1, 3}, "foo.bar": Position{2, 3}, "foo.baz": Position{3, 3}, @@ -418,7 +418,7 @@ assertPosition(t, "[[foo]]\nbar=42\nbaz=69", map[string]Position{ - "": Position{1, 1}, + "": Position{1, 1}, "foo": Position{1, 1}, "foo.bar": Position{2, 1}, "foo.baz": Position{3, 1}, @@ -429,9 +429,9 @@ assertPosition(t, "[foo.bar]\na=42\nb=69", map[string]Position{ - "": Position{1, 1}, - "foo": Position{1, 1}, - "foo.bar": Position{1, 1}, + "": Position{1, 1}, + "foo": Position{1, 1}, + "foo.bar": Position{1, 1}, "foo.bar.a": Position{2, 1}, "foo.bar.b": Position{3, 1}, })
diff --git a/query.go b/query.go index 8bfb9ed..dfda4e8 100644 --- a/query.go +++ b/query.go
@@ -4,29 +4,29 @@ type nodeFn func(node interface{}) interface{} type QueryResult struct { - items []interface{} - positions []Position + items []interface{} + positions []Position } func (r *QueryResult) appendResult(node interface{}, pos Position) { - r.items = append(r.items, node) - r.positions = append(r.positions, pos) + r.items = append(r.items, node) + r.positions = append(r.positions, pos) } func (r *QueryResult) Values() []interface{} { - return r.items + return r.items } func (r *QueryResult) Positions() []Position { - return r.positions + return r.positions } // runtime context for executing query paths type queryContext struct { - result *QueryResult - filters *map[string]nodeFilterFn - scripts *map[string]nodeFn - lastPosition Position + result *QueryResult + filters *map[string]nodeFilterFn + scripts *map[string]nodeFn + lastPosition Position } // generic path functor interface @@ -68,20 +68,20 @@ } func (q *Query) Execute(tree *TomlTree) *QueryResult { - result := &QueryResult { - items: []interface{}{}, - positions: []Position{}, - } + result := &QueryResult{ + items: []interface{}{}, + positions: []Position{}, + } if q.root == nil { - result.appendResult(tree, tree.GetPosition("")) + result.appendResult(tree, tree.GetPosition("")) } else { - ctx := &queryContext{ - result: result, - filters: q.filters, - scripts: q.scripts, - } - q.root.Call(tree, ctx) - } + ctx := &queryContext{ + result: result, + filters: q.filters, + scripts: q.scripts, + } + q.root.Call(tree, ctx) + } return result }
diff --git a/querylexer.go b/querylexer.go index 355838c..1532e1d 100644 --- a/querylexer.go +++ b/querylexer.go
@@ -116,7 +116,6 @@ return strings.HasPrefix(l.input[l.pos:], next) } - func (l *queryLexer) lexVoid() queryLexStateFn { for { next := l.peek()
diff --git a/queryparser.go b/queryparser.go index 4973afb..8fdb10b 100644 --- a/queryparser.go +++ b/queryparser.go
@@ -15,17 +15,17 @@ type queryParser struct { flow chan token tokensBuffer []token - query *Query + query *Query union []PathFn - err error + err error } type queryParserStateFn func() queryParserStateFn // Formats and panics an error message based on a token func (p *queryParser) parseError(tok *token, msg string, args ...interface{}) queryParserStateFn { - p.err = fmt.Errorf(tok.Position.String() + ": " + msg, args...) - return nil // trigger parse to end + p.err = fmt.Errorf(tok.Position.String()+": "+msg, args...) + return nil // trigger parse to end } func (p *queryParser) run() {
diff --git a/queryparser_test.go b/queryparser_test.go index 96c88d0..bd147a0 100644 --- a/queryparser_test.go +++ b/queryparser_test.go
@@ -2,72 +2,72 @@ import ( "fmt" + "sort" + "strings" "testing" - "sort" - "strings" ) type queryTestNode struct { - value interface{} - position Position + value interface{} + position Position } func valueString(root interface{}) string { - result := "" //fmt.Sprintf("%T:", root) + result := "" //fmt.Sprintf("%T:", root) switch node := root.(type) { - case *tomlValue: - return valueString(node.value) + case *tomlValue: + return valueString(node.value) case *QueryResult: - items := []string{} - for i, v := range node.Values() { - items = append(items, fmt.Sprintf("%s:%s", - node.Positions()[i].String(), valueString(v))) - } - sort.Strings(items) - result = "[" + strings.Join(items, ", ") + "]" - case queryTestNode: - result = fmt.Sprintf("%s:%s", - node.position.String(), valueString(node.value)) + items := []string{} + for i, v := range node.Values() { + items = append(items, fmt.Sprintf("%s:%s", + node.Positions()[i].String(), valueString(v))) + } + sort.Strings(items) + result = "[" + strings.Join(items, ", ") + "]" + case queryTestNode: + result = fmt.Sprintf("%s:%s", + node.position.String(), valueString(node.value)) case []interface{}: - items := []string{} - for _, v := range node { - items = append(items, valueString(v)) - } - sort.Strings(items) - result = "[" + strings.Join(items, ", ") + "]" + items := []string{} + for _, v := range node { + items = append(items, valueString(v)) + } + sort.Strings(items) + result = "[" + strings.Join(items, ", ") + "]" case *TomlTree: - // workaround for unreliable map key ordering - items := []string{} - for _, k := range node.Keys() { - v := node.GetPath([]string{k}) - items = append(items, k + ":" + valueString(v)) - } - sort.Strings(items) - result = "{" + strings.Join(items, ", ") + "}" + // workaround for unreliable map key ordering + items := []string{} + for _, k := range node.Keys() { + v := node.GetPath([]string{k}) + items = append(items, k+":"+valueString(v)) + } + sort.Strings(items) + result = "{" + strings.Join(items, ", ") + "}" case map[string]interface{}: - // workaround for unreliable map key ordering - items := []string{} - for k, v := range node { - items = append(items, k + ":" + valueString(v)) - } - sort.Strings(items) - result = "{" + strings.Join(items, ", ") + "}" + // workaround for unreliable map key ordering + items := []string{} + for k, v := range node { + items = append(items, k+":"+valueString(v)) + } + sort.Strings(items) + result = "{" + strings.Join(items, ", ") + "}" case int64: - result += fmt.Sprintf("%d", node) - case string: - result += "'" + node + "'" - } - return result + result += fmt.Sprintf("%d", node) + case string: + result += "'" + node + "'" + } + return result } func assertValue(t *testing.T, result, ref interface{}) { - pathStr := valueString(result) - refStr := valueString(ref) - if pathStr != refStr { - t.Errorf("values do not match") + pathStr := valueString(result) + refStr := valueString(ref) + if pathStr != refStr { + t.Errorf("values do not match") t.Log("test:", pathStr) t.Log("ref: ", refStr) - } + } } func assertQueryPositions(t *testing.T, toml, query string, ref []interface{}) { @@ -76,12 +76,12 @@ t.Errorf("Non-nil toml parse error: %v", err) return } - q, err := Compile(query) - if err != nil { - t.Error(err) - return - } - results := q.Execute(tree) + q, err := Compile(query) + if err != nil { + t.Error(err) + return + } + results := q.Execute(tree) assertValue(t, results, ref) } @@ -91,10 +91,10 @@ "$", []interface{}{ queryTestNode{ - map[string]interface{}{ - "a": int64(42), - }, Position{1, 1}, - }, + map[string]interface{}{ + "a": int64(42), + }, Position{1, 1}, + }, }) } @@ -104,8 +104,8 @@ "$.foo.a", []interface{}{ queryTestNode{ - int64(42), Position{2,1}, - }, + int64(42), Position{2, 1}, + }, }) } @@ -115,8 +115,8 @@ "$.foo['a']", []interface{}{ queryTestNode{ - int64(42), Position{2,1}, - }, + int64(42), Position{2, 1}, + }, }) } @@ -126,8 +126,8 @@ "$.foo.a[5]", []interface{}{ queryTestNode{ - int64(6), Position{2,1}, - }, + int64(6), Position{2, 1}, + }, }) } @@ -137,21 +137,21 @@ "$.foo.a[0:5]", []interface{}{ queryTestNode{ - int64(1), Position{2,1}, - }, - queryTestNode{ - int64(2), Position{2,1}, - }, - queryTestNode{ - int64(3), Position{2,1}, - }, - queryTestNode{ - int64(4), Position{2,1}, - }, - queryTestNode{ - int64(5), Position{2,1}, - }, - }) + int64(1), Position{2, 1}, + }, + queryTestNode{ + int64(2), Position{2, 1}, + }, + queryTestNode{ + int64(3), Position{2, 1}, + }, + queryTestNode{ + int64(4), Position{2, 1}, + }, + queryTestNode{ + int64(5), Position{2, 1}, + }, + }) } func TestQuerySliceStep(t *testing.T) { @@ -159,15 +159,15 @@ "[foo]\na = [1,2,3,4,5,6,7,8,9,0]", "$.foo.a[0:5:2]", []interface{}{ - queryTestNode{ - int64(1), Position{2,1}, - }, - queryTestNode{ - int64(3), Position{2,1}, - }, - queryTestNode{ - int64(5), Position{2,1}, - }, + queryTestNode{ + int64(1), Position{2, 1}, + }, + queryTestNode{ + int64(3), Position{2, 1}, + }, + queryTestNode{ + int64(5), Position{2, 1}, + }, }) } @@ -177,17 +177,17 @@ "$.foo.*", []interface{}{ queryTestNode{ - map[string]interface{}{ - "a": int64(1), - "b": int64(2), - }, Position{1,1}, - }, + map[string]interface{}{ + "a": int64(1), + "b": int64(2), + }, Position{1, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(3), - "b": int64(4), - }, Position{4,1}, - }, + map[string]interface{}{ + "a": int64(3), + "b": int64(4), + }, Position{4, 1}, + }, }) } func TestQueryUnionSimple(t *testing.T) { @@ -196,24 +196,24 @@ "$.*[bar,foo]", []interface{}{ queryTestNode{ - map[string]interface{}{ - "a": int64(1), - "b": int64(2), - }, Position{1,1}, - }, + map[string]interface{}{ + "a": int64(1), + "b": int64(2), + }, Position{1, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(3), - "b": int64(4), - }, Position{4,1}, - }, + map[string]interface{}{ + "a": int64(3), + "b": int64(4), + }, Position{4, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(5), - "b": int64(6), - }, Position{7,1}, - }, - }) + map[string]interface{}{ + "a": int64(5), + "b": int64(6), + }, Position{7, 1}, + }, + }) } func TestQueryRecursionAll(t *testing.T) { @@ -222,65 +222,65 @@ "$..*", []interface{}{ queryTestNode{ - map[string]interface{}{ - "bar": map[string]interface{}{ - "a": int64(1), - "b": int64(2), - }, - }, Position{1,1}, - }, + map[string]interface{}{ + "bar": map[string]interface{}{ + "a": int64(1), + "b": int64(2), + }, + }, Position{1, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(1), - "b": int64(2), - }, Position{1,1}, - }, + map[string]interface{}{ + "a": int64(1), + "b": int64(2), + }, Position{1, 1}, + }, queryTestNode{ - int64(1), Position{2,1}, - }, + int64(1), Position{2, 1}, + }, queryTestNode{ - int64(2), Position{3,1}, - }, + int64(2), Position{3, 1}, + }, queryTestNode{ - map[string]interface{}{ - "foo": map[string]interface{}{ - "a": int64(3), - "b": int64(4), - }, - }, Position{4,1}, - }, + map[string]interface{}{ + "foo": map[string]interface{}{ + "a": int64(3), + "b": int64(4), + }, + }, Position{4, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(3), - "b": int64(4), - }, Position{4,1}, - }, + map[string]interface{}{ + "a": int64(3), + "b": int64(4), + }, Position{4, 1}, + }, queryTestNode{ - int64(3), Position{5,1}, - }, + int64(3), Position{5, 1}, + }, queryTestNode{ - int64(4), Position{6,1}, - }, + int64(4), Position{6, 1}, + }, queryTestNode{ - map[string]interface{}{ - "foo": map[string]interface{}{ - "a": int64(5), - "b": int64(6), - }, - }, Position{7,1}, - }, + map[string]interface{}{ + "foo": map[string]interface{}{ + "a": int64(5), + "b": int64(6), + }, + }, Position{7, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(5), - "b": int64(6), - }, Position{7,1}, - }, + map[string]interface{}{ + "a": int64(5), + "b": int64(6), + }, Position{7, 1}, + }, queryTestNode{ - int64(5), Position{8,1}, - }, + int64(5), Position{8, 1}, + }, queryTestNode{ - int64(6), Position{9,1}, - }, + int64(6), Position{9, 1}, + }, }) } @@ -290,23 +290,23 @@ "$..['foo','bar']", []interface{}{ queryTestNode{ - map[string]interface{}{ - "a": int64(1), - "b": int64(2), - }, Position{1,1}, - }, + map[string]interface{}{ + "a": int64(1), + "b": int64(2), + }, Position{1, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(3), - "b": int64(4), - }, Position{4,1}, - }, + map[string]interface{}{ + "a": int64(3), + "b": int64(4), + }, Position{4, 1}, + }, queryTestNode{ - map[string]interface{}{ - "a": int64(5), - "b": int64(6), - }, Position{7,1}, - }, + map[string]interface{}{ + "a": int64(5), + "b": int64(6), + }, Position{7, 1}, + }, }) } @@ -315,9 +315,9 @@ "[foo]\na = [0,1,2,3,4,5,6,7,8,9]", "$.foo.a[(last)]", []interface{}{ - queryTestNode{ - int64(9), Position{2,1}, - }, + queryTestNode{ + int64(9), Position{2, 1}, + }, }) } @@ -326,21 +326,21 @@ "[foo]\na = [0,1,2,3,4,5,6,7,8,9]", "$.foo.a[?(odd)]", []interface{}{ - queryTestNode{ - int64(1), Position{2,1}, - }, - queryTestNode{ - int64(3), Position{2,1}, - }, - queryTestNode{ - int64(5), Position{2,1}, - }, - queryTestNode{ - int64(7), Position{2,1}, - }, - queryTestNode{ - int64(9), Position{2,1}, - }, + queryTestNode{ + int64(1), Position{2, 1}, + }, + queryTestNode{ + int64(3), Position{2, 1}, + }, + queryTestNode{ + int64(5), Position{2, 1}, + }, + queryTestNode{ + int64(7), Position{2, 1}, + }, + queryTestNode{ + int64(9), Position{2, 1}, + }, }) } @@ -349,20 +349,20 @@ "[foo]\na = [0,1,2,3,4,5,6,7,8,9]", "$.foo.a[?(even)]", []interface{}{ - queryTestNode{ - int64(0), Position{2,1}, - }, - queryTestNode{ - int64(2), Position{2,1}, - }, - queryTestNode{ - int64(4), Position{2,1}, - }, - queryTestNode{ - int64(6), Position{2,1}, - }, - queryTestNode{ - int64(8), Position{2,1}, - }, + queryTestNode{ + int64(0), Position{2, 1}, + }, + queryTestNode{ + int64(2), Position{2, 1}, + }, + queryTestNode{ + int64(4), Position{2, 1}, + }, + queryTestNode{ + int64(6), Position{2, 1}, + }, + queryTestNode{ + int64(8), Position{2, 1}, + }, }) }
diff --git a/token.go b/token.go index eb44066..22ad37a 100644 --- a/token.go +++ b/token.go
@@ -2,7 +2,7 @@ import ( "fmt" - "strconv" + "strconv" "unicode" ) @@ -26,18 +26,18 @@ tokenEqual tokenLeftBracket tokenRightBracket - tokenLeftParen - tokenRightParen + tokenLeftParen + tokenRightParen tokenDoubleLeftBracket tokenDoubleRightBracket tokenDate tokenKeyGroup tokenKeyGroupArray tokenComma - tokenColon + tokenColon tokenDollar - tokenStar - tokenQuestion + tokenStar + tokenQuestion tokenDot tokenDotDot tokenEOL @@ -55,20 +55,20 @@ "=", "[", "[", - "(", - ")", + "(", + ")", "]]", "[[", "Date", "KeyGroup", "KeyGroupArray", ",", - ":", - "$", - "*", - "?", - ".", - "..", + ":", + "$", + "*", + "?", + ".", + "..", "EOL", }
diff --git a/toml.go b/toml.go index 043d609..2c7c55f 100644 --- a/toml.go +++ b/toml.go
@@ -207,8 +207,8 @@ } nextTree, exists := subtree.values[intermediateKey] if !exists { - tree := newTomlTree() - tree.position = pos + tree := newTomlTree() + tree.position = pos subtree.values[intermediateKey] = tree nextTree = tree } @@ -320,11 +320,11 @@ } func (t *TomlTree) Query(query string) (*QueryResult, error) { - if q, err := Compile(query); err != nil { - return nil, err - } else { - return q.Execute(t), nil - } + if q, err := Compile(query); err != nil { + return nil, err + } else { + return q.Execute(t), nil + } } // ToString generates a human-readable representation of the current tree. @@ -335,26 +335,26 @@ // Load creates a TomlTree from a string. func Load(content string) (tree *TomlTree, err error) { - defer func() { - if r := recover(); r != nil { - if _, ok := r.(runtime.Error); ok { - panic(r) - } - err = errors.New(r.(string)) - } - }() - tree = parseToml(lexToml(content)) - return + defer func() { + if r := recover(); r != nil { + if _, ok := r.(runtime.Error); ok { + panic(r) + } + err = errors.New(r.(string)) + } + }() + tree = parseToml(lexToml(content)) + return } // LoadFile creates a TomlTree from a file. func LoadFile(path string) (tree *TomlTree, err error) { - buff, ferr := ioutil.ReadFile(path) - if ferr != nil { - err = ferr - } else { - s := string(buff) - tree, err = Load(s) - } - return + buff, ferr := ioutil.ReadFile(path) + if ferr != nil { + err = ferr + } else { + s := string(buff) + tree, err = Load(s) + } + return }
diff --git a/toml_test.go b/toml_test.go index f14d046..c89b973 100644 --- a/toml_test.go +++ b/toml_test.go
@@ -49,26 +49,26 @@ } func TestTomlQuery(t *testing.T) { - tree, err := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6") - if err != nil { - t.Error(err) - return - } - result, err := tree.Query("$.foo.bar") - if err != nil { - t.Error(err) - return - } - values := result.Values() - if len(values) != 1 { - t.Errorf("Expected resultset of 1, got %d instead: %v", len(values), values) - } + tree, err := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6") + if err != nil { + t.Error(err) + return + } + result, err := tree.Query("$.foo.bar") + if err != nil { + t.Error(err) + return + } + values := result.Values() + if len(values) != 1 { + t.Errorf("Expected resultset of 1, got %d instead: %v", len(values), values) + } - if tt, ok := values[0].(*TomlTree); !ok { - t.Errorf("Expected type of TomlTree: %T Tv", values[0], values[0]) - } else if tt.Get("a") != int64(1) { - t.Errorf("Expected 'a' with a value 1: %v", tt.Get("a")) - } else if tt.Get("b") != int64(2) { - t.Errorf("Expected 'b' with a value 2: %v", tt.Get("b")) - } + if tt, ok := values[0].(*TomlTree); !ok { + t.Errorf("Expected type of TomlTree: %T Tv", values[0], values[0]) + } else if tt.Get("a") != int64(1) { + t.Errorf("Expected 'a' with a value 1: %v", tt.Get("a")) + } else if tt.Get("b") != int64(2) { + t.Errorf("Expected 'b' with a value 2: %v", tt.Get("b")) + } }