Skip to content

Commit ec17a88

Browse files
committed
[+] feat: Add v.At(...).Set(...) methods
1 parent 1d89554 commit ec17a88

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

docs/en/03_set.md

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ This feature is so complicated that we will not use in most cases. But there is
119119
fmt.Println(c.MustMarshalString())
120120
```
121121

122+
If you like placing keys ahead, you can use the `v.At(...).Set(...)` pattern:
123+
124+
```go
125+
// ...
126+
v.At("array", i, "word").Set(words[i])
127+
v.At("array", i, "lesson").Set(lessons[i]).
128+
// ...
129+
```
130+
122131
Final output:
123132

124133
```json

docs/zh-cn/03_set.md

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ v.MustSet("Hello, array!").At("arr", 0) // {"obj":{"message":"Hello, ob
115115
fmt.Println(c.MustMarshalString())
116116
```
117117

118+
如果你喜欢把 key 放在前面,那你可以使用 `v.At(...).Set(...)` 模式:
119+
120+
```go
121+
// ...
122+
v.At("array", i, "word").Set(words[i])
123+
v.At("array", i, "lesson").Set(lessons[i]).
124+
// ...
125+
```
126+
118127
最终输出为:
119128

120129
```json

set_must.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package jsonvalue
22

3+
// MARK: v.MustSet(xxx).At(xxx)
4+
35
// MustSetter is just like Setter, but not returning sub-value or error.
46
type MustSetter interface {
57
// At completes the following operation of Set(). It defines position of value
@@ -25,7 +27,7 @@ type mSetter struct {
2527
// MustSet is just like Set, but not returning sub-value or error.
2628
func (v *V) MustSet(child any) MustSetter {
2729
setter := v.Set(child)
28-
return &mSetter{
30+
return mSetter{
2931
setter: setter,
3032
}
3133
}
@@ -129,6 +131,32 @@ func (v *V) MustSetArray() MustSetter {
129131
return v.MustSet(NewArray())
130132
}
131133

132-
func (s *mSetter) At(firstParam any, otherParams ...any) {
134+
func (s mSetter) At(firstParam any, otherParams ...any) {
133135
_, _ = s.setter.At(firstParam, otherParams...)
134136
}
137+
138+
// MARK: v.At(xxx).Set(xxx)
139+
140+
// AtSetter works like v.MustSet(...).At(...), just with different sequence.
141+
type AtSetter interface {
142+
Set(subValue any)
143+
}
144+
145+
// At works like v.MustSet(...).At(...), just with different sequence.
146+
func (v *V) At(firstParam any, otherParams ...any) AtSetter {
147+
return atSetter{
148+
v: v,
149+
firstParam: firstParam,
150+
otherParams: otherParams,
151+
}
152+
}
153+
154+
type atSetter struct {
155+
v *V
156+
firstParam any
157+
otherParams []any
158+
}
159+
160+
func (a atSetter) Set(sub any) {
161+
a.v.MustSet(sub).At(a.firstParam, a.otherParams...)
162+
}

set_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ func testSetMisc(t *testing.T) {
189189
_, err = a.SetBool(true).At(0, 0, -1)
190190
so(err, isNil)
191191
so(a.MustMarshalString(), eq, "[[[[],true]]]")
192+
193+
a.At(0, 0, -1).Set(false)
194+
so(a.MustMarshalString(), eq, "[[[[],false]]]")
192195
}
193196

194197
func testSetError(t *testing.T) {

0 commit comments

Comments
 (0)