rewrite expand function
This commit is contained in:
parent
babc02905d
commit
90ba6e6066
|
@ -36,6 +36,12 @@ type about struct {
|
||||||
CaptivatingIndex json.Number
|
CaptivatingIndex json.Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func expand(arr []any, idx int) []any {
|
||||||
|
front := append(arr[:idx], nil)
|
||||||
|
all := append(front, arr[idx:])
|
||||||
|
return all
|
||||||
|
}
|
||||||
|
|
||||||
func setValue(obj any, key string, value any) error {
|
func setValue(obj any, key string, value any) error {
|
||||||
var err error
|
var err error
|
||||||
switch v := obj.(type) {
|
switch v := obj.(type) {
|
||||||
|
@ -51,13 +57,7 @@ func setValue(obj any, key string, value any) error {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v) <= idx {
|
v = expand(v, idx)
|
||||||
arr := make([]any, idx+1)
|
|
||||||
for i, val := range v {
|
|
||||||
arr[i] = val
|
|
||||||
}
|
|
||||||
v = arr
|
|
||||||
}
|
|
||||||
v[idx] = value
|
v[idx] = value
|
||||||
default:
|
default:
|
||||||
err = errors.New("couldn't determine type")
|
err = errors.New("couldn't determine type")
|
||||||
|
@ -66,16 +66,6 @@ func setValue(obj any, key string, value any) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func expand(v []any, idx int) {
|
|
||||||
if len(v) <= idx {
|
|
||||||
arr := make([]any, idx+1)
|
|
||||||
for i, val := range v {
|
|
||||||
arr[i] = val
|
|
||||||
}
|
|
||||||
v = arr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func patch(obj any, path string, value any) error {
|
func patch(obj any, path string, value any) error {
|
||||||
var err error
|
var err error
|
||||||
first, rest, found := strings.Cut(path, "/")
|
first, rest, found := strings.Cut(path, "/")
|
||||||
|
@ -94,7 +84,7 @@ func patch(obj any, path string, value any) error {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
expand(v, idx)
|
v = expand(v, idx)
|
||||||
err = patch(v[idx], rest, value)
|
err = patch(v[idx], rest, value)
|
||||||
default:
|
default:
|
||||||
err = errors.New("couldn't determine type")
|
err = errors.New("couldn't determine type")
|
||||||
|
|
Loading…
Reference in New Issue