Skip to content

Commit a4d1b87

Browse files
committed
feat: support merge patch when update
1 parent 8d0faac commit a4d1b87

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

ext/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.23.3
55
require (
66
github.com/andybalholm/brotli v1.1.1
77
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd
8+
github.com/evanphx/json-patch/v5 v5.9.0
89
github.com/evanw/esbuild v0.24.0
910
github.com/go-faker/faker/v4 v4.5.0
1011
github.com/gofrs/uuid v4.4.0+incompatible

ext/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yA
1919
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
2020
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd h1:QMSNEh9uQkDjyPwu/J541GgSH+4hw+0skJDIj9HJ3mE=
2121
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
22+
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
23+
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
2224
github.com/evanw/esbuild v0.24.0 h1:GZ78naTLp7FKr+K7eNuM/SLs5maeiHYRPsTg6kmdsSE=
2325
github.com/evanw/esbuild v0.24.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
2426
github.com/go-faker/faker/v4 v4.5.0 h1:ARzAY2XoOL9tOUK+KSecUQzyXQsUaZHefjyF8x6YFHc=

ext/pkg/system/syscall.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package system
22

33
import (
44
"context"
5+
"encoding/json"
6+
7+
jsonpatch "github.com/evanphx/json-patch/v5"
58
"github.com/gofrs/uuid"
69
"github.com/siyul-park/uniflow/pkg/types"
710

@@ -60,8 +63,8 @@ func UpdateResource[T resource.Resource](store resource.Store[T]) func(context.C
6063
}
6164

6265
for i := 0; i < len(resources); i++ {
63-
patch := resources[i]
64-
origin, ok := origins[patch.GetID()]
66+
rsc := resources[i]
67+
origin, ok := origins[rsc.GetID()]
6568
if !ok {
6669
resources = append(resources[:i], resources[i+1:]...)
6770
i--
@@ -72,21 +75,34 @@ func UpdateResource[T resource.Resource](store resource.Store[T]) func(context.C
7275
if err != nil {
7376
return nil, err
7477
}
75-
76-
doc2, err := types.Marshal(patch)
78+
doc2, err := types.Marshal(rsc)
7779
if err != nil {
7880
return nil, err
7981
}
8082

81-
var pair []types.Value
82-
if doc, ok := doc1.(types.Map); ok {
83-
pair = append(pair, doc.Pairs()...)
83+
json1, err := json.Marshal(doc1.Interface())
84+
if err != nil {
85+
return nil, err
8486
}
85-
if doc, ok := doc2.(types.Map); ok {
86-
pair = append(pair, doc.Pairs()...)
87+
json2, err := json.Marshal(doc2.Interface())
88+
if err != nil {
89+
return nil, err
8790
}
8891

89-
if err := types.Unmarshal(types.NewMap(pair...), &resources[i]); err != nil {
92+
merge, err := jsonpatch.MergePatch(json1, json2)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var doc any
98+
if err := json.Unmarshal(merge, &doc); err != nil {
99+
return nil, err
100+
}
101+
val, err := types.Marshal(doc)
102+
if err != nil {
103+
return nil, err
104+
}
105+
if err := types.Unmarshal(val, &resources[i]); err != nil {
90106
return nil, err
91107
}
92108
}

0 commit comments

Comments
 (0)