forked from hofstadter-io/structural
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcue.go
126 lines (99 loc) · 2.05 KB
/
cue.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package structural
import (
"fmt"
"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/load"
)
type CueRuntime struct {
Entrypoints []string
CueRT *cue.Runtime
BIS []*build.Instance
FieldOpts []cue.Option
CueInstance *cue.Instance
TopLevelValue cue.Value
TopLevelStruct *cue.Struct
}
func NewCueRuntime() *CueRuntime {
return &CueRuntime{
FieldOpts: []cue.Option{
cue.Attributes(true),
cue.Concrete(false),
cue.Definitions(true),
cue.Docs(true),
cue.Hidden(true),
cue.Optional(true),
},
}
}
func (CR *CueRuntime) LoadCue(entrypoints []string) []error {
var errs []error
CR.Entrypoints = entrypoints
CR.CueRT = &cue.Runtime{}
CR.BIS = load.Instances(CR.Entrypoints, &load.Config{
Package: "",
Tools: false,
})
// fmt.Println("len(BIS):", len(CR.BIS))
// Build the Instances
I, err := CR.CueRT.Build(CR.BIS[0])
if err != nil {
es := errors.Errors(err)
// fmt.Println("BUILD ERR", es, I)
for _, e := range es {
errs = append(errs, e.(error))
}
return errs
}
CR.CueInstance = I
// Get top level value from cuelang
V := I.Value()
CR.TopLevelValue = V
// Get top level struct from cuelang
S, err := V.Struct()
if err != nil {
// fmt.Println("STRUCT ERR", err)
es := errors.Errors(err)
for _, e := range es {
errs = append(errs, e.(error))
}
return errs
}
CR.TopLevelStruct = S
return errs
}
func (CR *CueRuntime) PrintValue() error {
node := CR.TopLevelValue.Syntax(CR.FieldOpts...)
bytes, err := format.Node(
node,
format.TabIndent(false),
format.UseSpaces(2),
)
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}
func PrintCueValue(val cue.Value) error {
node := val.Syntax(
cue.Attributes(true),
cue.Concrete(false),
cue.Definitions(true),
cue.Docs(true),
cue.Hidden(true),
cue.Optional(true),
)
bytes, err := format.Node(
node,
format.TabIndent(false),
format.UseSpaces(2),
)
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}