/
type_adapters.go
163 lines (148 loc) · 4.13 KB
/
type_adapters.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
package diff
import (
"github.com/go-openapi/spec"
)
func forItems(items *spec.Items) *spec.Schema {
if items == nil {
return nil
}
valids := items.CommonValidations
schema := spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{items.SimpleSchema.Type},
Format: items.SimpleSchema.Format,
Maximum: valids.Maximum,
ExclusiveMaximum: valids.ExclusiveMaximum,
Minimum: valids.Minimum,
ExclusiveMinimum: valids.ExclusiveMinimum,
MaxLength: valids.MaxLength,
MinLength: valids.MinLength,
Pattern: valids.Pattern,
MaxItems: valids.MaxItems,
MinItems: valids.MinItems,
UniqueItems: valids.UniqueItems,
MultipleOf: valids.MultipleOf,
Enum: valids.Enum,
},
}
return &schema
}
func forHeader(header spec.Header) *spec.SchemaProps {
return &spec.SchemaProps{
Type: []string{header.Type},
Format: header.Format,
Items: &spec.SchemaOrArray{Schema: forItems(header.Items)},
Maximum: header.Maximum,
ExclusiveMaximum: header.ExclusiveMaximum,
Minimum: header.Minimum,
ExclusiveMinimum: header.ExclusiveMinimum,
MaxLength: header.MaxLength,
MinLength: header.MinLength,
Pattern: header.Pattern,
MaxItems: header.MaxItems,
MinItems: header.MinItems,
UniqueItems: header.UniqueItems,
MultipleOf: header.MultipleOf,
Enum: header.Enum,
}
}
func forParam(param spec.Parameter) *spec.SchemaProps {
return &spec.SchemaProps{
Type: []string{param.Type},
Format: param.Format,
Items: &spec.SchemaOrArray{Schema: forItems(param.Items)},
Maximum: param.Maximum,
ExclusiveMaximum: param.ExclusiveMaximum,
Minimum: param.Minimum,
ExclusiveMinimum: param.ExclusiveMinimum,
MaxLength: param.MaxLength,
MinLength: param.MinLength,
Pattern: param.Pattern,
MaxItems: param.MaxItems,
MinItems: param.MinItems,
UniqueItems: param.UniqueItems,
MultipleOf: param.MultipleOf,
Enum: param.Enum,
}
}
// OperationMap saves indexing operations in PathItems individually
type OperationMap map[string]*spec.Operation
func toMap(item *spec.PathItem) OperationMap {
m := make(OperationMap)
if item.Post != nil {
m["post"] = item.Post
}
if item.Get != nil {
m["get"] = item.Get
}
if item.Put != nil {
m["put"] = item.Put
}
if item.Patch != nil {
m["patch"] = item.Patch
}
if item.Head != nil {
m["head"] = item.Head
}
if item.Options != nil {
m["options"] = item.Options
}
if item.Delete != nil {
m["delete"] = item.Delete
}
return m
}
func getURLMethodsFor(spec *spec.Swagger) URLMethods {
returnURLMethods := URLMethods{}
for url, eachPath := range spec.Paths.Paths {
eachPath := eachPath
opsMap := toMap(&eachPath)
for method, op := range opsMap {
returnURLMethods[URLMethod{url, method}] = &PathItemOp{&eachPath, op}
}
}
return returnURLMethods
}
func isStringType(typeName string) bool {
return typeName == "string" || typeName == "password"
}
// SchemaFromRefFn define this to get a schema for a ref
type SchemaFromRefFn func(spec.Ref) (*spec.Schema, string)
func propertiesFor(schema *spec.Schema, getRefFn SchemaFromRefFn) PropertyMap {
if isRefType(schema) {
schema, _ = getRefFn(schema.Ref)
}
props := PropertyMap{}
requiredProps := schema.Required
requiredMap := map[string]bool{}
for _, prop := range requiredProps {
requiredMap[prop] = true
}
if schema.Properties != nil {
for name, prop := range schema.Properties {
prop := prop
required := requiredMap[name]
props[name] = PropertyDefn{Schema: &prop, Required: required}
}
}
for _, e := range schema.AllOf {
eachAllOf := e
allOfMap := propertiesFor(&eachAllOf, getRefFn)
for name, prop := range allOfMap {
props[name] = prop
}
}
return props
}
func getRef(item interface{}) spec.Ref {
switch s := item.(type) {
case *spec.Refable:
return s.Ref
case *spec.Schema:
return s.Ref
case *spec.SchemaProps:
return s.Ref
default:
return spec.Ref{}
}
}