mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-04-24 21:18:19 -05:00
drop sjson dependency
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
@@ -3,10 +3,10 @@ package theme
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"dario.cat/mergo"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
// KV is a generic key-value map.
|
||||
@@ -27,30 +27,33 @@ func MergeKV(values ...KV) (KV, error) {
|
||||
}
|
||||
|
||||
// PatchKV injects the given values into to v.
|
||||
func PatchKV(v any, values KV) error {
|
||||
bv, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
func PatchKV(v map[string]interface{}, values KV) KV {
|
||||
if v == nil {
|
||||
v = KV{}
|
||||
}
|
||||
|
||||
nv := string(bv)
|
||||
|
||||
for k, val := range values {
|
||||
var err error
|
||||
switch val {
|
||||
// if the value is nil, we delete the key
|
||||
case nil:
|
||||
nv, err = sjson.Delete(nv, k)
|
||||
default:
|
||||
nv, err = sjson.Set(nv, k, val)
|
||||
}
|
||||
t := v
|
||||
path := strings.Split(k, ".")
|
||||
for i, p := range path {
|
||||
if i == len(path)-1 {
|
||||
switch val {
|
||||
// if the value is nil, we delete the key
|
||||
case nil:
|
||||
delete(t, p)
|
||||
default:
|
||||
t[p] = val
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
if _, ok := t[p]; !ok {
|
||||
t[p] = map[string]interface{}{}
|
||||
}
|
||||
|
||||
t = t[p].(map[string]interface{})
|
||||
}
|
||||
}
|
||||
|
||||
return json.Unmarshal([]byte(nv), v)
|
||||
return v
|
||||
}
|
||||
|
||||
// LoadKV loads a key-value map from the given file system.
|
||||
@@ -89,10 +92,7 @@ func UpdateKV(fsys afero.Fs, p string, values KV) error {
|
||||
kv = existing
|
||||
}
|
||||
|
||||
err = PatchKV(&kv, values)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kv = PatchKV(kv, values)
|
||||
|
||||
return WriteKV(fsys, p, kv)
|
||||
}
|
||||
|
||||
@@ -32,19 +32,20 @@ func TestMergeKV(t *testing.T) {
|
||||
|
||||
func TestPatchKV(t *testing.T) {
|
||||
in := theme.KV{
|
||||
"a": theme.KV{
|
||||
"a": map[string]interface{}{
|
||||
"value": "a",
|
||||
},
|
||||
"b": theme.KV{
|
||||
"b": map[string]interface{}{
|
||||
"value": "b",
|
||||
},
|
||||
}
|
||||
err := theme.PatchKV(&in, theme.KV{
|
||||
"b.value": "b-new",
|
||||
"c.value": "c-new",
|
||||
out := theme.PatchKV(in, theme.KV{
|
||||
"b.value": "b-new",
|
||||
"c.value": "c-new",
|
||||
"d": "d-new",
|
||||
"e.value.subvalue": "e-new",
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, in, theme.KV{
|
||||
assert.Equal(t, theme.KV{
|
||||
"a": map[string]interface{}{
|
||||
"value": "a",
|
||||
},
|
||||
@@ -54,7 +55,55 @@ func TestPatchKV(t *testing.T) {
|
||||
"c": map[string]interface{}{
|
||||
"value": "c-new",
|
||||
},
|
||||
"d": "d-new",
|
||||
"e": map[string]interface{}{
|
||||
"value": map[string]interface{}{
|
||||
"subvalue": "e-new",
|
||||
},
|
||||
},
|
||||
}, out)
|
||||
}
|
||||
|
||||
func TestPatchKVUnset(t *testing.T) {
|
||||
in := theme.KV{
|
||||
"a": map[string]interface{}{
|
||||
"value": "a",
|
||||
},
|
||||
"b": map[string]interface{}{
|
||||
"value": "b",
|
||||
},
|
||||
}
|
||||
out := theme.PatchKV(in, theme.KV{
|
||||
"a.value": nil,
|
||||
"b": nil,
|
||||
})
|
||||
assert.Equal(t, theme.KV{
|
||||
"a": map[string]interface{}{},
|
||||
}, out)
|
||||
}
|
||||
|
||||
func TestPatchKVwithNil(t *testing.T) {
|
||||
var in theme.KV
|
||||
out := theme.PatchKV(in, theme.KV{
|
||||
"b.value": "b-new",
|
||||
"c.value": "c-new",
|
||||
"d": "d-new",
|
||||
"e.value.subvalue": "e-new",
|
||||
})
|
||||
assert.Equal(t, theme.KV{
|
||||
"b": map[string]interface{}{
|
||||
"value": "b-new",
|
||||
},
|
||||
"c": map[string]interface{}{
|
||||
"value": "c-new",
|
||||
},
|
||||
"d": "d-new",
|
||||
"e": map[string]interface{}{
|
||||
"value": map[string]interface{}{
|
||||
"subvalue": "e-new",
|
||||
},
|
||||
},
|
||||
}, out)
|
||||
}
|
||||
|
||||
func TestLoadKV(t *testing.T) {
|
||||
@@ -113,10 +162,10 @@ func TestUpdateKV(t *testing.T) {
|
||||
|
||||
fsys := fsx.NewMemMapFs()
|
||||
assert.Nil(t, afero.WriteFile(fsys, "some.json", wb, 0644))
|
||||
assert.Nil(t, theme.UpdateKV(fsys, "some.json", theme.KV{
|
||||
_ = theme.UpdateKV(fsys, "some.json", theme.KV{
|
||||
"b.value": "b-new",
|
||||
"c.value": "c-new",
|
||||
}))
|
||||
})
|
||||
|
||||
f, err := fsys.Open("some.json")
|
||||
assert.Nil(t, err)
|
||||
|
||||
Reference in New Issue
Block a user