-
Notifications
You must be signed in to change notification settings - Fork 9
/
parsing.go
152 lines (119 loc) · 3.76 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
// 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"
"namespacelabs.dev/foundation/framework/rpcerrors"
)
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(resource, 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", resource)
}
// 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 map[string]interface{}:
if child, ok := x[fieldName]; ok {
if child == nil {
return nil, rpcerrors.Errorf(codes.NotFound, "%s: %s: no value set", resource, originalSel)
}
return selectField(resource, originalSel, child, left)
}
return nil, rpcerrors.Errorf(codes.NotFound, "%s: %s: selector doesn't match a value", resource, originalSel)
}
return nil, rpcerrors.Errorf(codes.InvalidArgument, "%s: resource is of type %q, not supported", resource, reflect.TypeOf(value).String())
}
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
}