-
Notifications
You must be signed in to change notification settings - Fork 2
/
value.go
278 lines (218 loc) · 4.08 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2013 Ostap Cherkashin. You can use this source code
// under the terms of the MIT License found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"io"
"math"
"strconv"
)
type Bool bool
type Number float64
type String string
type List []Value
type Object []Value
var True Value = Bool(true)
var False Value = Bool(false)
type Value interface {
Bool() Bool
String() String
Number() Number
List() List
Object() Object
Quote(w io.Writer, t Type) error
Equals(v Value) Bool
}
func marshal(w io.Writer, v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func (b Bool) Bool() Bool {
return b
}
func (b Bool) String() String {
if b {
return String("true")
}
return String("")
}
func (b Bool) Number() Number {
if b {
return 1.0
}
return 0.0
}
func (b Bool) List() List {
return List{b}
}
func (b Bool) Object() Object {
return Object{b}
}
func (b Bool) Quote(w io.Writer, t Type) error {
return marshal(w, bool(b))
}
func (b Bool) Equals(v Value) Bool {
return b == v.Bool()
}
func (n Number) Bool() Bool {
if math.IsNaN(float64(n)) {
return Bool(false)
}
return Bool(n != 0)
}
func (n Number) Number() Number {
return n
}
func (n Number) String() String {
return String(fmt.Sprintf("%v", n))
}
func (n Number) List() List {
return List{n}
}
func (n Number) Object() Object {
return Object{n}
}
func (n Number) Quote(w io.Writer, t Type) error {
var v interface{}
if math.IsInf(float64(n), 0) || math.IsNaN(float64(n)) {
v = fmt.Sprintf(`%v`, n)
} else {
v = float64(n)
}
return marshal(w, v)
}
func (n Number) Equals(v Value) Bool {
return n == v.Number()
}
func (s String) Bool() Bool {
return s != ""
}
func (s String) Number() Number {
res, _ := strconv.ParseFloat(string(s), 64)
return Number(res)
}
func (s String) String() String {
return s
}
func (s String) List() List {
return List{s}
}
func (s String) Object() Object {
return Object{s}
}
func (s String) Quote(w io.Writer, t Type) error {
return marshal(w, string(s))
}
func (s String) Equals(v Value) Bool {
return s == v.String()
}
func (l List) Bool() Bool {
return len(l) > 0
}
func (l List) Number() Number {
return Number(math.NaN())
}
func (l List) String() String {
return ""
}
func (l List) List() List {
return l
}
func (l List) Object() Object {
return nil
}
func (l List) Quote(w io.Writer, t Type) error {
_, err := io.WriteString(w, "[")
if err != nil {
return err
}
lt, isList := t.(ListType)
if !isList {
return fmt.Errorf("internal error: %v is not a list", t.Name())
}
for i, v := range l {
if i != 0 {
_, err = io.WriteString(w, ",")
if err != nil {
return err
}
}
if err := v.Quote(w, lt.Elem); err != nil {
return err
}
}
_, err = io.WriteString(w, "]")
return err
}
func (l List) Equals(v Value) Bool {
r := v.List()
if len(l) != len(r) {
return false
}
for i := 0; i < len(l); i++ {
if !l[i].Equals(r[i]) {
return false
}
}
return true
}
func (o Object) Bool() Bool {
return len(o) > 0
}
func (o Object) Number() Number {
return Number(math.NaN())
}
func (o Object) String() String {
return ""
}
func (o Object) List() List {
return nil
}
func (o Object) Object() Object {
return o
}
func (o Object) Quote(w io.Writer, t Type) error {
_, err := io.WriteString(w, "{")
if err != nil {
return err
}
ot, isObject := t.(ObjectType)
if !isObject {
return fmt.Errorf("internal error: %v is not an object", t.Name())
}
for i, v := range o {
if i != 0 {
_, err = io.WriteString(w, ",")
if err != nil {
return err
}
}
_, err = fmt.Fprintf(w, `%v:`, strconv.Quote(ot[i].Name))
if err != nil {
return err
}
if err := v.Quote(w, ot[i].Type); err != nil {
return err
}
}
_, err = io.WriteString(w, "}")
return err
}
func (o Object) Equals(v Value) Bool {
// FIXME: algorithm assumes the same field ordering for both objects
r := v.Object()
if len(o) != len(r) {
return false
}
for i := 0; i < len(o); i++ {
if !bool(o[i].Equals(r[i])) {
return false
}
}
return true
}