/
metadata.go
222 lines (183 loc) · 5.04 KB
/
metadata.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package metadata
import (
"encoding/json"
"fmt"
"github.com/mailru/easyjson"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
type (
// Metadata is an immutable map[string]interface{} implementation
Metadata interface {
// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
Value(key string) interface{}
// AsMap return the Metadata as a map[string]interface{}
AsMap() map[string]interface{}
}
// jsonMarshaler is an interface used to speed up the json marshalling process
jsonMarshaler interface {
// MarshalJSONKeyValue writes the Metadata Key and Value and all it's parent Key Value pairs into the writer
// If last is true it MUST omit the `,` from the last Key Value pair
MarshalJSONKeyValue(out *jwriter.Writer, last bool)
}
)
// New return a new Metadata instance without any information
func New() Metadata {
return new(emptyData)
}
// FromMap returns a new Metadata instance filled with the map data
func FromMap(data map[string]interface{}) Metadata {
meta := New()
for k, v := range data {
meta = WithValue(meta, k, v)
}
return meta
}
// WithValue returns a copy of parent in which the value associated with key is val.
func WithValue(parent Metadata, key string, val interface{}) Metadata {
return &valueData{parent, key, val}
}
// emptyData represents the empty root of a metadata chain
type emptyData int
var (
// Ensure emptyData implements the Metadata interface
_ Metadata = new(emptyData)
// Ensure valueData implements the jsonMarshaler interface
_ jsonMarshaler = new(emptyData)
// Ensure emptyData implements the json.Marshaler interface
_ json.Marshaler = new(emptyData)
// Ensure emptyData implements the easyjson.Marshaler interface
_ easyjson.Marshaler = new(emptyData)
)
func (*emptyData) Value(key string) interface{} {
return nil
}
func (*emptyData) AsMap() map[string]interface{} {
return map[string]interface{}{}
}
func (v *emptyData) MarshalJSON() ([]byte, error) {
return []byte("{}"), nil
}
func (v *emptyData) MarshalEasyJSON(w *jwriter.Writer) {
w.RawByte('{')
w.RawByte('}')
}
func (v *emptyData) MarshalJSONKeyValue(*jwriter.Writer, bool) {
}
// valueData represents a key, value pair in a metadata chain
type valueData struct {
Metadata
key string
val interface{}
}
var (
// Ensure valueData implements the Metadata interface
_ Metadata = new(valueData)
// Ensure valueData implements the jsonMarshaler interface
_ jsonMarshaler = new(valueData)
// Ensure valueData implements the json.Marshaler interface
_ json.Marshaler = new(valueData)
// Ensure valueData implements the easyjson.Marshaler interface
_ easyjson.Marshaler = new(valueData)
)
func (v *valueData) Value(key string) interface{} {
if v.key == key {
return v.val
}
return v.Metadata.Value(key)
}
func (v *valueData) AsMap() map[string]interface{} {
var m map[string]interface{}
if v.Metadata == nil {
m = map[string]interface{}{}
} else {
m = v.Metadata.AsMap()
}
m[v.key] = v.val
return m
}
func (v *valueData) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
}
func (v *valueData) MarshalEasyJSON(w *jwriter.Writer) {
w.RawByte('{')
v.MarshalJSONKeyValue(w, true)
w.RawByte('}')
}
func (v *valueData) MarshalJSONKeyValue(out *jwriter.Writer, last bool) {
marshalJSONKeyValues(out, v.Metadata)
out.String(v.key)
out.RawByte(':')
if vm, ok := v.val.(easyjson.Marshaler); ok {
vm.MarshalEasyJSON(out)
} else if vm, ok := v.val.(json.Marshaler); ok {
out.Raw(vm.MarshalJSON())
} else {
out.Raw(json.Marshal(v.val))
}
if !last {
out.RawByte(',')
}
}
// UnmarshalJSON unmarshals the provided json into a Metadata instance
func UnmarshalJSON(json []byte) (Metadata, error) {
in := jlexer.Lexer{Data: json}
metadata := New()
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return metadata, in.Error()
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
metadata = WithValue(metadata, key, in.Interface())
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
return metadata, in.Error()
}
func marshalJSONKeyValues(out *jwriter.Writer, parent Metadata) {
if parent == nil {
return
}
if m, ok := parent.(jsonMarshaler); ok {
m.MarshalJSONKeyValue(out, false)
return
}
var (
parentJSON []byte
err error
)
if vm, ok := parent.(easyjson.Marshaler); ok {
w := &jwriter.Writer{}
vm.MarshalEasyJSON(out)
parentJSON = w.Buffer.BuildBytes()
err = w.Error
} else if vm, ok := parent.(json.Marshaler); ok {
parentJSON, err = vm.MarshalJSON()
} else {
parentJSON, err = json.Marshal(parent)
}
if err != nil {
out.Raw(parentJSON, err)
return
}
plen := len(parentJSON)
if parentJSON[1] != '{' || parentJSON[plen-1] != '}' {
out.Raw(parentJSON, fmt.Errorf("JSON unmarshal failed for Metadata of type %T", parent))
return
}
out.Raw(parentJSON[1:plen-2], nil)
}