Skip to content

Commit

Permalink
Merge pull request #9624 from owncloud/drop-sjson-dependency
Browse files Browse the repository at this point in the history
drop sjson dependency
  • Loading branch information
butonic authored Jul 18, 2024
2 parents 3e3fcf3 + c3c6085 commit 81ef018
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 1,077 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ require (
github.com/test-go/testify v1.1.4
github.com/thejerf/suture/v4 v4.0.5
github.com/tidwall/gjson v1.17.1
github.com/tidwall/sjson v1.2.5
github.com/tus/tusd v1.13.0
github.com/unrolled/secure v1.14.0
github.com/urfave/cli/v2 v2.27.2
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2037,16 +2037,13 @@ github.com/thanhpk/randstr v1.0.6 h1:psAOktJFD4vV9NEVb3qkhRSMvYh4ORRaj1+w/hn4B+o
github.com/thanhpk/randstr v1.0.6/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U=
github.com/thejerf/suture/v4 v4.0.5 h1:F1E/4FZwXWqvlWDKEUo6/ndLtxGAUzMmNqkrMknZbAA=
github.com/thejerf/suture/v4 v4.0.5/go.mod h1:gu9Y4dXNUWFrByqRt30Rm9/UZ0wzRSt9AJS6xu/ZGxU=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
Expand Down
50 changes: 25 additions & 25 deletions services/web/pkg/theme/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

if err != nil {
return err
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 _, 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.
Expand Down Expand Up @@ -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)
}
67 changes: 58 additions & 9 deletions services/web/pkg/theme/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 0 additions & 21 deletions vendor/github.com/tidwall/sjson/LICENSE

This file was deleted.

Loading

0 comments on commit 81ef018

Please sign in to comment.