This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
178 lines (155 loc) · 2.9 KB
/
common.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package structural
import (
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
)
var r cue.Runtime
type pvStruct struct {
sl *ast.StructLit
}
type pvList struct {
ll *ast.ListLit
}
type pvExpr struct {
e *ast.Expr
}
func NewpvStruct() *pvStruct {
return &pvStruct{
sl: ast.NewStruct(),
}
}
func NewpvList() *pvList {
return &pvList{
ll: ast.NewList(),
}
}
func ExprFromValue(v cue.Value) *pvExpr {
bytes, err := format.Node(v.Syntax())
if err != nil {
panic(err)
}
expr, err := parser.ParseExpr("", bytes)
if err != nil {
panic(err)
}
return &pvExpr{
e: &expr,
}
}
func (pv pvStruct) Get(key string) *pvStruct {
for _, d := range pv.sl.Elts {
df := d.(*ast.Field)
label, _, _ := ast.LabelName(df.Label)
if label == key {
return &pvStruct{
sl: df.Value.(*ast.StructLit),
}
}
}
return nil
}
func (pv *pvStruct) Ensure(key string) {
pvS := pv.Get(key)
if pvS == nil {
var e ast.Expr = ast.NewStruct()
pv.Set(key, pvExpr{
e: &e,
})
}
}
func (pv *pvStruct) Set(key string, expr pvExpr) {
found := false
for _, d := range pv.sl.Elts {
df := d.(*ast.Field)
label, _, _ := ast.LabelName(df.Label)
if label == key {
df.Value = *expr.e
found = true
break
}
}
if !found {
newField := &ast.Field{Label: ast.NewIdent(key), Value: *expr.e}
pv.sl.Elts = append(pv.sl.Elts, newField)
}
}
func (pv *pvList) Append(expr pvExpr) {
pv.ll.Elts = append(pv.ll.Elts, *expr.e)
}
func (pv *pvStruct) ToValue() (*cue.Value, error) {
i, err := r.CompileExpr(pv.sl)
if err != nil {
return nil, err
}
v := i.Value()
if err = v.Err(); err != nil {
return nil, err
}
return &v, nil
}
func (pv *pvList) ToValue() (*cue.Value, error) {
i, err := r.CompileExpr(pv.ll)
if err != nil {
return nil, err
}
v := i.Value()
if err = v.Err(); err != nil {
return nil, err
}
return &v, nil
}
func (pv *pvStruct) ToString() (string, error) {
v, err := pv.ToValue()
if err != nil {
return "", nil
}
bytes, err := format.Node(v.Syntax())
if err != nil {
return "", nil
}
return strings.TrimSpace(string(bytes)), nil
}
func (pv *pvList) ToString() (string, error) {
v, err := pv.ToValue()
if err != nil {
return "", nil
}
bytes, err := format.Node(v.Syntax())
if err != nil {
return "", nil
}
return strings.TrimSpace(string(bytes)), nil
}
func (pv *pvStruct) ToExpr() *pvExpr {
var e ast.Expr = pv.sl
return &pvExpr{
e: &e,
}
}
func (pv *pvList) ToExpr() *pvExpr {
var e ast.Expr = pv.ll
return &pvExpr{
e: &e,
}
}
//////////
func isStruct(val cue.Value) bool {
k := val.Kind()
return k == cue.StructKind
}
func isList(val cue.Value) bool {
k := val.Kind()
return k == cue.ListKind
}
func isBuiltin(val cue.Value) bool {
k := val.Kind()
return k == cue.NullKind ||
k == cue.BoolKind ||
k == cue.IntKind ||
k == cue.FloatKind ||
k == cue.StringKind ||
k == cue.BytesKind
}