MCPcopy Create free account
hub / github.com/crossplane-contrib/function-patch-and-transform / ApplyFromFieldPathPatch

Function ApplyFromFieldPathPatch

patches.go:65–103  ·  view source on GitHub ↗

ApplyFromFieldPathPatch patches the "to" resource, using a source field on the "from" resource. Values may be transformed if any are defined on the patch.

(p PatchInterface, from, to runtime.Object)

Source from the content-addressed store, hash-verified

63// on the "from" resource. Values may be transformed if any are defined on
64// the patch.
65func ApplyFromFieldPathPatch(p PatchInterface, from, to runtime.Object) error {
66 fromMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(from)
67 if err != nil {
68 return err
69 }
70
71 in, err := fieldpath.Pave(fromMap).GetValue(p.GetFromFieldPath())
72 if err != nil {
73 return err
74 }
75
76 // Apply transform pipeline
77 out, err := ResolveTransforms(p.GetTransforms(), in)
78 if err != nil {
79 return err
80 }
81
82 // Round-trip the "from" source field value through Kubernetes JSON decoder,
83 // so that the json integers are unmarshalled as int64 consistent with "to"/dest value handling.
84 // Kubernetes JSON decoder will get us a map[string]any where number values are int64,
85 // but protojson and structpb will get us one where number values are float64.
86 // https://pkg.go.dev/sigs.k8s.io/json#UnmarshalCaseSensitivePreserveInts
87 v, err := toValidJSON(out)
88 if err != nil {
89 return err
90 }
91
92 mo, err := toMergeOption(p)
93 if err != nil {
94 return err
95 }
96
97 // ComposedPatch all expanded fields if the ToFieldPath contains wildcards
98 if strings.Contains(p.GetToFieldPath(), "[*]") {
99 return patchFieldValueToMultiple(p.GetToFieldPath(), v, to, mo)
100 }
101
102 return errors.Wrap(patchFieldValueToObject(p.GetToFieldPath(), v, to, mo), "cannot patch to object")
103}
104
105func toValidJSON(value any) (any, error) {
106 var v any

Callers 4

ApplyEnvironmentPatchFunction · 0.85
ApplyComposedPatchFunction · 0.85

Calls 8

ResolveTransformsFunction · 0.85
toValidJSONFunction · 0.85
toMergeOptionFunction · 0.85
patchFieldValueToObjectFunction · 0.85
GetFromFieldPathMethod · 0.65
GetTransformsMethod · 0.65
GetToFieldPathMethod · 0.65

Tested by 1