Ejemplo n.º 1
0
// Apply the patch to an object; as if the object were
// marshalled/unmarshalled JSON.
func (patch JSONMergePatch) Apply(in interface{}, out interface{}) locale.Error {
	inBytes, uerr := json.Marshal(in)
	if uerr != nil {
		return locale.UntranslatedError(uerr)
	}
	outBytes, uerr := evan.MergePatch(inBytes, patch)
	if uerr != nil {
		return locale.UntranslatedError(uerr)
	}
	return locale.UntranslatedError(json.Unmarshal(outBytes, out))
}
Ejemplo n.º 2
0
// Merge requires JSON serialization
// TODO: merge assumes JSON serialization, and does not properly abstract API retrieval
func Merge(codec runtime.Codec, dst runtime.Object, fragment, kind string) (runtime.Object, error) {
	// encode dst into versioned json and apply fragment directly too it
	target, err := runtime.Encode(codec, dst)
	if err != nil {
		return nil, err
	}
	patched, err := jsonpatch.MergePatch(target, []byte(fragment))
	if err != nil {
		return nil, err
	}
	out, err := runtime.Decode(codec, patched)
	if err != nil {
		return nil, err
	}
	return out, nil
}
Ejemplo n.º 3
0
func getPatchedJSON(patchType types.PatchType, originalJS, patchJS []byte, obj runtime.Object) ([]byte, error) {
	switch patchType {
	case types.JSONPatchType:
		patchObj, err := jsonpatch.DecodePatch(patchJS)
		if err != nil {
			return nil, err
		}
		return patchObj.Apply(originalJS)

	case types.MergePatchType:
		return jsonpatch.MergePatch(originalJS, patchJS)

	case types.StrategicMergePatchType:
		return strategicpatch.StrategicMergePatch(originalJS, patchJS, obj)

	default:
		// only here as a safety net - go-restful filters content-type
		return nil, fmt.Errorf("unknown Content-Type header for patch: %v", patchType)
	}
}