-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsvalue.go
189 lines (171 loc) · 3.54 KB
/
jsvalue.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
package jsongo
import (
"fmt"
"strconv"
)
type JSType int64
const (
String JSType = iota + 1
Number
Boolean
Null
Array
Object
)
type JSValue struct {
value interface{}
kind JSType
}
func NewValue() *JSValue {
return &JSValue{kind: Null, value: nil}
}
func NewBoolean(b bool) *JSValue {
return &JSValue{kind: Boolean, value: b}
}
func NewNumber(n float64) *JSValue {
return &JSValue{kind: Number, value: n}
}
func NewArray() []*JSValue {
return make([]*JSValue, 0, 10)
}
func NewObject() map[string]*JSValue {
return make(map[string]*JSValue, 0)
}
func (val *JSValue) Equals(other *JSValue) bool {
if val.kind != other.kind {
return false
} else if val.kind == Array || val.kind == Object {
return false
} else {
if val.kind == Boolean {
v1, _ := val.Bool()
v2, _ := other.Bool()
return v1 == v2
} else if val.kind == Null {
return true
} else if val.kind == Number {
v1, _ := val.Float()
v2, _ := other.Float()
return v1 == v2
} else if val.kind == String {
v1, _ := val.String()
v2, _ := other.String()
return v1 == v2
}
return false
}
}
func (val *JSValue) ToString() (string, error) {
result := ""
switch val.kind {
case Array:
v, ok := val.value.([]*JSValue)
if !ok {
return "", fmt.Errorf("%s", "Cannot convert to []*JSValue in value")
}
result += "["
for index, i := range v {
s, _ := i.ToString()
result += s
if index+1 != len(v) {
result += ", "
}
}
result += "]"
case Object:
v, ok := val.value.(map[string]*JSValue)
if !ok {
return "", fmt.Errorf("%s", "Cannot convert to map[string]*JSValue struct")
}
result += "{"
amount := len(v)
j := 0
for k, v := range v {
s, _ := v.ToString()
result += fmt.Sprintf("\"%s\" : %s", k, s)
if j+1 != amount {
result += ", "
}
j++
}
result += "}"
case String:
v, ok := val.value.(string)
if !ok {
return "", fmt.Errorf("%s", "Cannot convert to string")
}
result += "\"" + string(v) + "\""
case Boolean:
v, ok := val.value.(bool)
if !ok {
return "", fmt.Errorf("%s", "Cannot convert to bool")
}
if v {
result += "true"
} else {
result += "false"
}
case Null:
result += "null"
case Number:
v, ok := val.value.(float64)
if !ok {
return "", fmt.Errorf("%s", "Cannot convert to float")
}
result += strconv.FormatFloat(v, 'f', -1, 64)
default:
return "", fmt.Errorf("%s", "Cannot convert to string")
}
return string(result), nil
}
func (v *JSValue) Float() (float64, error) {
res, ok := v.value.(float64)
if !ok {
return 0, fmt.Errorf("%s", "Error: Not a number value")
} else {
return res, nil
}
}
func (v *JSValue) Int() (int64, error) {
res, ok := v.value.(float64)
if !ok {
return 0, fmt.Errorf("%s", "Error: Not a number value")
} else {
return int64(res), nil
}
}
func (v *JSValue) String() (string, error) {
res, ok := v.value.(string)
if !ok {
return "", fmt.Errorf("%s", "Error: Not a string value")
} else {
return res, nil
}
}
func (v *JSValue) Bool() (bool, error) {
res, ok := v.value.(bool)
if !ok {
return false, fmt.Errorf("%s", "Error: Not a bool value")
} else {
return res, nil
}
}
func (v *JSValue) Array() ([]*JSValue, error) {
res, ok := v.value.([]*JSValue)
if !ok {
return nil, fmt.Errorf("%s", "Error: Not an array value")
} else {
return res, nil
}
}
func (v *JSValue) Object() (map[string]*JSValue, error) {
res, ok := v.value.(map[string]*JSValue)
if !ok {
return nil, fmt.Errorf("%s", "Error: Not an object value")
} else {
return res, nil
}
}
func (v *JSValue) Type() JSType {
return v.kind
}