-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
182 lines (170 loc) · 4.34 KB
/
Copy pathparse.go
File metadata and controls
182 lines (170 loc) · 4.34 KB
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
package dyfields
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
// CurrentVersion is the schema_version written by Marshal.
const CurrentVersion = 1
// Parse reads a JSON schema. Unknown keys are rejected rather than ignored: a
// typo in a rule name would otherwise silently drop the rule, and a validation
// rule that quietly does not exist is worse than a parse error.
//
// Parse only checks that the document is well-formed. Use Compile to check that
// it makes sense.
func Parse(data []byte) (Schema, error) {
return ParseReader(bytes.NewReader(data))
}
// ParseReader is Parse over a stream.
func ParseReader(r io.Reader) (Schema, error) {
var s Schema
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
dec.UseNumber()
if err := dec.Decode(&s); err != nil {
return Schema{}, fmt.Errorf("dyfields: parse schema: %w", err)
}
if err := dec.Decode(new(json.RawMessage)); err != io.EOF {
return Schema{}, fmt.Errorf("dyfields: parse schema: trailing content after the document")
}
normalizeSchema(&s)
return s, nil
}
// Load parses and compiles in one step, which is what a server wants at
// startup: either a usable schema or a reason it is unusable.
func Load(data []byte) (*Compiled, error) {
s, err := Parse(data)
if err != nil {
return nil, err
}
return Compile(s)
}
// Validate reports whether a JSON schema document has problems, without
// building anything. The returned SchemaErrors is empty when the schema is
// usable; a parse failure is reported as a single error.
func ValidateSchema(data []byte) SchemaErrors {
s, err := Parse(data)
if err != nil {
return SchemaErrors{{Code: CodeBadType, Reason: err.Error()}}
}
if _, err := Compile(s); err != nil {
if se, ok := err.(SchemaErrors); ok {
return se
}
return SchemaErrors{{Code: CodeBadType, Reason: err.Error()}}
}
return nil
}
// Marshal writes the schema as JSON.
func (s Schema) Marshal() ([]byte, error) {
if s.Version == 0 {
s.Version = CurrentVersion
}
return json.Marshal(s)
}
// MarshalIndent writes the schema as indented JSON, for schemas kept in a repo.
func (s Schema) MarshalIndent() ([]byte, error) {
if s.Version == 0 {
s.Version = CurrentVersion
}
return json.MarshalIndent(s, "", " ")
}
// normalizeSchema turns json.Number into the plain Go values the rest of the
// library compares against, and fills in defaults that are only omitted for
// brevity in hand-written JSON.
func normalizeSchema(s *Schema) {
if s.Version == 0 {
s.Version = CurrentVersion
}
for _, g := range s.Groups {
if g == nil {
continue
}
if g.Mode == "" {
g.Mode = ModeAlways
}
normalizeCondition(g.VisibleWhen)
for _, r := range g.ValidWhen {
if r != nil {
normalizeCondition(r.When)
}
}
for _, f := range g.Fields {
normalizeField(f)
}
}
}
func normalizeField(f *Field) {
if f == nil {
return
}
f.Default = normalizeJSONValue(f.Default)
for i := range f.Options {
f.Options[i].Value = normalizeJSONValue(f.Options[i].Value)
}
if f.Items != nil {
for i := range f.Items.Options {
f.Items.Options[i].Value = normalizeJSONValue(f.Items.Options[i].Value)
}
}
if f.Metadata != nil {
f.Metadata = normalizeJSONValue(f.Metadata).(map[string]any)
}
normalizeCondition(f.VisibleWhen)
normalizeCondition(f.RequiredWhen)
normalizeCondition(f.ReadOnlyWhen)
for _, r := range f.ValidWhen {
if r != nil {
normalizeCondition(r.When)
}
}
for _, sub := range f.Fields {
normalizeField(sub)
}
}
func normalizeCondition(c *Condition) {
if c == nil {
return
}
c.Equals = normalizeJSONValue(c.Equals)
for i := range c.In {
c.In[i] = normalizeJSONValue(c.In[i])
}
for i := range c.NotIn {
c.NotIn[i] = normalizeJSONValue(c.NotIn[i])
}
for _, sub := range c.AllOf {
normalizeCondition(sub)
}
for _, sub := range c.AnyOf {
normalizeCondition(sub)
}
normalizeCondition(c.Not)
}
// normalizeJSONValue converts json.Number to int64 or float64. Integers stay
// integers so that an enum option of 3 still equals a submitted 3.
func normalizeJSONValue(v any) any {
switch t := v.(type) {
case json.Number:
if i, err := t.Int64(); err == nil {
return i
}
if f, err := t.Float64(); err == nil {
return f
}
return t.String()
case map[string]any:
for k, e := range t {
t[k] = normalizeJSONValue(e)
}
return t
case []any:
for i, e := range t {
t[i] = normalizeJSONValue(e)
}
return t
default:
return v
}
}