Skip to content

Commit

Permalink
feat: support merge patch when update
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 11, 2024
1 parent 8d0faac commit 3e98d3f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions ext/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.3
require (
github.com/andybalholm/brotli v1.1.1
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd
github.com/evanphx/json-patch/v5 v5.9.0
github.com/evanw/esbuild v0.24.0
github.com/go-faker/faker/v4 v4.5.0
github.com/gofrs/uuid v4.4.0+incompatible
Expand Down
2 changes: 2 additions & 0 deletions ext/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yA
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd h1:QMSNEh9uQkDjyPwu/J541GgSH+4hw+0skJDIj9HJ3mE=
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/evanw/esbuild v0.24.0 h1:GZ78naTLp7FKr+K7eNuM/SLs5maeiHYRPsTg6kmdsSE=
github.com/evanw/esbuild v0.24.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/go-faker/faker/v4 v4.5.0 h1:ARzAY2XoOL9tOUK+KSecUQzyXQsUaZHefjyF8x6YFHc=
Expand Down
20 changes: 9 additions & 11 deletions ext/pkg/system/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package system

import (
"context"
"encoding/json"

jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/gofrs/uuid"
"github.com/siyul-park/uniflow/pkg/types"

"github.com/siyul-park/uniflow/pkg/resource"
)
Expand Down Expand Up @@ -68,25 +70,21 @@ func UpdateResource[T resource.Resource](store resource.Store[T]) func(context.C
continue
}

doc1, err := types.Marshal(origin)
json1, err := json.Marshal(patch)
if err != nil {
return nil, err
}

doc2, err := types.Marshal(patch)
json2, err := json.Marshal(origin)
if err != nil {
return nil, err
}

var pair []types.Value
if doc, ok := doc1.(types.Map); ok {
pair = append(pair, doc.Pairs()...)
}
if doc, ok := doc2.(types.Map); ok {
pair = append(pair, doc.Pairs()...)
merge, err := jsonpatch.MergePatch(json1, json2)
if err != nil {
return nil, err
}

if err := types.Unmarshal(types.NewMap(pair...), &resources[i]); err != nil {
if err := json.Unmarshal(merge, &resources[i]); err != nil {
return nil, err
}
}
Expand Down
18 changes: 9 additions & 9 deletions ext/pkg/system/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestWatchResource(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

st := resource.NewStore[*resource.Meta]()
st := resource.NewStore[resource.Resource]()
fn := WatchResource(st)

_, err := fn(ctx)
Expand All @@ -26,15 +26,15 @@ func TestCreateResource(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

st := resource.NewStore[*resource.Meta]()
st := resource.NewStore[resource.Resource]()
fn := CreateResource(st)

meta := &resource.Meta{
ID: uuid.Must(uuid.NewV7()),
Name: faker.Word(),
}

res, err := fn(ctx, []*resource.Meta{meta})
res, err := fn(ctx, []resource.Resource{meta})
assert.NoError(t, err)
assert.Len(t, res, 1)
}
Expand All @@ -43,7 +43,7 @@ func TestReadResource(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

st := resource.NewStore[*resource.Meta]()
st := resource.NewStore[resource.Resource]()
fn := ReadResource(st)

meta := &resource.Meta{
Expand All @@ -54,7 +54,7 @@ func TestReadResource(t *testing.T) {
_, err := st.Store(ctx, meta)
assert.NoError(t, err)

res, err := fn(ctx, []*resource.Meta{meta})
res, err := fn(ctx, []resource.Resource{meta})
assert.NoError(t, err)
assert.Len(t, res, 1)
}
Expand All @@ -63,7 +63,7 @@ func TestUpdateResource(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

st := resource.NewStore[*resource.Meta]()
st := resource.NewStore[resource.Resource]()
fn := UpdateResource(st)

meta := &resource.Meta{
Expand All @@ -74,7 +74,7 @@ func TestUpdateResource(t *testing.T) {
_, err := st.Store(ctx, meta)
assert.NoError(t, err)

res, err := fn(ctx, []*resource.Meta{meta})
res, err := fn(ctx, []resource.Resource{meta})
assert.NoError(t, err)
assert.Len(t, res, 1)
}
Expand All @@ -83,7 +83,7 @@ func TestDeleteResource(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

st := resource.NewStore[*resource.Meta]()
st := resource.NewStore[resource.Resource]()
fn := DeleteResource(st)

meta := &resource.Meta{
Expand All @@ -94,7 +94,7 @@ func TestDeleteResource(t *testing.T) {
_, err := st.Store(ctx, meta)
assert.NoError(t, err)

res, err := fn(ctx, []*resource.Meta{meta})
res, err := fn(ctx, []resource.Resource{meta})
assert.NoError(t, err)
assert.Len(t, res, 1)
}

0 comments on commit 3e98d3f

Please sign in to comment.