-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
146 lines (124 loc) · 2.52 KB
/
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
package njson
import (
"fmt"
"math/bits"
)
// Type is the type of a node.
type Type uint8
// Token types and type masks
const (
TypeInvalid Type = iota
TypeString Type = 1 << iota
TypeObject
TypeArray
TypeNumber
TypeBoolean
TypeNull
TypeAnyValue = TypeString | TypeNumber | TypeBoolean | TypeObject | TypeArray | TypeNull
)
// Types returns all types of a typemask
func (t Type) Types() (types []Type) {
if t == 0 {
return []Type{}
}
if bits.OnesCount(uint(t)) == 1 {
return []Type{t}
}
for i := Type(0); i < 8; i++ {
tt := Type(1 << i)
if t&tt != 0 {
types = append(types, tt)
}
}
return
}
const (
strFalse = "false"
strTrue = "true"
strNull = "null"
strNaN = "NaN"
)
func (t Type) String() string {
switch t {
case TypeInvalid:
return "InvalidToken"
case TypeString:
return "String"
case TypeArray:
return "Array"
case TypeObject:
return "Object"
case TypeNumber:
return "Number"
case TypeNull:
return "Null"
case TypeBoolean:
return "Boolean"
case TypeAnyValue:
return "AnyValue"
default:
if bits.OnesCount(uint(t)) > 1 {
return fmt.Sprint(t.Types())
}
return "InvalidToken"
}
}
// info is a bitmask with type info for a node.
type info uint16
const (
vString = info(TypeString)
vNumber = info(TypeNumber)
vNull = info(TypeNull)
vBoolean = info(TypeBoolean)
vArray = info(TypeArray)
vObject = info(TypeObject)
)
// Type flags
const (
_ info = 1 << (iota + 8)
infRoot
)
// IsRoot checks if IsRoot flag is set.
func (i info) IsRoot() bool {
return i&infRoot == infRoot
}
func (i info) Flags() info {
return i & 0xFF00
}
// Type retutns the Type part of Info.
func (i info) Type() Type {
return Type(i)
}
// IsNull checks if i is TypeNull
func (i info) IsNull() bool {
return i.Type() == TypeNull
}
// IsBoolean checks if i is TypeBoolean
func (i info) IsBoolean() bool {
return i.Type() == TypeBoolean
}
// IsArray checks if i is TypeArray
func (i info) IsArray() bool {
return i.Type() == TypeArray
}
// IsValue checks if t matches TypeAnyValue
func (t Type) IsValue() bool {
return t&TypeAnyValue != 0
}
const infAnyValue = info(TypeAnyValue)
// IsValue checks if i matches TypeAnyValue
func (i info) IsValue() bool {
return i&infAnyValue != 0
}
// IsString checks if i is TypeString
func (i info) IsString() bool {
return i.Type() == TypeString
}
// IsNumber checks if i is TypeNumber
func (i info) IsNumber() bool {
return i.Type() == TypeNumber
}
// IsObject checks if i is TypeObject
func (i info) IsObject() bool {
return i.Type() == TypeObject
}