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 6cb060e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 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

0 comments on commit 6cb060e

Please sign in to comment.