forked from hofstadter-io/structural
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
75 lines (65 loc) · 1.31 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package structural
import (
"cuelang.org/go/cue"
)
func CueQuery(squery, sdata string) (string, error) {
out := NewpvList()
vqueryi, err := r.Compile("", squery)
if err != nil {
return "", err
}
vquery := vqueryi.Value()
if vquery.Err() != nil {
return "", vquery.Err()
}
vdatai, err := r.Compile("", sdata)
if err != nil {
return "", err
}
vdata := vdatai.Value()
if vdata.Err() != nil {
return "", vdata.Err()
}
err = cueQuery(out, vquery, vdata)
if err != nil {
return "", err
}
return out.ToString()
}
func cueQuery(out *pvList, vquery, vdata cue.Value) error {
// Loop over the query args in vquery
queryListIter, err := vquery.List()
if err != nil {
return err
}
var list = []cue.Value{vdata}
for queryListIter.Next() {
queryVal := queryListIter.Value()
list, err = queryStep(queryVal, list)
if err != nil {
return err
}
}
for _, v := range list {
out.Append(*ExprFromValue(v))
}
return nil
}
func queryStep(query cue.Value, list []cue.Value) ([]cue.Value, error) {
out := make([]cue.Value, 0)
for _, v := range list {
if isStruct(v) {
viter, err := v.Fields()
if err != nil {
return out, err
}
for viter.Next() {
val := viter.Value()
if query.Unify(val).Kind() != cue.BottomKind {
out = append(out, val)
}
}
}
}
return out, nil
}