-
Notifications
You must be signed in to change notification settings - Fork 19
/
extensions.go
177 lines (155 loc) · 3.99 KB
/
extensions.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
package spec3
import (
"encoding/json"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
// Extensions vendor specific extensions
type Extensions struct {
data OrderedMap
}
func (e Extensions) Set(key string, value interface{}) bool {
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
return e.data.Set(key, value)
}
// Add adds a value to these extensions
func (e Extensions) Add(key string, value interface{}) {
e.Set(key, value)
}
func (e Extensions) GetOK(key string) (interface{}, bool) {
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
return e.data.GetOK(key)
}
func (e Extensions) Get(key string) interface{} {
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
return e.data.Get(key)
}
// GetString gets a string value from the extensions
func (e Extensions) GetString(key string) (string, bool) {
if v, ok := e.GetOK(key); ok {
str, ok := v.(string)
return str, ok
}
return "", false
}
// GetBool gets a boolean value from the extensions
func (e Extensions) GetBool(key string) (bool, bool) {
if v, ok := e.GetOK(key); ok {
str, ok := v.(bool)
return str, ok
}
return false, false
}
// GetInt gets an int value from the extensions
func (e Extensions) GetInt(key string) (int, bool) {
if v, ok := e.GetOK(key); ok {
switch res := v.(type) {
case int:
return res, ok
case int8:
return int(res), ok
case int16:
return int(res), ok
case int32:
return int(res), ok
case int64:
return int(res), ok
default:
return 0, ok
}
}
return 0, false
}
// GetInt32 gets an int32 value from the extensions
func (e Extensions) GetInt32(key string) (int32, bool) {
if v, ok := e.GetOK(key); ok {
str, ok := v.(int32)
return str, ok
}
return 0, false
}
// GetInt64 gets an int64 value from the extensions
func (e Extensions) GetInt64(key string) (int64, bool) {
if v, ok := e.GetOK(key); ok {
str, ok := v.(int64)
return str, ok
}
return 0, false
}
// GetStringSlice gets a string value from the extensions
func (e Extensions) GetStringSlice(key string) ([]string, bool) {
if v, ok := e.GetOK(key); ok {
strv, ok := v.([]string) // get out quick
if ok {
return strv, ok
}
// do the thing
arr, ok := v.([]interface{})
if !ok {
return nil, false
}
var strs []string
for _, iface := range arr {
str, ok := iface.(string)
if !ok {
return nil, false
}
strs = append(strs, str)
}
return strs, ok
}
return nil, false
}
// MarshalJSON supports json.Marshaler interface
func (e Extensions) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
encodeSortedMap(&w, e.data)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (e Extensions) MarshalEasyJSON(w *jwriter.Writer) {
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
encodeSortedMap(w, e.data)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (e *Extensions) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
decodeSortedMap(&r, &e.data)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (e *Extensions) UnmarshalEasyJSON(l *jlexer.Lexer) {
e.data.filter = MatchExtension
e.data.normalize = LowerCaseKeys
decodeSortedMap(l, &e.data)
}
// VendorExtensible composition block.
type VendorExtensible struct {
Extensions Extensions
}
// AddExtension adds an extension to this extensible object
func (v *VendorExtensible) AddExtension(key string, value interface{}) {
if value == nil {
return
}
v.Extensions.Add(key, value)
}
// MarshalJSON marshals the extensions to json
func (v VendorExtensible) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Extensions)
}
// UnmarshalJSON for this extensible object
func (v *VendorExtensible) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &v.Extensions); err != nil {
return err
}
return nil
}