Skip to content

Commit 5ac6fa4

Browse files
committed
Make client.Patch implementations gomock-friendly
Signed-off-by: ialidzhikov <i.alidjikov@gmail.com>
1 parent b5065bd commit 5ac6fa4

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pkg/client/patch.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,15 @@ type MergeFromOptions struct {
8484
OptimisticLock bool
8585
}
8686

87+
type patchHandler interface {
88+
CreatePatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error)
89+
}
90+
8791
type mergeFromPatch struct {
88-
patchType types.PatchType
89-
createPatch func(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error)
90-
from Object
91-
opts MergeFromOptions
92+
patchType types.PatchType
93+
patchHandler patchHandler
94+
from Object
95+
opts MergeFromOptions
9296
}
9397

9498
// Type implements Patch.
@@ -124,19 +128,23 @@ func (s *mergeFromPatch) Data(obj Object) ([]byte, error) {
124128
return nil, err
125129
}
126130

127-
data, err := s.createPatch(originalJSON, modifiedJSON, obj)
131+
data, err := s.patchHandler.CreatePatch(originalJSON, modifiedJSON, obj)
128132
if err != nil {
129133
return nil, err
130134
}
131135

132136
return data, nil
133137
}
134138

135-
func createMergePatch(originalJSON, modifiedJSON []byte, _ interface{}) ([]byte, error) {
139+
type mergePatchHandler struct{}
140+
141+
func (h mergePatchHandler) CreatePatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error) {
136142
return jsonpatch.CreateMergePatch(originalJSON, modifiedJSON)
137143
}
138144

139-
func createStrategicMergePatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error) {
145+
type strategicMergePatchHandler struct{}
146+
147+
func (h strategicMergePatchHandler) CreatePatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error) {
140148
return strategicpatch.CreateTwoWayMergePatch(originalJSON, modifiedJSON, dataStruct)
141149
}
142150

@@ -148,7 +156,7 @@ func createStrategicMergePatch(originalJSON, modifiedJSON []byte, dataStruct int
148156
// See https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/ for more details on
149157
// the difference between merge-patch and strategic-merge-patch.
150158
func MergeFrom(obj Object) Patch {
151-
return &mergeFromPatch{patchType: types.MergePatchType, createPatch: createMergePatch, from: obj}
159+
return &mergeFromPatch{patchType: types.MergePatchType, patchHandler: mergePatchHandler{}, from: obj}
152160
}
153161

154162
// MergeFromWithOptions creates a Patch that patches using the merge-patch strategy with the given object as base.
@@ -158,7 +166,7 @@ func MergeFromWithOptions(obj Object, opts ...MergeFromOption) Patch {
158166
for _, opt := range opts {
159167
opt.ApplyToMergeFrom(options)
160168
}
161-
return &mergeFromPatch{patchType: types.MergePatchType, createPatch: createMergePatch, from: obj, opts: *options}
169+
return &mergeFromPatch{patchType: types.MergePatchType, patchHandler: mergePatchHandler{}, from: obj, opts: *options}
162170
}
163171

164172
// StrategicMergeFrom creates a Patch that patches using the strategic-merge-patch strategy with the given object as base.
@@ -175,7 +183,7 @@ func StrategicMergeFrom(obj Object, opts ...MergeFromOption) Patch {
175183
for _, opt := range opts {
176184
opt.ApplyToMergeFrom(options)
177185
}
178-
return &mergeFromPatch{patchType: types.StrategicMergePatchType, createPatch: createStrategicMergePatch, from: obj, opts: *options}
186+
return &mergeFromPatch{patchType: types.StrategicMergePatchType, patchHandler: strategicMergePatchHandler{}, from: obj, opts: *options}
179187
}
180188

181189
// mergePatch uses a raw merge strategy to patch the object.

0 commit comments

Comments
 (0)