/
cue.go
302 lines (283 loc) · 8.18 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package hz
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"time"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/format"
"github.com/google/uuid"
"github.com/verifa/horizon/pkg/internal/managedfields"
)
func cueSpecFromObject(cCtx *cue.Context, obj Objecter) (cue.Value, error) {
cueVal := cCtx.CompileString("{}")
kindPath := cue.ParsePath("kind")
apiVersionPath := cue.ParsePath("apiVersion")
// Use regular expressions for exact match string for kind, group and
// apiVersion.
// This is a limitatin of CUE, see this issue:
// https://github.com/cue-lang/cue/issues/740
kindExpr := cCtx.CompileString(fmt.Sprintf("=~\"^%s$\"", obj.ObjectKind()))
if err := kindExpr.Err(); err != nil {
return cue.Value{}, fmt.Errorf("compiling kind expression: %w", err)
}
apiVersionExpr := cCtx.CompileString(
fmt.Sprintf("=~\"^%s/%s$\"", obj.ObjectGroup(), obj.ObjectVersion()),
)
if err := apiVersionExpr.Err(); err != nil {
return cue.Value{}, fmt.Errorf(
"compiling apiVersion expression: %w",
err,
)
}
t := reflect.TypeOf(obj)
cueObj, err := cueEncodeStruct(cCtx, t)
if err != nil {
return cue.Value{}, fmt.Errorf("encoding struct: %w", err)
}
objDef := cueVal.FillPath(apiVersionPath, apiVersionExpr).
FillPath(kindPath, kindExpr).
Unify(cueObj)
// We need a cue definition to validate that there are not extra fields in
// the object.
// To create a definition in Go, the only hacky approach I found was to
// fill a definition path with the object and then lookup that path
// afterwards.
defPath := cue.MakePath(cue.Def(obj.ObjectKind()))
cueDef := cCtx.CompileString("").
FillPath(defPath, objDef).LookupPath(defPath)
if cueDef.Err() != nil {
return cue.Value{}, fmt.Errorf(
"compiling cue definition: %w",
cueDef.Err(),
)
}
return cueDef, nil
}
func cueEncodeStruct(cCtx *cue.Context, t reflect.Type) (cue.Value, error) {
if t.Kind() == reflect.Ptr {
return cueEncodeStruct(cCtx, t.Elem())
}
if t.Kind() != reflect.Struct {
return cue.Value{}, errors.New("value must be a struct")
}
// Handle special case of struct with one field which is embedded, like
// [Time] in the ObjectMeta struct.
if t.NumField() == 1 && t.Field(0).Anonymous {
return cueEncodeStruct(cCtx, t.Field(0).Type)
}
val := cCtx.CompileString("{}")
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// Skip fields that are not exported as they won't be JSON marshalled
// anyway.
if !field.IsExported() {
continue
}
fieldType := field.Type
if fieldType.Kind() == reflect.Pointer {
fieldType = fieldType.Elem()
}
fieldPath, ok := cueFieldPath(field)
if !ok {
continue
}
// If the field is embedded / annoymous, we need to honour any JSON
// tags, otherwise we unify it with it's parent.
if field.Anonymous {
jTag, ok := field.Tag.Lookup("json")
// If no json tag, or a json tag with an empty name.
if !ok || strings.Split(jTag, ",")[0] == "" {
embedVal, err := cueEncodeStruct(cCtx, fieldType)
if err != nil {
return cue.Value{}, fmt.Errorf(
"encoding embedded struct %q: %w",
field.Name,
err,
)
}
val = val.Unify(embedVal)
continue
}
}
fieldExpr, err := cueEncodeField(cCtx, fieldType)
if err != nil {
return cue.Value{}, fmt.Errorf(
"encoding field %q: %w",
field.Name,
err,
)
}
fieldVal := fieldExpr
cTag, ok := field.Tag.Lookup("cue")
if ok {
parts := strings.Split(cTag, ",")
if parts[0] != "" {
cueExpr := cCtx.CompileString(parts[0])
if err := cueExpr.Err(); err != nil {
return cue.Value{}, fmt.Errorf(
"compiling cue tag %q: %w",
cTag,
err,
)
}
fieldVal = fieldVal.Unify(cueExpr)
}
}
val = val.FillPath(fieldPath, fieldVal)
}
return val, nil
}
func cueEncodeField(
cCtx *cue.Context,
fieldType reflect.Type,
) (cue.Value, error) {
if fieldType.Kind() == reflect.Ptr {
return cueEncodeField(cCtx, fieldType.Elem())
}
// Handle special types.
iVal := reflect.New(fieldType).Elem().Interface()
switch iVal.(type) {
case Time, time.Time:
// This was the best attempt at getting formatting for time, but it
// involves importing stuff and complicated things a lot right now.
// importTime := ast.NewImport(nil, "time")
// vtime := &ast.Ident{Name: "time", Node: importTime}
// return cCtx.BuildExpr(ast.NewCall(ast.NewSel(vtime, "Format"),
// ast.NewSel(vtime, "RFC3339"))), nil
//
// So we just go with string for now.
return cCtx.BuildExpr(ast.NewIdent("string")), nil
case json.RawMessage:
// Use lattice type for raw message.
return cCtx.BuildExpr(ast.NewIdent("_")), nil
case uuid.UUID:
return cCtx.BuildExpr(ast.NewIdent("string")), nil
case managedfields.FieldsV1:
// Use lattice type for fieldsv1 because it is recursive.
// Would be nice to "solve" recursion in the future...
return cCtx.BuildExpr(ast.NewIdent("_")), nil
}
switch fieldType.Kind() {
case reflect.Struct:
return cueEncodeStruct(cCtx, fieldType)
// Had an error treating arrays differently when generating the OpenAPI
// spec, from cue. So just treat them like slices... sigh.
// case reflect.Array:
// // An array in Go, `[3]string` becomes simply `[string, string, string]`
// // in CUE.
// // So just add the field element to the list as many times as the array
// // is long.
// elem := fieldType.Elem()
// elemVal, err := cueEncodeField(cCtx, field, elem)
// if err != nil {
// return cue.Value{}, err
// }
// vals := make([]cue.Value, fieldType.Len())
// for i := 0; i < fieldType.Len(); i++ {
// vals[i] = elemVal
// }
// return cCtx.NewList(vals...), nil
case reflect.Slice, reflect.Array:
elem := fieldType.Elem()
elemVal, err := cueEncodeField(cCtx, elem)
if err != nil {
return cue.Value{}, err
}
// This is a bit hacky.
// We need to add the ellipsis (...) in front of the element of the
// list.
// There seems to be no simple way of doing this without using the
// ast.Ellipsis{} struct, but that requires working with ast.Expr, which
// is a bit low-level and involved.
// Hence, we just manually create [...<bytes>], where <bytes> is the raw
// bytes of the element. We then compile that back into a cue.Value.
// Not lovely, not efficient, but practical.
node := elemVal.Syntax()
b, err := format.Node(node)
if err != nil {
return cue.Value{}, err
}
buf := bytes.Buffer{}
buf.WriteByte('[')
buf.Write([]byte("..."))
buf.Write(b)
buf.WriteByte(']')
listVal := cCtx.CompileBytes(buf.Bytes())
return listVal, nil
case reflect.Map:
iVal := reflect.New(fieldType).Elem().Interface()
// Encode type returns an or expression like:
// *null | {
// [string]: string
// }
// We don't care about the null, and the other value is a struct, so
// take the first struct value.
mapVal := cCtx.EncodeType(iVal)
op, vals := mapVal.Expr()
if op != cue.OrOp {
return cue.Value{}, fmt.Errorf(
"encoding map: expected or expression, got %s",
op,
)
}
for _, val := range vals {
if val.IncompleteKind() == cue.StructKind {
return val, nil
}
}
return cue.Value{}, errors.New(
"using cue to encode the map did not produce a struct value",
)
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.Bool,
reflect.String:
iVal := reflect.New(fieldType).Elem().Interface()
return cCtx.EncodeType(iVal), nil
case reflect.Interface:
return cue.Value{}, errors.New("interface type not supported")
default:
return cue.Value{}, fmt.Errorf(
"unsupported type %s",
fieldType.Kind(),
)
}
}
func cueFieldPath(field reflect.StructField) (cue.Path, bool) {
fieldName := field.Name
isRequired := false
jTag, ok := field.Tag.Lookup("json")
if ok {
fieldName = strings.Split(jTag, ",")[0]
}
cTag, ok := field.Tag.Lookup("cue")
if ok {
parts := strings.Split(cTag, ",")
if len(parts) == 1 || parts[1] != "opt" {
isRequired = true
}
if cTag == "-" {
return cue.Path{}, false
}
}
fieldPath := cue.ParsePath(fieldName)
if !isRequired {
fieldPath = fieldPath.Optional()
}
return fieldPath, true
}