-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
128 lines (99 loc) · 2.54 KB
/
model.go
File metadata and controls
128 lines (99 loc) · 2.54 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
package mason
import (
"encoding/json"
"fmt"
"strings"
"github.com/swaggest/jsonschema-go"
"github.com/tailbits/mason/model"
)
var _ jsonschema.Exposer = (*Model)(nil)
type Model struct {
jsonschema.Struct
model.WithSchema
}
func (m Model) IsNil() bool {
_, isNil := m.WithSchema.(model.Nil)
return isNil
}
func (m Model) JSONSchema() (jsonschema.Schema, error) {
if m.Schema() == nil {
return jsonschema.Schema{}, nil
}
var sch jsonschema.Schema
if err := json.Unmarshal(m.Schema(), &sch); err != nil {
return jsonschema.Schema{}, fmt.Errorf("error unmarshalling schema for %s: %w", m.Name(), err)
}
ex := make(map[string]interface{})
if err := json.Unmarshal(m.Example(), &ex); err != nil {
return jsonschema.Schema{}, fmt.Errorf("error unmarshalling example for %s : %w", m.Name(), err)
}
sch.WithExamples(ex)
walkRefs(&sch, func(ref *string) {
refID := strings.ReplaceAll(*ref, "#/definitions/", "#/components/schemas/")
refID = strings.TrimPrefix(refID, "#/components/schemas/")
*ref = "#/components/schemas/" + refID
})
return sch, nil
}
func NewModel(ent model.WithSchema) Model {
m := Model{
Struct: jsonschema.Struct{
DefName: ent.Name(),
},
WithSchema: ent,
}
return m
}
// =============================================================================
func walkRefs(schema *jsonschema.Schema, f func(*string)) {
if schema == nil {
return
}
apply := func(sch *jsonschema.Schema) {
if sch.Ref != nil {
f(sch.Ref)
}
}
walkSchema(&jsonschema.SchemaOrBool{TypeObject: schema}, apply)
for _, def := range schema.Definitions {
walkSchema(&def, apply)
}
}
func walkSchema(schemaOrBool *jsonschema.SchemaOrBool, f func(*jsonschema.Schema)) {
if schemaOrBool == nil || schemaOrBool.TypeObject == nil {
return
}
schema := schemaOrBool.TypeObject
f(schema)
walkSchema(schema.AdditionalItems, f)
if schema.Items != nil && schema.Items.SchemaArray != nil {
items := schema.Items.SchemaArray
for _, item := range items {
walkSchema(&item, f)
}
}
if schema.Items != nil && schema.Items.SchemaOrBool != nil {
walkSchema(schema.Items.SchemaOrBool, f)
}
walkSchema(schema.Contains, f)
walkSchema(schema.AdditionalProperties, f)
for _, prop := range schema.Properties {
walkSchema(&prop, f)
}
if schema.AllOf != nil {
for _, s := range schema.AllOf {
walkSchema(&s, f)
}
}
if schema.AnyOf != nil {
for _, s := range schema.AnyOf {
walkSchema(&s, f)
}
}
if schema.OneOf != nil {
for _, s := range schema.OneOf {
walkSchema(&s, f)
}
}
walkSchema(schema.Not, f)
}