-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_set.go
executable file
·117 lines (110 loc) · 2.88 KB
/
parse_set.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
package jin
import "strconv"
// Set sets the value that path has pointed.
// Path can point anything, a key-value pair, a value, an array, an object.
// Path variable can not be null,
// otherwise it will provide an error message.
func (p *Parser) Set(newVal []byte, path ...string) error {
lenp := len(path)
lenv := len(newVal)
var curr *node
var err error
if lenp == 0 {
return errNullPath()
}
curr, err = p.core.walk(path)
if err != nil {
return err
}
if lenv >= 2 {
if newVal[0] == 91 || newVal[0] == 123 {
newCore := createNode(nil)
pCore(newVal, newCore)
newCore.label = curr.label
newCore.up = curr.up
index := curr.getIndex()
curr.up.down[index] = newCore
newCore.value = newVal
p.json, _ = Set(p.json, newVal, path...)
for i := 0; i < lenp-1; i++ {
newCore = newCore.up
newCore.value, err = Get(p.json, path[:lenp-1-i]...)
if err != nil {
return err
}
}
return nil
}
}
curr.value = newVal
p.json, _ = Set(p.json, newVal, path...)
for i := 0; i < lenp-1; i++ {
curr = curr.up
curr.value, err = Get(p.json, path[:lenp-1-i]...)
if err != nil {
return err
}
}
return nil
}
// SetString is a variation of Set() func.
// SetString takes the set value as string.
func (p *Parser) SetString(newValue string, path ...string) error {
if newValue[0] != 34 && newValue[len(newValue)-1] != 34 {
return p.Set([]byte(`"`+newValue+`"`), path...)
}
return p.Set([]byte(newValue), path...)
}
// SetInt is a variation of Set() func.
// SetInt takes the set value as integer.
func (p *Parser) SetInt(newValue int, path ...string) error {
return p.Set([]byte(strconv.Itoa(newValue)), path...)
}
// SetFloat is a variation of Set() func.
// SetFloat takes the set value as float64.
func (p *Parser) SetFloat(newValue float64, path ...string) error {
return p.Set([]byte(strconv.FormatFloat(newValue, 'f', -1, 64)), path...)
}
// SetBool is a variation of Set() func.
// SetBool takes the set value as boolean.
func (p *Parser) SetBool(newValue bool, path ...string) error {
if newValue {
return p.Set([]byte("true"), path...)
}
return p.Set([]byte("false"), path...)
}
// SetKey sets the key value of key-value pair that path has pointed.
// Path must point to an object.
// otherwise it will provide an error message.
// Path variable can not be null,
func (p *Parser) SetKey(newKey string, path ...string) error {
lenp := len(path)
lenv := len(newKey)
var curr *node
var err error
if lenp == 0 {
return errNullPath()
}
if lenv == 0 {
return errNullKey()
}
curr, err = p.core.walk(path)
if err != nil {
return err
}
for _, d := range curr.up.down {
if d.label == newKey {
return errKeyAlreadyExist(newKey)
}
}
curr.label = newKey
p.json, _ = SetKey(p.json, newKey, path...)
for i := 0; i < lenp-1; i++ {
curr = curr.up
curr.value, _ = Get(p.json, path[:lenp-1-i]...)
if err != nil {
return err
}
}
return nil
}