-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
45 lines (39 loc) · 1.11 KB
/
transform.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
package api
import (
"fmt"
jtd "github.com/jsontypedef/json-typedef-go"
)
func transformSchema(schema jtd.Schema) jtd.Schema {
// Perform transformations
switch schema.Form() {
case jtd.FormProperties:
// For backwards/forwards compatibility, always ignore unknown properties.
schema.AdditionalProperties = true
}
// Recursively transform any sub-schemas
for k, v := range schema.Definitions {
schema.Definitions[k] = transformSchema(v)
}
switch schema.Form() {
case jtd.FormEmpty, jtd.FormRef, jtd.FormType, jtd.FormEnum:
// no-op
case jtd.FormElements:
*schema.Elements = transformSchema(*schema.Elements)
case jtd.FormProperties:
for k, v := range schema.Properties {
schema.Properties[k] = transformSchema(v)
}
for k, v := range schema.OptionalProperties {
schema.OptionalProperties[k] = transformSchema(v)
}
case jtd.FormValues:
*schema.Values = transformSchema(*schema.Values)
case jtd.FormDiscriminator:
for k, v := range schema.Mapping {
schema.Mapping[k] = transformSchema(v)
}
default:
panic(fmt.Errorf("unexpected schema form: %s", schema.Form()))
}
return schema
}