-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsonschema.go
176 lines (157 loc) · 4.23 KB
/
jsonschema.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
package ast
import (
"math/big"
"github.com/dipdup-net/go-lib/tools/types"
)
// Schema types
const (
JSONSchemaTypeInt = "integer"
JSONSchemaTypeString = "string"
JSONSchemaTypeBool = "boolean"
JSONSchemaTypeArray = "array"
JSONSchemaTypeObject = "object"
)
// JSONModel -
type JSONModel map[string]interface{}
// JSONSchema -
type JSONSchema struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Prim string `json:"prim,omitempty"`
Tag string `json:"tag,omitempty"`
Format string `json:"format,omitempty"`
Default interface{} `json:"default,omitempty"`
MinLength int `json:"minLength,omitempty"`
MaxLength int `json:"maxLength,omitempty"`
Properties map[string]*JSONSchema `json:"properties,omitempty"`
OneOf []*JSONSchema `json:"oneOf,omitempty"`
Required []string `json:"required,omitempty"`
XItemTitle string `json:"x-itemTitle,omitempty"`
Const string `json:"const,omitempty"`
SchemaKey *SchemaKey `json:"schemaKey,omitempty"`
Items *SchemaKey `json:"items,omitempty"`
XOptions map[string]interface{} `json:"x-options,omitempty"`
}
// SchemaKey -
type SchemaKey JSONSchema
func getStringJSONSchema(d Default) *JSONSchema {
return wrapObject(&JSONSchema{
Prim: d.Prim,
Type: JSONSchemaTypeString,
Default: "",
Title: d.GetName(),
})
}
func getIntJSONSchema(d Default) *JSONSchema {
return wrapObject(&JSONSchema{
Prim: d.Prim,
Type: JSONSchemaTypeInt,
Default: 0,
Title: d.GetName(),
})
}
func getAddressJSONSchema(d Default) *JSONSchema {
return wrapObject(&JSONSchema{
Prim: d.Prim,
Type: JSONSchemaTypeString,
MinLength: 36,
MaxLength: 36,
Default: "",
Title: d.GetName(),
})
}
func setIntJSONSchema(d *Default, data map[string]interface{}) {
for key := range data {
if key == d.GetName() {
switch v := data[key].(type) {
case float64:
i := big.NewInt(0)
i, _ = big.NewFloat(v).Int(i)
d.Value = &types.BigInt{Int: i}
case string:
d.Value = types.NewBigIntFromString(v)
}
d.ValueKind = valueKindInt
break
}
}
}
func setBytesJSONSchema(d *Default, data map[string]interface{}) {
for key := range data {
if key == d.GetName() {
d.Value = data[key]
d.ValueKind = valueKindBytes
break
}
}
}
func setOptimizedJSONSchema(d *Default, data map[string]interface{}, optimizer func(string) (string, error)) {
for key, value := range data {
if key == d.GetName() {
val, err := optimizer(value.(string))
if err != nil {
val = value.(string)
}
d.ValueKind = valueKindString
d.Value = val
break
}
}
}
type mergeFields struct {
reqs []string
xTitle string
}
func mergePropertiesMap(src, dest map[string]*JSONSchema, required, needXTitle bool) *mergeFields {
fields := mergeFields{}
if required {
fields.reqs = make([]string, 0)
}
for key, value := range src {
dest[key] = value
if required {
fields.reqs = append(fields.reqs, key)
}
if needXTitle {
fields.xTitle = key
}
}
return &fields
}
func setChildSchemaForMap(child Node, needXTitle bool, parent *JSONSchema) error {
s, err := child.ToJSONSchema()
if err != nil {
return err
}
if len(s.Properties) > 0 {
if parent.Items.Properties == nil {
parent.Items.Properties = make(map[string]*JSONSchema)
}
if parent.Items.Required == nil {
parent.Items.Required = make([]string, 0)
}
fields := mergePropertiesMap(s.Properties, parent.Items.Properties, true, needXTitle)
parent.Items.Required = append(parent.Items.Required, fields.reqs...)
if fields.xTitle != "" {
parent.XItemTitle = fields.xTitle
}
} else {
parent.Items.Properties[child.GetName()] = s
}
return nil
}
func wrapObject(schema *JSONSchema) *JSONSchema {
return &JSONSchema{
Type: JSONSchemaTypeObject,
Properties: map[string]*JSONSchema{
schema.Title: schema,
},
}
}
// WrapEntrypointJSONSchema -
func WrapEntrypointJSONSchema(schema *JSONSchema) *JSONSchema {
if schema == nil || schema.Type != JSONSchemaTypeObject {
return wrapObject(schema)
}
return schema
}