Skip to content

Commit

Permalink
Make client.Patch implementations gomock-friendly
Browse files Browse the repository at this point in the history
Signed-off-by: ialidzhikov <i.alidjikov@gmail.com>
  • Loading branch information
ialidzhikov committed Mar 18, 2021
1 parent b5065bd commit f8d053d
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pkg/client/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ type MergeFromOptions struct {
OptimisticLock bool
}

type patchHandler interface {
createPatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error)
}

type mergeFromPatch struct {
patchType types.PatchType
createPatch func(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error)
from Object
opts MergeFromOptions
patchType types.PatchType
patchHandler patchHandler
from Object
opts MergeFromOptions
}

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

data, err := s.createPatch(originalJSON, modifiedJSON, obj)
data, err := s.patchHandler.createPatch(originalJSON, modifiedJSON, obj)
if err != nil {
return nil, err
}

return data, nil
}

func createMergePatch(originalJSON, modifiedJSON []byte, _ interface{}) ([]byte, error) {
type mergePatchHandler struct{}

func (mergePatchHandler) createPatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error) {
return jsonpatch.CreateMergePatch(originalJSON, modifiedJSON)
}

func createStrategicMergePatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error) {
type strategicMergePatchHandler struct{}

func (strategicMergePatchHandler) createPatch(originalJSON, modifiedJSON []byte, dataStruct interface{}) ([]byte, error) {
return strategicpatch.CreateTwoWayMergePatch(originalJSON, modifiedJSON, dataStruct)
}

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

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

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

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

0 comments on commit f8d053d

Please sign in to comment.