/
untyped.go
209 lines (179 loc) · 4.03 KB
/
untyped.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
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package schema
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"strconv"
"strings"
)
type Untyped struct {
Metadata
Types []string `json:"type,omitempty"`
}
func (u *Untyped) AssignValue(_ map[string]string, valueName, resultName string) string {
return fmt.Sprintf("%s = %s", resultName, valueName)
}
func (u *Untyped) CompareValues(v1, v2 interface{}) int {
switch {
case reflect.DeepEqual(v1, v2):
return 0
default:
return -1
}
}
func (u *Untyped) Copy() Schema {
j, err := json.Marshal(u)
if err != nil {
panic(fmt.Errorf("failed to encode schema: %w", err))
}
cp := &Untyped{}
if err := json.Unmarshal(j, cp); err != nil {
panic(fmt.Errorf("failed to decode schema: %w", err))
}
return cp
}
func (u *Untyped) DefaultValue() interface{} {
return nil
}
func (u *Untyped) GoString(imports map[string]string) string {
buf := bytes.NewBuffer(nil)
buf.WriteString("&schema.Untyped{\n")
if !reflect.ValueOf(u.Metadata).IsZero() {
_, _ = fmt.Fprintf(buf, "\tMetadata: %s,\n", indent(u.Metadata.GoString(imports)))
}
if len(u.Types) > 0 {
_, _ = fmt.Fprintf(buf, "\tTypes: %#v,\n", u.Types)
}
buf.WriteRune('}')
return buf.String()
}
func (u *Untyped) GoType(_ map[string]string) string {
return "interface{}"
}
func (u *Untyped) StringValue(value interface{}) string {
switch v := value.(type) {
case nil:
return "null"
case bool:
return strconv.FormatBool(v)
case int64:
return strconv.FormatInt(v, 10)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case string:
return strconv.Quote(v)
case []interface{}:
if len(v) == 0 {
return "[]"
}
var sb strings.Builder
sb.WriteRune('[')
sb.WriteString(u.StringValue(v[0]))
for _, e := range v[1:] {
sb.WriteString(", ")
sb.WriteString(u.StringValue(e))
}
sb.WriteRune(']')
return sb.String()
case map[string]interface{}:
if len(v) == 0 {
return "{}"
}
keys := getSortedMapKeys(v)
var sb strings.Builder
sb.WriteRune('{')
sb.WriteString(keys[0])
sb.WriteString(": ")
sb.WriteString(u.StringValue(v[keys[0]]))
for _, k := range keys[1:] {
sb.WriteString(", ")
sb.WriteString(k)
sb.WriteString(": ")
sb.WriteString(u.StringValue(v[k]))
}
sb.WriteRune('}')
return sb.String()
case io.Reader:
return "<byte stream>"
case fmt.Stringer:
return v.String()
default:
return fmt.Sprintf("<%T>", v)
}
}
func (u *Untyped) Type() Type {
return TypeUntyped
}
func (u *Untyped) TypeString() string {
return "untyped"
}
func (u *Untyped) ValidateSchema(s Schema, compare bool) error {
if len(u.Types) == 0 {
return nil
}
isValid := false
for _, t := range u.Types {
if err := typeSchemas[Type(t)].ValidateSchema(s, compare); err == nil {
isValid = true
break
}
}
if !isValid {
if len(u.Types) == 1 {
return fmt.Errorf("was expecting %s", u.Types[0])
}
return fmt.Errorf(
"was expecting %s or %s",
strings.Join(u.Types[0:len(u.Types)-1], ", "),
u.Types[len(u.Types)-1],
)
}
return nil
}
func (u *Untyped) ValidateValue(v interface{}) (interface{}, error) {
if len(u.Types) == 0 {
return v, nil
}
isValid := false
for _, t := range u.Types {
nv, err := typeSchemas[Type(t)].ValidateValue(v)
if err == nil {
v = nv
isValid = true
break
}
}
if !isValid {
if len(u.Types) == 1 {
return nil, fmt.Errorf("was expecting %s", u.Types[0])
}
return nil, fmt.Errorf(
"was expecting %s or %s",
strings.Join(u.Types[0:len(u.Types)-1], ", "),
u.Types[len(u.Types)-1],
)
}
return v, nil
}
func UntypedValue() Schema {
return untypedValueInst
}
var untypedValueInst = &untypedValue{
Untyped: &Untyped{},
}
type untypedValue struct {
*Untyped
}
func (u *untypedValue) Copy() Schema {
return untypedValueInst
}
func (u *untypedValue) GoString(map[string]string) string {
return "schema.UntypedValue()"
}