-
Notifications
You must be signed in to change notification settings - Fork 9
/
parsing.go
198 lines (156 loc) · 5.22 KB
/
parsing.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
// 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 resources
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"namespacelabs.dev/foundation/framework/rpcerrors"
"namespacelabs.dev/foundation/internal/fnerrors"
)
type Parsed struct {
resources map[string]any
}
func LoadResources() (*Parsed, error) {
configBytes, err := os.ReadFile("/namespace/config/resources.json")
if err != nil {
return nil, fmt.Errorf("failed to unwrap resource configuration: %w", err)
}
return ParseResourceData(configBytes)
}
func ParseResourceData(data []byte) (*Parsed, error) {
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
tok, err := dec.Token()
if err == nil && tok != json.Delim('{') {
err = fmt.Errorf("expected an object, got %v", tok)
}
if err != nil {
return nil, fmt.Errorf("bad resource data: %w", err)
}
resources := map[string]any{}
for dec.More() {
label, err := dec.Token()
if err != nil {
return nil, fmt.Errorf("bad resource data: %w", err)
}
strLabel, ok := label.(string)
if !ok {
return nil, fmt.Errorf("bad resource data, expected label: %w", err)
}
var raw any
if err := dec.Decode(&raw); err != nil {
return nil, fmt.Errorf("bad resource data: failed to decode: %w", err)
}
resources[strLabel] = raw
}
finalTok, err := dec.Token()
if err == nil && finalTok != json.Delim('}') {
err = fmt.Errorf("expected object closure, got %v", finalTok)
}
if err != nil {
return nil, fmt.Errorf("bad resource data: %w", err)
}
return &Parsed{resources: resources}, nil
}
// SelectField traverses the specified resource, if one exists, and obtains the
// value specified by the field selector.
func (p *Parsed) SelectField(resource, field string) (any, error) {
raw, ok := p.resources[resource]
if !ok {
return nil, rpcerrors.Errorf(codes.NotFound, "no instance found for resource %q", resource)
}
return selectField(resource, field, raw, field)
}
func SelectField(description string, value any, field string) (any, error) {
return selectField(description, field, value, field)
}
func selectField(description, originalSel string, value any, field string) (any, error) {
if field == "" {
switch x := value.(type) {
// Hack! Guess the primitive number type.
case json.Number:
if n, err := x.Int64(); err == nil {
return n, nil
}
return x.Float64()
default:
return value, nil
}
}
p := strings.SplitN(field, ".", 2)
fieldName := p[0]
left := ""
if len(p) > 1 {
left = p[1]
}
if field == "" {
return nil, rpcerrors.Errorf(codes.InvalidArgument, "%s: invalid field selector", description)
}
// XXX this field parsing is fairly simple, it only traverses maps. We should also:
// - support arbitrary keys (e.g. with dots)
// - support indexing.
switch x := value.(type) {
case proto.Message:
return selectProtoField(description, originalSel, x.ProtoReflect(), fieldName, left)
case protoreflect.Message:
return selectProtoField(description, originalSel, x, fieldName, left)
case map[string]interface{}:
if child, ok := x[fieldName]; ok {
if child == nil {
return nil, rpcerrors.Errorf(codes.NotFound, "%s: %s: no value set", description, originalSel)
}
return selectField(description, originalSel, child, left)
}
return nil, rpcerrors.Errorf(codes.NotFound, "%s: %s: selector doesn't match a value", description, originalSel)
}
return nil, rpcerrors.Errorf(codes.InvalidArgument, "%s: resource is of type %q, not supported", description, reflect.TypeOf(value).String())
}
func selectProtoField(description, originalSel string, m protoreflect.Message, fieldName, left string) (any, error) {
desc := m.Descriptor().(protoreflect.MessageDescriptor)
field := byProtoOrJsonName(desc.Fields(), fieldName)
if field == nil {
return nil, rpcerrors.Errorf(codes.NotFound, "%s: %s: selector doesn't match a value", description, originalSel)
}
return selectField(description, originalSel, m.Get(field).Interface(), left)
}
func byProtoOrJsonName(r protoreflect.FieldDescriptors, name string) protoreflect.FieldDescriptor {
if field := r.ByTextName(name); field != nil {
return field
}
if field := r.ByJSONName(name); field != nil {
return field
}
return nil
}
func (p *Parsed) Unmarshal(resource string, out any) error {
raw, ok := p.resources[resource]
if !ok {
return rpcerrors.Errorf(codes.NotFound, "no resource config found for resource %q", resource)
}
data, err := json.Marshal(raw)
if err != nil {
return rpcerrors.Errorf(codes.Internal, "%s: failed to re-marshal value: %w", resource, err)
}
if err := json.Unmarshal(data, out); err != nil {
return rpcerrors.Errorf(codes.Internal, "%s: failed to unmarshal resource: %w", resource, err)
}
return nil
}
func CoerceAsString(v any) (string, error) {
switch x := v.(type) {
case string:
return x, nil
case int32, int64, uint32, uint64, int:
return fmt.Sprintf("%d", x), nil
default:
return "", fnerrors.BadInputError("unsupported resource field value %q", reflect.TypeOf(v).String())
}
}