-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
189 lines (165 loc) · 3.97 KB
/
type.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package openapi
import (
"encoding/json"
"github.com/chanced/jsonx"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
const (
// TypeString = string
//
// https://json-schema.org/understanding-json-schema/reference/string.html#string
TypeString Type = "string"
// TypeNumber = number
//
// https://json-schema.org/understanding-json-schema/reference/numeric.html#number
TypeNumber Type = "number"
// TypeInteger = integer
//
// https://json-schema.org/understanding-json-schema/reference/numeric.html#integer
TypeInteger Type = "integer"
// TypeObject = object
//
// https://json-schema.org/understanding-json-schema/reference/object.html#object
TypeObject Type = "object"
// TypeArray = array
//
// https://json-schema.org/understanding-json-schema/reference/array.html#array
TypeArray Type = "array"
// TypeBoolean = boolean
//
// https://json-schema.org/understanding-json-schema/reference/boolean.html#boolean
TypeBoolean Type = "boolean"
// TypeNull = null
//
// https://json-schema.org/understanding-json-schema/reference/null.html#null
TypeNull Type = "null"
)
// Type restricts to a JSON Schema specific type
//
// https://json-schema.org/understanding-json-schema/reference/type.html#type
type Type = Text
// Types is a set of Types. A single Type marshals/unmarshals into a string
// while 2+ marshals into an array.
type Types []Type
type types Types
func (t Types) Clone() Types {
if t == nil {
return nil
}
c := make(Types, len(t))
copy(c, t)
return c
}
// ContainsString returns true if TypeString is present
func (t Types) ContainsString() bool {
return t.Contains(TypeString)
}
// ContainsNumber returns true if TypeNumber is present
func (t Types) ContainsNumber() bool {
return t.Contains(TypeNumber)
}
// ContainsInteger returns true if TypeInteger is present
func (t Types) ContainsInteger() bool {
return t.Contains(TypeInteger)
}
// ContainsObject returns true if TypeObject is present
func (t Types) ContainsObject() bool {
return t.Contains(TypeObject)
}
// ContainsArray returns true if TypeArray is present
func (t Types) ContainsArray() bool {
return t.Contains(TypeArray)
}
// ContainsBoolean returns true if TypeBoolean is present
func (t Types) ContainsBoolean() bool {
return t.Contains(TypeBoolean)
}
// ContainsNull returns true if TypeNull is present
func (t Types) ContainsNull() bool {
return t.Contains(TypeNull)
}
// IsSingle returns true if len(t) == 1
func (t Types) IsSingle() bool {
return len(t) == 1
}
// Len returns len(t)
func (t Types) Len() int {
return len(t)
}
// Contains returns true if t contains typ
func (t Types) Contains(typ Type) bool {
for _, v := range t {
if v == typ {
return true
}
}
return false
}
// Add adds typ if not present
func (t *Types) Add(typ Type) Types {
if !t.Contains(typ) {
*t = append(*t, typ)
}
return *t
}
// Remove removes typ if present
func (t *Types) Remove(typ Type) Types {
for i, v := range *t {
if typ == v {
copy((*t)[i:], (*t)[i+1:])
(*t)[len(*t)-1] = ""
*t = (*t)[:len(*t)-1]
}
}
return *t
}
// MarshalJSON marshals JSON
func (t Types) MarshalJSON() ([]byte, error) {
switch len(t) {
case 1:
return json.Marshal(t[0].String())
default:
return json.Marshal(types(t))
}
}
// UnmarshalJSON unmarshals JSON
func (t *Types) UnmarshalJSON(data []byte) error {
if jsonx.IsString(data) {
var v Type
err := json.Unmarshal(data, &v)
*t = Types{v}
return err
}
var v types
err := json.Unmarshal(data, &v)
*t = Types(v)
return err
}
func (t *Types) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, t)
}
func (t Types) MarshalYAML() (interface{}, error) {
j, err := t.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
var (
_ yaml.Unmarshaler = (*Types)(nil)
_ yaml.Marshaler = (Types)(nil)
)