-
Notifications
You must be signed in to change notification settings - Fork 2
/
make.go
executable file
·367 lines (341 loc) · 8.78 KB
/
make.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package jin
import (
"fmt"
"strconv"
)
// Scheme is a tool for creating non-nested JSONs.
// It provides a struct for saving a JSON scheme for later usage.
// Do not access or manipulate this struct.
// Please use methods provided for.
type Scheme struct {
originalKeys []string
keys []string
}
// MakeScheme is constructor method for creating Scheme's.
// It needs key(s) for create JSON.
// More information on Type Scheme example.
func MakeScheme(keys ...string) *Scheme {
return &Scheme{keys: keys, originalKeys: keys}
}
// MakeJson is main creation method for creating JSON's from Schemes.
// More information on Type Scheme example.
func (s *Scheme) MakeJson(values ...interface{}) []byte {
return MakeJson(s.keys, values)
}
// MakeJsonString is main creation method for creating JSON's from Schemes.
// More information on Type Scheme example.
func (s *Scheme) MakeJsonString(values ...string) []byte {
return MakeJsonString(s.keys, values)
}
// MutateJson use for mutating a json to another json with a scheme.
func (s *Scheme) MutateJson(json []byte) ([]byte, error) {
newJson := []byte(`{}`)
var err, err2 error
var val string
for _, k := range s.keys {
val, err = GetString(json, k)
val = formatType(val)
newJson, err2 = AddKeyValueString(newJson, k, val)
if err == nil {
if err2 != nil {
return nil, err
}
}
}
return newJson, nil
}
// MutateJsonAbs use for mutating a json to another json with a scheme.
func (s *Scheme) MutateJsonAbs(json []byte) ([]byte, error) {
newJson := []byte(`{}`)
var err error
var val string
for _, k := range s.keys {
val, err = GetString(json, k)
if err != nil {
return nil, err
}
val = formatType(val)
newJson, err = AddKeyValueString(newJson, k, val)
if err != nil {
return nil, err
}
}
return newJson, nil
}
// Check checks the json is fitting the scheme or not
func (s *Scheme) Check(json []byte) bool {
for _, k := range s.keys {
_, err := Get(json, k)
if err != nil {
return false
}
}
return true
}
// CheckAbs checks the json is absolutely fitting the scheme or not
func (s *Scheme) CheckAbs(json []byte) bool {
keys, err := GetKeys(json)
if err != nil {
return false
}
if len(keys) != len(s.keys) {
return false
}
for i := 0; i < len(keys); i++ {
has := false
key := keys[i]
for j := 0; j < len(s.keys); j++ {
if key == s.keys[j] {
has = true
break
}
}
if !has {
return false
}
}
return true
}
// Add adds a new key value to the current scheme.
// If given key is already exists it returns false, otherwise returns true.
// More information on Type Scheme example.
func (s *Scheme) Add(key string) bool {
for _, k := range s.keys {
if k == key {
return false
}
}
s.keys = append(s.keys, key)
return true
}
// Remove removes the key value to the current scheme.
// If given key is not exists it returns false, otherwise returns true.
// More information on Type Scheme example.
func (s *Scheme) Remove(key string) bool {
newKeys := make([]string, 0, len(s.keys))
result := false
for _, k := range s.keys {
if k != key {
newKeys = append(newKeys, k)
} else {
result = true
}
}
s.keys = newKeys
return result
}
// Save saves current keys for protect them temporary changes.
// More information on Type Scheme example.
func (s *Scheme) Save() {
s.originalKeys = s.keys
}
// Restore Schemes original form.
// More information on Type Scheme example.
func (s *Scheme) Restore() {
s.keys = s.originalKeys
}
// GetOriginalKeys is a simple get function for get Schemes original keys.
// More information on Type Scheme example.
func (s *Scheme) GetOriginalKeys() []string {
return s.originalKeys
}
// GetCurrentKeys is a simple get function for get Schemes current keys.
// More information on Type Scheme example.
func (s *Scheme) GetCurrentKeys() []string {
return s.keys
}
// MakeEmptyArray simply creates "[]" this as byte slice.
func MakeEmptyArray() []byte {
return []byte{91, 93}
}
// MakeArray creates an array formation from given values and returns them as byte slice.
// Do not use any slice/array for parameter.
// It will accept this kind types but won't be able to make valid representation for use!
func MakeArray(elements ...interface{}) []byte {
if elements == nil {
return []byte{91, 93}
}
js := make([]byte, 0, 128)
js = append(js, 91)
for _, e := range elements {
js = append(js, []byte(formatType(fmt.Sprintf("%v", e)))...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 93)
return js
}
// MakeArrayString is a variation of MakeArray() func.
// Parameter type must be slice of string.
// For more information look MakeArray() function.
func MakeArrayString(values []string) []byte {
if values == nil {
return []byte(`[]`)
}
js := make([]byte, 0, 128)
js = append(js, 91)
for _, v := range values {
js = append(js, []byte(formatType(v))...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 93)
return js
}
// MakeArrayInt is a variation of MakeArray() func.
// Parameter type must be slice of integer.
// For more information look MakeArray() function.
func MakeArrayInt(values []int) []byte {
if values == nil {
return []byte{91, 93}
}
js := make([]byte, 0, 128)
js = append(js, 91)
for _, v := range values {
js = append(js, []byte(strconv.Itoa(v))...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 93)
return js
}
// MakeArrayBool is a variation of MakeArray() func.
// Parameter type must be slice of boolean.
// For more information look MakeArray() function.
func MakeArrayBool(values []bool) []byte {
if values == nil {
return []byte{91, 93}
}
js := make([]byte, 0, 128)
js = append(js, 91)
t := []byte("true")
f := []byte("false")
for _, v := range values {
var val []byte
if v {
val = t
} else {
val = f
}
js = append(js, val...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 93)
return js
}
// MakeArrayFloat is a variation of MakeArray() func.
// Parameter type must be slice of float64.
// For more information look MakeArray() function.
func MakeArrayFloat(values []float64) []byte {
if values == nil {
return []byte{91, 93}
}
js := make([]byte, 0, 128)
js = append(js, 91)
for _, v := range values {
num := strconv.FormatFloat(v, 'f', -1, 64)
start := 0
for i := 0; i < len(num); i++ {
if num[i] == 'e' {
start = i
}
}
exp, _ := strconv.Atoi(num[start+2:])
if exp == 0 {
js = append(js, []byte(num[:start])...)
} else {
js = append(js, []byte(num)...)
}
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 93)
return js
}
// MakeArrayBytes is a variation of MakeArray() func.
// Parameter type must be slice of byte.
// For more information look MakeArray() function.
func MakeArrayBytes(values ...[]byte) []byte {
if values == nil {
return []byte{91, 93}
}
js := make([]byte, 0, 128)
js = append(js, 91)
for _, v := range values {
js = append(js, v...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 93)
return js
}
// MakeEmptyJson simply creates "{}" this as byte slice.
func MakeEmptyJson() []byte {
return []byte{123, 125}
}
// MakeJson creates an JSON formation from given key and value slices, and returns them as byte slice.
// Do not use any slice/array for 'values' variable parameter.
// It will accept this kind types but won't be able to make valid representation for use!
func MakeJson(keys []string, values []interface{}) []byte {
if len(keys) != len(values) {
return nil
}
if keys == nil {
return []byte{123, 125}
}
js := make([]byte, 0, 128)
js = append(js, 123)
for i, k := range keys {
js = append(js, 34)
js = append(js, []byte(k)...)
js = append(js, 34)
js = append(js, 58)
js = append(js, []byte(formatType(fmt.Sprintf("%v", values[i])))...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 125)
return js
}
// MakeJsonString creates an JSON formation from given key and value string slices, and returns them as byte slice.
func MakeJsonString(keys, values []string) []byte {
if len(keys) != len(values) {
return nil
}
if keys == nil {
return []byte{123, 125}
}
js := make([]byte, 0, 128)
js = append(js, 123)
for i, k := range keys {
js = append(js, 34)
js = append(js, []byte(k)...)
js = append(js, 34)
js = append(js, 58)
js = append(js, []byte(formatType(values[i]))...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 125)
return js
}
// MakeJsonWithMap creates an JSON formation from given string-string-map, and returns them as byte slice.
func MakeJsonWithMap(json map[string]string) []byte {
if json == nil {
return []byte{123, 125}
}
js := make([]byte, 0, 128)
js = append(js, 123)
for k, v := range json {
js = append(js, 34)
js = append(js, []byte(k)...)
js = append(js, 34)
js = append(js, 58)
js = append(js, []byte(formatType(v))...)
js = append(js, 44)
}
js = js[:len(js)-1]
js = append(js, 125)
return js
}