gofmt pass
This commit is contained in:
@@ -8,18 +8,18 @@ import (
|
||||
// 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) SetNext(next PathFn) {
|
||||
}
|
||||
|
||||
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 newMatchIndexFn(idx int) *matchIndexFn {
|
||||
}
|
||||
|
||||
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 newMatchAnyFn() *matchAnyFn {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ func assertPathMatch(t *testing.T, path, ref *Query) bool {
|
||||
}
|
||||
|
||||
func assertPath(t *testing.T, query string, ref *Query) {
|
||||
path, _:= parseQuery(lexQuery(query))
|
||||
path, _ := parseQuery(lexQuery(query))
|
||||
assertPathMatch(t, path, ref)
|
||||
}
|
||||
|
||||
|
||||
@@ -154,7 +154,7 @@ func (p *tomlParser) parseGroup() tomlParserStateFn {
|
||||
|
||||
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 (p *tomlParser) parseArray() []interface{} {
|
||||
|
||||
func parseToml(flow chan token) *TomlTree {
|
||||
result := newTomlTree()
|
||||
result.position = Position{1,1}
|
||||
result.position = Position{1, 1}
|
||||
parser := &tomlParser{
|
||||
flow: flow,
|
||||
tree: result,
|
||||
|
||||
+6
-6
@@ -396,7 +396,7 @@ func TestDocumentPositions(t *testing.T) {
|
||||
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 @@ func TestDocumentPositionsWithSpaces(t *testing.T) {
|
||||
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 @@ func TestDocumentPositionsWithGroupArray(t *testing.T) {
|
||||
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 @@ func TestNestedTreePosition(t *testing.T) {
|
||||
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},
|
||||
})
|
||||
|
||||
@@ -4,29 +4,29 @@ type nodeFilterFn func(node interface{}) bool
|
||||
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 Compile(path string) (*Query, error) {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +116,6 @@ func (l *queryLexer) follow(next string) bool {
|
||||
return strings.HasPrefix(l.input[l.pos:], next)
|
||||
}
|
||||
|
||||
|
||||
func (l *queryLexer) lexVoid() queryLexStateFn {
|
||||
for {
|
||||
next := l.peek()
|
||||
|
||||
+4
-4
@@ -15,17 +15,17 @@ import (
|
||||
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() {
|
||||
|
||||
+210
-210
@@ -2,72 +2,72 @@ package toml
|
||||
|
||||
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 @@ func assertQueryPositions(t *testing.T, toml, query string, ref []interface{}) {
|
||||
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 @@ func TestQueryRoot(t *testing.T) {
|
||||
"$",
|
||||
[]interface{}{
|
||||
queryTestNode{
|
||||
map[string]interface{}{
|
||||
"a": int64(42),
|
||||
}, Position{1, 1},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"a": int64(42),
|
||||
}, Position{1, 1},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -104,8 +104,8 @@ func TestQueryKey(t *testing.T) {
|
||||
"$.foo.a",
|
||||
[]interface{}{
|
||||
queryTestNode{
|
||||
int64(42), Position{2,1},
|
||||
},
|
||||
int64(42), Position{2, 1},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -115,8 +115,8 @@ func TestQueryKeyString(t *testing.T) {
|
||||
"$.foo['a']",
|
||||
[]interface{}{
|
||||
queryTestNode{
|
||||
int64(42), Position{2,1},
|
||||
},
|
||||
int64(42), Position{2, 1},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -126,8 +126,8 @@ func TestQueryIndex(t *testing.T) {
|
||||
"$.foo.a[5]",
|
||||
[]interface{}{
|
||||
queryTestNode{
|
||||
int64(6), Position{2,1},
|
||||
},
|
||||
int64(6), Position{2, 1},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -137,21 +137,21 @@ func TestQuerySliceRange(t *testing.T) {
|
||||
"$.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 @@ func TestQuerySliceStep(t *testing.T) {
|
||||
"[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 @@ func TestQueryAny(t *testing.T) {
|
||||
"$.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 @@ func TestQueryUnionSimple(t *testing.T) {
|
||||
"$.*[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 @@ func TestQueryRecursionAll(t *testing.T) {
|
||||
"$..*",
|
||||
[]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 @@ func TestQueryRecursionUnionSimple(t *testing.T) {
|
||||
"$..['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 @@ func TestQueryScriptFnLast(t *testing.T) {
|
||||
"[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 @@ func TestQueryFilterFnOdd(t *testing.T) {
|
||||
"[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 @@ func TestQueryFilterFnEven(t *testing.T) {
|
||||
"[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},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package toml
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strconv"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
@@ -26,18 +26,18 @@ const (
|
||||
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 @@ var tokenTypeNames = []string{
|
||||
"=",
|
||||
"[",
|
||||
"[",
|
||||
"(",
|
||||
")",
|
||||
"(",
|
||||
")",
|
||||
"]]",
|
||||
"[[",
|
||||
"Date",
|
||||
"KeyGroup",
|
||||
"KeyGroupArray",
|
||||
",",
|
||||
":",
|
||||
"$",
|
||||
"*",
|
||||
"?",
|
||||
".",
|
||||
"..",
|
||||
":",
|
||||
"$",
|
||||
"*",
|
||||
"?",
|
||||
".",
|
||||
"..",
|
||||
"EOL",
|
||||
}
|
||||
|
||||
|
||||
@@ -207,8 +207,8 @@ func (t *TomlTree) createSubTree(keys []string, pos Position) error {
|
||||
}
|
||||
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) toToml(indent, keyspace string) string {
|
||||
}
|
||||
|
||||
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 @@ func (t *TomlTree) ToString() string {
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
+21
-21
@@ -49,26 +49,26 @@ func TestTomlGetPath(t *testing.T) {
|
||||
}
|
||||
|
||||
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"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user