Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions find_attrs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package structural

import (
"cuelang.org/go/cue"
)

var xx struct{}

// FindByAttrs finds values in s if
// 1) attrs : it has @attr(...)
// 2) attrsK : it has @attr(key,...) [it must have all keys in the passed list, not just one]
// 3) attrsKV : it has @attr(key=value,...) [it must have all key=value in the passed map, not just one of them]
func FindByAttrs(s cue.Value, attrs []string, attrsK map[string][]string, attrsKV map[string]map[string]string) ([]cue.Value, error) {
out := make([]cue.Value, 0)

siter, err := s.Fields()
if err != nil {
return nil, err
}

attrsSet := make(map[string]struct{})
for _, a := range attrs {
attrsSet[a] = xx
}

for siter.Next() {
// label := siter.Label()
value := siter.Value()
attrs := value.Attributes()
for k, attr := range attrs {
if _, ok := attrsSet[k]; ok {
out = append(out, value)
break
}
if ks, ok := attrsK[k]; ok {
include := true
for _, checkKey := range ks {
// TODO API is lacking here, assume we have less than 20 attribute val positions...
found := false
for i := 0; i < 20; i++ {
key, err := attr.String(i)
if err == nil && key == checkKey {
found = true
break
}
}
if !found {
include = false
break
}
}
if include {
out = append(out, value)
break
}
}
if kvs, ok := attrsKV[k]; ok {
include := true
for checkKey, checkVal := range kvs {
val, found, err := attr.Lookup(0, checkKey)
if !found || err != nil || val != checkVal {
include = false
break
}
}
if include {
out = append(out, value)
break
}
}
}
}

return out, nil
}
52 changes: 52 additions & 0 deletions find_attrs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package structural_test

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/hofstadter-io/structural"

"cuelang.org/go/cue"
)

type FindAttrsTestSuite struct {
suite.Suite

findAttrsRT *structural.CueRuntime
}

func (suit *FindAttrsTestSuite) TestFindAttrs() {
var r cue.Runtime
input := `
a: 2
b: _ @findme(hide=true) // 1
b: 2
c: 2
d: _ @findme(hide=false) // 2
d: 3
x: _ @andme() // 3
xx: _ @check(this=isthis) // 4
xx1: _ @check(a,this=isthis) // 5
xxy: _ @check(this=notthat)
xxz: _ @check(this)
abc: _ @hasthis(needs,these) // 6
abd: _ @hasthis(a,needs,these) // 7
abb: _ @hasthis(eeds,hese)
`
i, err := r.Compile("", input)
assert.Nil(suit.T(), err)
v := i.Value()
assert.Nil(suit.T(), v.Err())
vs, err := structural.FindByAttrs(v,
[]string{"findme", "andme"},
map[string][]string{
"hasthis": []string{"needs", "these"},
},
map[string]map[string]string{
"check": map[string]string{
"this": "isthis",
},
})
assert.Nil(suit.T(), err)
assert.Len(suit.T(), vs, 7)
}
1 change: 1 addition & 0 deletions structural_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestStructuralTestSuite(t *testing.T) {
suite.Run(t, new(MaskTestSuite))
suite.Run(t, new(PickTestSuite))
suite.Run(t, new(QueryTestSuite))
suite.Run(t, new(FindAttrsTestSuite))

}

Expand Down