-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.go
176 lines (158 loc) · 4.8 KB
/
utils.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 json
import (
"errors"
"fmt"
"strings"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func exploreSchema(jsonSchema *jsonschema.Schema) map[string]*jsonschema.Schema {
baseLocation := jsonSchema.Location
exploredSchemas := make(map[string]*jsonschema.Schema, 10)
explore(jsonSchema, exploredSchemas, baseLocation)
return exploredSchemas
}
func explore(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
_, ok := locationSchemaMap[jsonSchema.Location]
if ok {
return //already explored
}
if !strings.HasPrefix(jsonSchema.Location, baseLocation) {
return //remote reference
}
locationSchemaMap[jsonSchema.Location] = jsonSchema // marking visited
exploreRef(jsonSchema, locationSchemaMap, baseLocation)
exploreAllOf(jsonSchema, locationSchemaMap, baseLocation)
exploreOneOf(jsonSchema, locationSchemaMap, baseLocation)
exploreAnyOf(jsonSchema, locationSchemaMap, baseLocation)
exploreProperties(jsonSchema, locationSchemaMap, baseLocation)
exploreItems(jsonSchema, locationSchemaMap, baseLocation)
exploreAdditionalItems(jsonSchema, locationSchemaMap, baseLocation)
}
func exploreItems(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
itemSchemas := getItems(jsonSchema)
for _, itemSchema := range itemSchemas {
explore(itemSchema, locationSchemaMap, baseLocation)
}
}
func exploreAdditionalItems(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
itemSchema := getRestOfItemsSchema(jsonSchema)
if itemSchema != nil {
explore(itemSchema, locationSchemaMap, baseLocation)
}
}
func exploreProperties(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
if jsonSchema.Properties == nil || len(jsonSchema.Properties) == 0 {
return
}
for _, schema := range jsonSchema.Properties {
explore(schema, locationSchemaMap, baseLocation)
}
}
func exploreAnyOf(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
if jsonSchema.AnyOf == nil || len(jsonSchema.AnyOf) == 0 {
return
}
for _, schema := range jsonSchema.AnyOf {
explore(schema, locationSchemaMap, baseLocation)
}
}
func exploreOneOf(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
if jsonSchema.OneOf == nil || len(jsonSchema.OneOf) == 0 {
return
}
for _, schema := range jsonSchema.OneOf {
explore(schema, locationSchemaMap, baseLocation)
}
}
func exploreAllOf(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
if jsonSchema.AllOf == nil || len(jsonSchema.AllOf) == 0 {
return
}
for _, schema := range jsonSchema.AllOf {
explore(schema, locationSchemaMap, baseLocation)
}
}
func exploreRef(jsonSchema *jsonschema.Schema, locationSchemaMap map[string]*jsonschema.Schema, baseLocation string) {
if jsonSchema.Ref == nil {
return
}
explore(jsonSchema.Ref, locationSchemaMap, baseLocation)
}
func elementsMatch[K comparable](arr1, arr2 []K) error {
if len(arr1) != len(arr2) {
return errors.New("count of elements do not match")
}
for _, element := range arr1 {
found := false
for _, element2 := range arr2 {
if element == element2 {
found = true
break
}
}
if !found {
return fmt.Errorf("%v element not found in second array", element)
}
}
return nil
}
func getKeys(properties map[string]*jsonschema.Schema) []string {
slice := make([]string, len(properties))
for key := range properties {
slice = append(slice, key)
}
return slice
}
func getDiffernce[K comparable](arr, toBeSubtracted []K) []K {
slice := make([]K, 1)
for _, element := range arr {
if !contains(toBeSubtracted, element) {
slice = append(slice, element)
}
}
return slice
}
func isSubset[K comparable](superSet, subSetCandidate []K) bool {
for _, val := range subSetCandidate {
if !contains(superSet, val) {
return false
}
}
return true
}
func contains[K comparable](haystack []K, needle K) bool {
for _, val := range haystack {
if val == needle {
return true
}
}
return false
}
func getItems(jsSchema *jsonschema.Schema) []*jsonschema.Schema {
if jsSchema.Draft == jsonschema.Draft2020 {
if jsSchema.PrefixItems != nil {
return jsSchema.PrefixItems
}
} else {
schemaArr, ok := jsSchema.Items.([]*jsonschema.Schema)
if ok {
return schemaArr
}
schema, ok := jsSchema.Items.(*jsonschema.Schema)
if ok {
return []*jsonschema.Schema{schema}
}
}
return []*jsonschema.Schema{}
}
func getRestOfItemsSchema(jsSchema *jsonschema.Schema) *jsonschema.Schema {
if jsSchema.Draft == jsonschema.Draft2020 {
return jsSchema.Items2020
} else {
schema, ok := jsSchema.AdditionalItems.(*jsonschema.Schema)
if !ok {
return nil
}
return schema
}
}