-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
|
||
package slime | ||
|
||
import "slices" | ||
|
||
var ( | ||
noSelector Selector = &selectSelf{} | ||
) | ||
|
||
type Selector interface { | ||
Select(value Value) Value | ||
WouldSelectEntry(idx int) bool | ||
WouldSelectField(name string) bool | ||
} | ||
|
||
type selectSelf struct{} | ||
|
||
func (s *selectSelf) Select(value Value) Value { | ||
return value | ||
} | ||
|
||
func (s *selectSelf) WouldSelectEntry(idx int) bool { | ||
return false | ||
} | ||
|
||
func (s *selectSelf) WouldSelectField(name string) bool { | ||
return false | ||
} | ||
|
||
type selectEntry struct { | ||
idx int | ||
} | ||
|
||
func (s *selectEntry) Select(value Value) Value { | ||
return value.Entry(s.idx) | ||
} | ||
|
||
func (s *selectEntry) WouldSelectEntry(idx int) bool { | ||
return idx == s.idx | ||
} | ||
|
||
func (s *selectEntry) WouldSelectField(name string) bool { | ||
return false | ||
} | ||
|
||
type selectField struct { | ||
name string | ||
} | ||
|
||
func (s *selectField) Select(value Value) Value { | ||
return value.Field(s.name) | ||
} | ||
|
||
func (s *selectField) WouldSelectEntry(idx int) bool { | ||
return false | ||
} | ||
|
||
func (s *selectField) WouldSelectField(name string) bool { | ||
return name == s.name | ||
} | ||
|
||
type Path struct { | ||
list []Selector | ||
} | ||
|
||
func (p *Path) Len() int { | ||
return len(p.list) | ||
} | ||
|
||
func (p *Path) At(idx int) Selector { | ||
if idx < 0 { | ||
idx = len(p.list) + idx | ||
} | ||
if idx < 0 || idx >= len(p.list) { | ||
return noSelector | ||
} | ||
return p.list[idx] | ||
} | ||
|
||
func NewPath() *Path { | ||
return &Path{} | ||
} | ||
|
||
func (p *Path) Entry(idx int) { | ||
p.list = append(p.list, &selectEntry{idx}) | ||
} | ||
|
||
func (p *Path) Field(name string) { | ||
p.list = append(p.list, &selectField{name}) | ||
} | ||
|
||
func (p *Path) Trim(n int) { | ||
end := len(p.list) - n | ||
if end < 0 { | ||
end = 0 | ||
} | ||
p.list = p.list[:end] | ||
} | ||
|
||
func (p *Path) Apply(value Value) Value { | ||
res := value | ||
for _, s := range p.list { | ||
res = s.Select(res) | ||
} | ||
return res | ||
} | ||
|
||
func (p *Path) Clone() *Path { | ||
return &Path{slices.Clone(p.list)} | ||
} | ||
|
||
func Find(value Value, pred func(path *Path, value Value) bool) []*Path { | ||
path := NewPath() | ||
var result []*Path | ||
var process func(value Value) | ||
perEntry := func(idx int, value Value) { | ||
path.Entry(idx) | ||
process(value) | ||
path.Trim(1) | ||
} | ||
perField := func(name string, value Value) { | ||
path.Field(name) | ||
process(value) | ||
path.Trim(1) | ||
} | ||
process = func(value Value) { | ||
if pred(path, value) { | ||
result = append(result, path.Clone()) | ||
return | ||
} | ||
value.EachEntry(perEntry) | ||
value.EachField(perField) | ||
} | ||
process(value) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
|
||
package slime | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func createComplexValue(t *testing.T) Value { | ||
val := FromJson("{" + | ||
"a:[17,{x:{z:[1,2]},y:[3,4,5]},['foo','bar']]," + | ||
"b:{x:[6,7,8],y:[9,10]}," + | ||
"c:11" + | ||
"}") | ||
assert.True(t, val.Valid()) | ||
return val | ||
} | ||
|
||
func TestSelector(t *testing.T) { | ||
x := createComplexValue(t) | ||
s1 := &selectField{"a"} | ||
s2 := &selectField{"y"} | ||
s3 := &selectEntry{0} | ||
s4 := &selectEntry{1} | ||
x = s1.Select(x) | ||
x = s4.Select(x) | ||
x = s2.Select(x) | ||
y := s3.Select(x) | ||
z := s4.Select(x) | ||
verifyLong(3)(t, y) | ||
verifyLong(4)(t, z) | ||
y = noSelector.Select(y) | ||
z = noSelector.Select(z) | ||
verifyLong(3)(t, y) | ||
verifyLong(4)(t, z) | ||
} | ||
|
||
func TestPath(t *testing.T) { | ||
root := createComplexValue(t) | ||
path := NewPath() | ||
path.Field("a") | ||
path.Entry(1) | ||
path.Field("y") | ||
path.Entry(0) | ||
verifyLong(3)(t, path.Apply(root)) | ||
path2 := path.Clone() | ||
assert.Equal(t, 4, path2.Len()) | ||
path2.Trim(1) | ||
assert.Equal(t, 3, path2.Len()) | ||
path2.Entry(1) | ||
verifyLong(3)(t, path.Apply(root)) | ||
verifyLong(4)(t, path2.Apply(root)) | ||
path.Trim(3) | ||
path2.Trim(100) | ||
assert.Equal(t, 1, path.Len()) | ||
assert.Equal(t, 0, path2.Len()) | ||
} | ||
|
||
func TestPathAt(t *testing.T) { | ||
path := NewPath() | ||
path.Entry(7) | ||
path.Field("foo") | ||
path.Entry(5) | ||
path.Field("bar") | ||
assert.True(t, path.At(0).WouldSelectEntry(7)) | ||
assert.False(t, path.At(0).WouldSelectEntry(5)) | ||
assert.True(t, path.At(1).WouldSelectField("foo")) | ||
assert.False(t, path.At(1).WouldSelectField("bar")) | ||
assert.True(t, path.At(2).WouldSelectEntry(5)) | ||
assert.False(t, path.At(2).WouldSelectEntry(7)) | ||
assert.True(t, path.At(3).WouldSelectField("bar")) | ||
assert.False(t, path.At(3).WouldSelectField("foo")) | ||
assert.False(t, path.At(4).WouldSelectField("")) | ||
assert.False(t, path.At(4).WouldSelectEntry(0)) | ||
assert.True(t, path.At(-1).WouldSelectField("bar")) | ||
assert.False(t, path.At(-1).WouldSelectField("foo")) | ||
assert.True(t, path.At(-2).WouldSelectEntry(5)) | ||
assert.False(t, path.At(-2).WouldSelectEntry(7)) | ||
assert.True(t, path.At(-3).WouldSelectField("foo")) | ||
assert.False(t, path.At(-3).WouldSelectField("bar")) | ||
assert.True(t, path.At(-4).WouldSelectEntry(7)) | ||
assert.False(t, path.At(-4).WouldSelectEntry(5)) | ||
assert.False(t, path.At(-5).WouldSelectField("")) | ||
assert.False(t, path.At(-5).WouldSelectEntry(0)) | ||
} | ||
|
||
func TestFindMultipleFields(t *testing.T) { | ||
root := createComplexValue(t) | ||
res := Find(root, func(path *Path, value Value) bool { | ||
return path.At(-1).WouldSelectField("y") | ||
}) | ||
arr2 := Invalid | ||
arr3 := Invalid | ||
assert.Equal(t, 2, len(res)) | ||
for _, path := range res { | ||
v := path.Apply(root) | ||
if v.NumEntries() == 2 { | ||
arr2 = v | ||
} | ||
if v.NumEntries() == 3 { | ||
arr3 = v | ||
} | ||
} | ||
verifyArray([]verifyValue{verifyLong(9), verifyLong(10)})(t, arr2) | ||
verifyArray([]verifyValue{verifyLong(3), verifyLong(4), verifyLong(5)})(t, arr3) | ||
} |