forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
126 lines (108 loc) · 2.75 KB
/
schema.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
package diff
import (
"fmt"
"strings"
"github.com/go-openapi/spec"
)
func getTypeFromSchema(schema *spec.Schema) (typeName string, isArray bool) {
refStr := definitionFromRef(schema.Ref)
if len(refStr) > 0 {
return refStr, false
}
typeName = schema.Type[0]
if typeName == ArrayType {
typeName, _ = getSchemaType(&schema.Items.Schema.SchemaProps)
return typeName, true
}
return typeName, false
}
func getTypeFromSimpleSchema(schema *spec.SimpleSchema) (typeName string, isArray bool) {
typeName = schema.Type
format := schema.Format
if len(format) > 0 {
typeName = fmt.Sprintf("%s.%s", typeName, format)
}
if typeName == ArrayType {
typeName, _ = getSchemaType(&schema.Items.SimpleSchema)
return typeName, true
}
return typeName, false
}
func getTypeFromSchemaProps(schema *spec.SchemaProps) (typeName string, isArray bool) {
refStr := definitionFromRef(schema.Ref)
if len(refStr) > 0 {
return refStr, false
}
if len(schema.Type) > 0 {
typeName = schema.Type[0]
format := schema.Format
if len(format) > 0 {
typeName = fmt.Sprintf("%s.%s", typeName, format)
}
if typeName == ArrayType {
typeName, _ = getSchemaType(&schema.Items.Schema.SchemaProps)
return typeName, true
}
}
return typeName, false
}
func getSchemaTypeStr(item interface{}) string {
typeStr, isArray := getSchemaType(item)
return formatTypeString(typeStr, isArray)
}
func getSchemaType(item interface{}) (typeName string, isArray bool) {
switch s := item.(type) {
case *spec.Schema:
typeName, isArray = getTypeFromSchema(s)
case *spec.SchemaProps:
typeName, isArray = getTypeFromSchemaProps(s)
case spec.SchemaProps:
typeName, isArray = getTypeFromSchemaProps(&s)
case spec.SimpleSchema:
typeName, isArray = getTypeFromSimpleSchema(&s)
case *spec.SimpleSchema:
typeName, isArray = getTypeFromSimpleSchema(s)
default:
typeName = "unknown"
}
return
}
func formatTypeString(typ string, isarray bool) string {
if isarray {
return fmt.Sprintf("<array[%s]>", typ)
}
return fmt.Sprintf("<%s>", typ)
}
func definitionFromRef(ref spec.Ref) string {
url := ref.GetURL()
if url == nil {
return ""
}
fragmentParts := strings.Split(url.Fragment, "/")
numParts := len(fragmentParts)
return fragmentParts[numParts-1]
}
func isArray(item interface{}) bool {
switch s := item.(type) {
case *spec.Schema:
return isArrayType(s.Type)
case *spec.SchemaProps:
return isArrayType(s.Type)
case *spec.SimpleSchema:
return isArrayType(spec.StringOrArray{s.Type})
default:
return false
}
}
func isPrimitive(item interface{}) bool {
switch s := item.(type) {
case *spec.Schema:
return isPrimitiveType(s.Type)
case *spec.SchemaProps:
return isPrimitiveType(s.Type)
case spec.StringOrArray:
return isPrimitiveType(s)
default:
return false
}
}