This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setters.go
107 lines (92 loc) · 2.12 KB
/
setters.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
/*
Written by Daniel Krom
2018
*/
package jonson
/*
Sets a value to the current JSON,
Makes a deep copy of the interface, removing the original reference
*/
func (jsn *JSON) Set(v interface{}) *JSON {
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
temp := jonsonize(v)
jsn.kind = temp.kind
jsn.value = temp.value
jsn.isPrimitive = temp.isPrimitive
return jsn
}
/*
Set a value to MapObject
if key doesn't exists, it creates it
if current json is not map, it does nothing
*/
func (jsn *JSON) MapSet(key string, value interface{}) *JSON {
if !jsn.IsMap() {
return jsn
}
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
jsn.value.(map[string]*JSON)[key] = jonsonize(value)
return jsn
}
func (jsn *JSON) DeleteMapKey(key string) *JSON {
if !jsn.IsMap() {
return jsn
}
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
delete(jsn.value.(map[string]*JSON), key)
return jsn
}
/*
Append a value at the end of the slice
if current json slice, it does nothing
multiple values will append in the order of the values
SliceAppend(1,2,3,4) -> [oldSlice..., 1,2,3,4]
*/
func (jsn *JSON) SliceAppend(value ...interface{}) *JSON {
if !jsn.IsSlice() {
return jsn
}
/*jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
*/
for _, v := range value {
jsn.value = append(jsn.value.([]*JSON), jonsonize(v))
}
return jsn
}
/*
Append a value at the start of the slice
if current json slice, it does nothing
multiple values will append begin in the order of the values
SliceAppend(1,2,3,4) -> [4,3,2,1, oldSlice...]
*/
func (jsn *JSON) SliceAppendBegin(value ...interface{}) *JSON {
if !jsn.IsSlice() {
return jsn
}
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
arr := jsn.value.([]*JSON)
for _, v := range value {
arr = append([]*JSON{jonsonize(v)}, arr...)
}
jsn.value = arr
return jsn
}
/*
Sets a value at index to current slice
if value isn't slice, it does nothing
User must make sure the length of the slice contains the index
*/
func (jsn *JSON) SliceSet(index int, value interface{}) *JSON {
if !jsn.IsSlice() {
return jsn
}
jsn.rwMutex.Lock()
defer jsn.rwMutex.Unlock()
jsn.value.([]*JSON)[index] = jonsonize(value)
return jsn
}