-
Notifications
You must be signed in to change notification settings - Fork 9
/
intent.go
301 lines (237 loc) · 8.53 KB
/
intent.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package parsing
import (
"encoding/json"
"io/fs"
"reflect"
"unicode/utf8"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema"
)
type parseContext struct {
FS fs.FS
PackageName schema.PackageName
EnsurePackage func(schema.PackageName) error
}
func allocateValue(pctx parseContext, parent protoreflect.Message, field protoreflect.FieldDescriptor, value any) (protoreflect.Value, error) {
if field.IsMap() {
return protoreflect.Value{}, fnerrors.New("maps not supported")
}
if field.IsList() {
list := parent.NewField(field).Interface().(protoreflect.List)
switch x := value.(type) {
case []any:
for k, v := range x {
allocated, err := allocateSingleValue(pctx, field, v)
if err != nil {
return protoreflect.Value{}, fnerrors.New("[%d]: %w", k, err)
}
list.Append(allocated)
}
return protoreflect.ValueOfList(list), nil
}
return protoreflect.Value{}, fnerrors.New("expected []any, got %s", reflect.TypeOf(value).String())
}
return allocateSingleValue(pctx, field, value)
}
func allocateSingleValue(pctx parseContext, field protoreflect.FieldDescriptor, value any) (protoreflect.Value, error) {
switch field.Kind() {
case protoreflect.BoolKind:
switch x := value.(type) {
case bool:
return protoreflect.ValueOfBool(x), nil
}
return protoreflect.Value{}, fnerrors.New("expected bool, got %s", reflect.TypeOf(value).String())
case protoreflect.DoubleKind,
protoreflect.FloatKind:
switch x := value.(type) {
case float32, float64:
return protoreflect.ValueOf(x), nil
case json.Number:
f, err := x.Float64()
if err != nil {
return protoreflect.Value{}, fnerrors.New("failed to parse json number %v as float: %w", x, err)
}
return protoreflect.ValueOf(f), nil
}
return protoreflect.Value{}, fnerrors.New("expected float, got %s", reflect.TypeOf(value).String())
case protoreflect.Int32Kind,
protoreflect.Fixed32Kind,
protoreflect.Uint32Kind,
protoreflect.Sfixed32Kind,
protoreflect.Sint32Kind:
switch x := value.(type) {
case int32, uint, uint32:
return protoreflect.ValueOf(x), nil
case json.Number:
n, err := x.Int64()
if err != nil {
return protoreflect.Value{}, fnerrors.New("failed to parse json number %v as integer: %w", x, err)
}
return protoreflect.ValueOf(int32(n)), nil
}
return protoreflect.Value{}, fnerrors.New("expected int32 or uint32, got %s", reflect.TypeOf(value).String())
case protoreflect.Int64Kind,
protoreflect.Fixed64Kind,
protoreflect.Uint64Kind,
protoreflect.Sfixed64Kind,
protoreflect.Sint64Kind:
switch x := value.(type) {
case int32, int64, uint, uint32, uint64:
return protoreflect.ValueOf(x), nil
case json.Number:
n, err := x.Int64()
if err != nil {
return protoreflect.Value{}, fnerrors.New("failed to parse json number %v as integer: %w", x, err)
}
return protoreflect.ValueOf(n), nil
}
return protoreflect.Value{}, fnerrors.New("expected int64 or uint64, got %s", reflect.TypeOf(value).String())
case protoreflect.StringKind:
switch x := value.(type) {
case string:
return protoreflect.ValueOf(x), nil
}
return protoreflect.Value{}, fnerrors.New("expected string, got %s", reflect.TypeOf(value).String())
case protoreflect.BytesKind:
switch x := value.(type) {
case []byte:
return protoreflect.ValueOf(x), nil
}
return protoreflect.Value{}, fnerrors.New("expected bytes, got %s", reflect.TypeOf(value).String())
case protoreflect.EnumKind:
switch x := value.(type) {
case string:
fieldValue := field.Enum().Values().ByName(protoreflect.Name(x))
if fieldValue == nil {
return protoreflect.Value{}, fnerrors.New("unknown enum value %s", x)
}
return protoreflect.ValueOfEnum(fieldValue.Number()), nil
case int32:
fieldValue := field.Enum().Values().ByNumber(protoreflect.EnumNumber(x))
if fieldValue == nil {
return protoreflect.Value{}, fnerrors.New("unknown enum value %v", x)
}
return protoreflect.ValueOfEnum(fieldValue.Number()), nil
case json.Number:
n, err := x.Int64()
if err != nil {
return protoreflect.Value{}, fnerrors.New("failed to parse json number %v as integer: %w", x, err)
}
fieldValue := field.Enum().Values().ByNumber(protoreflect.EnumNumber(n))
if fieldValue == nil {
return protoreflect.Value{}, fnerrors.New("unknown enum value %v", x)
}
return protoreflect.ValueOfEnum(fieldValue.Number()), nil
}
return protoreflect.Value{}, fnerrors.New("expected string or int32, got %s", reflect.TypeOf(value).String())
case protoreflect.MessageKind:
msg, err := allocateWellKnownMessage(pctx, field.Message(), value)
if err != nil {
return protoreflect.Value{}, err
}
return protoreflect.ValueOfMessage(msg.ProtoReflect()), nil
default:
return protoreflect.Value{}, fnerrors.New("kind not supported: %v", field.Kind())
}
}
func allocateWellKnownMessage(pctx parseContext, messageType protoreflect.MessageDescriptor, value any) (protoreflect.ProtoMessage, error) {
// Handle well-known types.
switch messageType.FullName() {
case "foundation.schema.FileContents":
return allocateFileContents(pctx, value)
case "foundation.schema.PackageRef":
pkgref, err := allocatePackageRef(pctx, messageType, value)
if err != nil {
return nil, err
}
if ref, ok := pkgref.(*schema.PackageRef); ok {
if pctx.EnsurePackage != nil {
if err := pctx.EnsurePackage(ref.AsPackageName()); err != nil {
return nil, err
}
}
} else {
return nil, fnerrors.InternalError("expected package ref parsing to yield a schema.PackageRef")
}
return pkgref, nil
}
return allocateDynamicMessage(pctx, messageType, value)
}
func allocateDynamicMessage(pctx parseContext, messageType protoreflect.MessageDescriptor, value any) (protoreflect.ProtoMessage, error) {
msg := dynamicpb.NewMessage(messageType)
return allocateMessage(pctx, msg, value)
}
func allocateMessage(pctx parseContext, msg protoreflect.Message, value any) (protoreflect.ProtoMessage, error) {
switch x := value.(type) {
case map[string]interface{}:
fields := msg.Descriptor().Fields()
for key, v := range x {
f := fields.ByJSONName(key)
if f == nil {
f = fields.ByName(protoreflect.Name(key))
}
if f == nil {
return nil, fnerrors.New("{%s}.%q: no such field", msg.Descriptor().FullName(), key)
}
newValue, err := allocateValue(pctx, msg, f, v)
if err != nil {
return nil, fnerrors.New("{%s}.%q: %w", msg.Descriptor().FullName(), key, err)
}
msg.Set(f, newValue)
}
return msg.Interface(), nil
}
return nil, fnerrors.New("%s: expected map, got %s", msg.Descriptor().FullName(), reflect.TypeOf(value).String())
}
func allocatePackageRef(pctx parseContext, messageType protoreflect.MessageDescriptor, value any) (protoreflect.ProtoMessage, error) {
switch x := value.(type) {
case string:
if pctx.PackageName == "" {
return nil, fnerrors.InternalError("failed to handle package ref, missing package name")
}
return schema.ParsePackageRef(pctx.PackageName, x)
}
return allocateMessage(pctx, (&schema.PackageRef{}).ProtoReflect(), value)
}
func allocateFileContents(pctx parseContext, value any) (protoreflect.ProtoMessage, error) {
if pctx.FS == nil {
return nil, fnerrors.New("failed to handle resource, no workspace access available")
}
switch x := value.(type) {
case string:
contents, err := fs.ReadFile(pctx.FS, x)
if err != nil {
return nil, fnerrors.New("failed to load %q: %w", x, err)
}
return &schema.FileContents{
Utf8: utf8.Valid(contents),
Contents: contents,
}, nil
case map[string]interface{}:
keys := maps.Keys(x)
if len(keys) != 1 {
return nil, fnerrors.New("failed to handle inline resource, expected single-key map")
}
switch keys[0] {
case "inline":
switch y := x[keys[0]].(type) {
case string:
return &schema.FileContents{
Utf8: true,
Contents: []byte(y),
}, nil
case []byte:
return &schema.FileContents{Contents: y}, nil
}
return nil, fnerrors.New("failed to handle inline resource, got %s", reflect.TypeOf(x[keys[0]]).String())
}
return nil, fnerrors.New("failed to handle inline resource, expected %q got %q", "inline", keys[0])
}
return nil, fnerrors.New("failed to handle resource type, got %s", reflect.TypeOf(value).String())
}