Skip to content

Commit

Permalink
[+] feat: Add v.At(...).Set(...) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-M-C committed Sep 28, 2024
1 parent 1d89554 commit ec17a88
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/en/03_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ This feature is so complicated that we will not use in most cases. But there is
fmt.Println(c.MustMarshalString())
```

If you like placing keys ahead, you can use the `v.At(...).Set(...)` pattern:

```go
// ...
v.At("array", i, "word").Set(words[i])
v.At("array", i, "lesson").Set(lessons[i]).
// ...
```

Final output:

```json
Expand Down
9 changes: 9 additions & 0 deletions docs/zh-cn/03_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ v.MustSet("Hello, array!").At("arr", 0) // {"obj":{"message":"Hello, ob
fmt.Println(c.MustMarshalString())
```

如果你喜欢把 key 放在前面,那你可以使用 `v.At(...).Set(...)` 模式:

```go
// ...
v.At("array", i, "word").Set(words[i])
v.At("array", i, "lesson").Set(lessons[i]).
// ...
```

最终输出为:

```json
Expand Down
32 changes: 30 additions & 2 deletions set_must.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jsonvalue

// MARK: v.MustSet(xxx).At(xxx)

// MustSetter is just like Setter, but not returning sub-value or error.
type MustSetter interface {
// At completes the following operation of Set(). It defines position of value
Expand All @@ -25,7 +27,7 @@ type mSetter struct {
// MustSet is just like Set, but not returning sub-value or error.
func (v *V) MustSet(child any) MustSetter {
setter := v.Set(child)
return &mSetter{
return mSetter{
setter: setter,
}
}
Expand Down Expand Up @@ -129,6 +131,32 @@ func (v *V) MustSetArray() MustSetter {
return v.MustSet(NewArray())
}

func (s *mSetter) At(firstParam any, otherParams ...any) {
func (s mSetter) At(firstParam any, otherParams ...any) {
_, _ = s.setter.At(firstParam, otherParams...)
}

// MARK: v.At(xxx).Set(xxx)

// AtSetter works like v.MustSet(...).At(...), just with different sequence.
type AtSetter interface {
Set(subValue any)
}

// At works like v.MustSet(...).At(...), just with different sequence.
func (v *V) At(firstParam any, otherParams ...any) AtSetter {
return atSetter{
v: v,
firstParam: firstParam,
otherParams: otherParams,
}
}

type atSetter struct {
v *V
firstParam any
otherParams []any
}

func (a atSetter) Set(sub any) {
a.v.MustSet(sub).At(a.firstParam, a.otherParams...)
}
3 changes: 3 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func testSetMisc(t *testing.T) {
_, err = a.SetBool(true).At(0, 0, -1)
so(err, isNil)
so(a.MustMarshalString(), eq, "[[[[],true]]]")

a.At(0, 0, -1).Set(false)
so(a.MustMarshalString(), eq, "[[[[],false]]]")
}

func testSetError(t *testing.T) {
Expand Down

0 comments on commit ec17a88

Please sign in to comment.