-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_object.go
151 lines (124 loc) · 2.91 KB
/
type_object.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
package goschema
import "sort"
type objectType struct {
baseType
props map[string]Type
optional map[string]bool
}
type objectAttribute struct {
object *objectType
name string
}
func NewObjectType(description string, each func(ObjectType)) ObjectType {
gop := &objectType{
baseType: baseType{
description: description,
},
props: map[string]Type{},
optional: map[string]bool{},
}
each(gop)
return gop
}
func (g *objectType) asJSONSchema() map[string]interface{} {
data := map[string]interface{}{}
data["title"] = g.description
data["type"] = "object"
g.addProperties(data)
return data
}
func (g *objectType) addProperties(data map[string]interface{}) {
props := map[string]interface{}{}
required := []string{}
for name, value := range g.props {
props[name] = value.asJSONSchema()
if !g.optional[name] {
required = append(required, name)
}
}
data["properties"] = props
data["additionalProperties"] = false
if len(required) > 0 {
data["required"] = required
}
}
func (g *objectType) docString(field string, docPrefix string) string {
result := ""
result += docString(field, g.description, docPrefix, "object")
keys := []string{}
for key := range g.props {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
opt := ""
if g.optional[key] {
opt = "optional"
}
result += g.props[key].docString(field+"."+key, opt)
}
return result
}
func (g *objectType) Attribute(name string) ObjectAttribute {
return &objectAttribute{
object: g,
name: name,
}
}
func (g *objectType) Optional(name string) ObjectAttribute {
g.optional[name] = true
return &objectAttribute{
object: g,
name: name,
}
}
func (g *objectAttribute) Enum(desc string) EnumType {
t := NewEnumType(desc)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) Int(desc string) IntType {
t := NewIntType(desc)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) Bool(desc string) BoolType {
t := NewBoolType(desc)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) String(desc string) StringType {
t := NewStringType(desc)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) Object(desc string, ops func(ObjectType)) ObjectType {
t := NewObjectType(desc, ops)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) Map(ops func(MapType)) MapType {
t := NewMapType("", ops)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) List(ops func(ListType)) ListType {
t := NewListType(ops)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) Any(description string) AnyType {
t := NewAnyType(description)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) Null(description string) NullType {
t := NewNullType(description)
g.object.props[g.name] = t
return t
}
func (g *objectAttribute) SomeOf(ops func(SomeOf)) SomeOf {
t := NewSomeOf(ops)
g.object.props[g.name] = t
return t
}