-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcollectjs_types.go
288 lines (262 loc) · 5.86 KB
/
collectjs_types.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
279
280
281
282
283
284
285
286
287
288
/*
Copyright 2021 The tKeel Authors.
Licensed 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 tdtl
import (
"fmt"
"github.com/tkeel-io/tdtl/pkg/json/gjson"
"github.com/tkeel-io/tdtl/pkg/json/jsonparser"
"strconv"
"strings"
)
type MapHandle func(key []byte, value *Collect) Node
type ForeachHandle func(key []byte, value *Collect)
type SortHandle func(p1 *Collect, p2 *Collect) bool
var (
UNDEFINED_RESULT = &JSONNode{datatype: Undefined}
NULL_RESULT = &JSONNode{datatype: Undefined}
)
// result represents a json value that is returned from Get().
type Result = gjson.Result
// Type node type
type Type int
const (
// Undefine is Not a value
// This isn't explicitly representable in JSON except by omitting the value.
Undefined Type = iota
// Null is a null json value
Null
// Bool is a json boolean
Bool
// Number is json number, include Int and Float
Number
// Int is json number, a discrete Int
Int
// Float is json number
Float
// String is a json string
String
// JSON is a raw block of JSON
JSON
// Object is a type of JSON
Object
// Array is a type of JSON
Array
)
// String returns a string representation of the type.
func (t Type) String() string {
switch t {
default:
return "Undefined"
case Null:
return "Null"
case Bool:
return "Bool"
case Int:
return "Int"
case Float:
return "Float"
case String:
return "String"
case JSON:
return "JSON"
}
}
var jsonparserDatetype = map[jsonparser.ValueType]Type{
jsonparser.NotExist: Null,
jsonparser.String: String,
jsonparser.Number: Number,
jsonparser.Object: Object,
jsonparser.Array: Array,
jsonparser.Boolean: Bool,
jsonparser.Null: Null,
jsonparser.Unknown: Null,
}
var gjsonDatetype = map[gjson.Type]Type{
gjson.Null: Null,
gjson.Number: Number,
gjson.String: String,
gjson.True: Bool,
gjson.False: Bool,
gjson.JSON: JSON,
}
func datetype(data interface{}) Type {
switch data := data.(type) {
case jsonparser.ValueType:
return jsonparserDatetype[data]
case gjson.Result:
typ := gjsonDatetype[data.Type]
if typ == JSON {
if data.IsArray() {
return Array
}
if data.IsObject() {
return Object
}
}
return typ
}
return Null
}
// True is a json true boolean
// JSON is a raw block of JSON
//Node interface
type Node interface {
Type() Type
To(Type) Node
Raw() []byte
String() string
Error() error
}
type BoolNode bool
func (r BoolNode) Type() Type { return Bool }
func (r BoolNode) Error() error { return nil }
func (r BoolNode) To(typ Type) Node {
switch typ {
case Bool:
return r
case String:
return StringNode(fmt.Sprintf("%t", r))
}
return UNDEFINED_RESULT
}
func (r BoolNode) Raw() []byte {
return []byte(r.String())
}
func (r BoolNode) String() string {
return fmt.Sprintf("%t", r)
}
type IntNode int64
func (r IntNode) Type() Type { return Int }
func (r IntNode) Error() error { return nil }
func (r IntNode) To(typ Type) Node {
switch typ {
case Number, Int:
return r
case Float:
return FloatNode(r)
case String:
return StringNode(fmt.Sprintf("%d", r))
}
return UNDEFINED_RESULT
}
func (r IntNode) Raw() []byte {
return []byte(r.String())
}
func (r IntNode) String() string {
return fmt.Sprintf("%d", r)
}
type FloatNode float64
func (r FloatNode) Type() Type { return Float }
func (r FloatNode) Error() error { return nil }
func (r FloatNode) To(typ Type) Node {
switch typ {
case Number, Float:
return r
case Int:
return IntNode(r)
case String:
return StringNode(fmt.Sprintf("%f", r))
}
return UNDEFINED_RESULT
}
func (r FloatNode) Raw() []byte {
return []byte(r.String())
}
func (r FloatNode) String() string {
return fmt.Sprintf("%.6f", r)
}
type StringNode string
func (r StringNode) Type() Type { return String }
func (r StringNode) Error() error { return nil }
func (r StringNode) To(typ Type) Node {
switch typ {
case String:
return r
case Bool:
b, err := strconv.ParseBool(string(r))
if err != nil {
return UNDEFINED_RESULT
}
return BoolNode(b)
case Number:
if strings.Index(string(r), ".") == -1 {
return r.To(Int)
}
return r.To(Float)
case Int:
b, err := strconv.ParseInt(string(r), 10, 64)
if err != nil {
return UNDEFINED_RESULT
}
return IntNode(b)
case Float:
b, err := strconv.ParseFloat(string(r), 64)
if err != nil {
return UNDEFINED_RESULT
}
return FloatNode(b)
}
return UNDEFINED_RESULT
}
func (r StringNode) Raw() []byte {
return []byte(fmt.Sprintf("\"%s\"", r))
}
func (r StringNode) String() string {
return string(r)
}
// JSONNode maybe Object or Array
type JSONNode struct {
value []byte
path string
datatype Type
offset int
err error
}
func (r JSONNode) Type() Type { return r.datatype }
func (r JSONNode) Error() error { return r.err }
func (cc JSONNode) To(typ Type) Node {
switch typ {
case JSON, Object, Array:
return cc
case Bool:
return cc.To(String).To(Bool)
case Number:
return cc.To(String).To(Number)
case Int:
return cc.To(String).To(Int)
case Float:
return cc.To(String).To(Float)
case String:
return StringNode(cc.String())
case Null:
return UNDEFINED_RESULT
case Undefined:
return UNDEFINED_RESULT
default:
return UNDEFINED_RESULT
}
return UNDEFINED_RESULT
}
func (r JSONNode) Raw() []byte {
switch r.datatype {
case String:
ret := append([]byte{byte('"')}, r.value...)
ret = append(ret, byte('"'))
return ret
default:
return []byte(r.String())
}
}
func (r JSONNode) String() string {
return string(r.value)
}