-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
189 lines (166 loc) · 4.16 KB
/
value.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package event
import (
"bytes"
"encoding/json"
"fmt"
)
var NullValue = &Value{Type: NullType}
type ValueType uint8
const (
NullType ValueType = iota
ArrayType
BoolType
FloatType
IntegerType
ObjectType
StringType
TimestampType
UnsignedIntegerType
maxValueType
)
var valueTypeNames = map[ValueType]string{
NullType: "null_type",
ArrayType: "array_type",
BoolType: "bool_type",
FloatType: "float_type",
IntegerType: "integer_type",
ObjectType: "object_type",
StringType: "string_type",
TimestampType: "timestamp_type",
UnsignedIntegerType: "unsigned_integer_type",
}
func (vt ValueType) String() string {
if name, found := valueTypeNames[vt]; found {
return name
}
return "unknown_type"
}
type Value struct {
Integer int64
UnsignedInteger uint64
Float float64
Timestamp Time
String string
Array []*Value
Object map[string]*Value
Bool bool
Type ValueType
}
func (v *Value) value() interface{} {
switch v.Type {
case ArrayType:
return v.Array
case BoolType:
return v.Bool
case FloatType:
return v.Float
case IntegerType:
return v.Integer
case ObjectType:
return v.Object
case StringType:
return v.String
case TimestampType:
return v.Timestamp
case UnsignedIntegerType:
return v.UnsignedInteger
case NullType:
return nil
default:
return nil
}
}
func (v *Value) Format(f fmt.State, verb rune) {
// Not implementing String() because it collides with the String field.
f.Write([]byte(v.Type.String()))
f.Write([]byte(":"))
fmt.Fprintf(f, "%v", v.value())
}
func (v *Value) MarshalJSON() ([]byte, error) {
// TODO: Replace this with an optimized version. There are a finite
// set of types so we can optimize this to avoid reflection.
return json.Marshal(v.value())
}
func (v *Value) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
tok, err := dec.Token()
if err != nil {
return err
}
switch tok {
case json.Delim('['):
if err = json.Unmarshal(data, &v.Array); err != nil {
return err
}
v.Type = ArrayType
case json.Delim('{'):
if err = json.Unmarshal(data, &v.Object); err != nil {
return err
}
v.Type = ObjectType
default:
var value interface{}
if err = json.Unmarshal(data, &value); err != nil {
return err
}
switch x := value.(type) {
case string:
v.Type = StringType
v.String = x
case float64:
v.Type = FloatType
v.Float = x
case int64:
v.Type = IntegerType
v.Integer = x
case bool:
v.Type = BoolType
v.Bool = x
case nil:
v.Type = NullType
default:
return fmt.Errorf("unhandled type %T for value %q", x, string(data))
}
}
return nil
}
func Bool(v bool) *Value {
return &Value{Type: BoolType, Bool: v}
}
func Integer(v int64) *Value {
return &Value{Type: IntegerType, Integer: v}
}
func UnsignedInteger(v uint64) *Value {
return &Value{Type: UnsignedIntegerType, UnsignedInteger: v}
}
func Float(v float64) *Value {
return &Value{Type: FloatType, Float: v}
}
func String(v string) *Value {
return &Value{Type: StringType, String: v}
}
func Timestamp(unixNanos int64) *Value {
return &Value{Type: TimestampType, Timestamp: Time{unixNanos}}
}
func Array(v ...*Value) *Value {
return &Value{Type: ArrayType, Array: v}
}
func Object(v map[string]*Value) *Value {
return &Value{Type: ObjectType, Object: v}
}