generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move merge logic out of main function
Signed-off-by: Steven Borrelli <[email protected]>
- Loading branch information
1 parent
7ea0dd4
commit 9f88fb6
Showing
2 changed files
with
38 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"dario.cat/mergo" | ||
"github.com/crossplane/function-sdk-go/errors" | ||
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1" | ||
"github.com/crossplane/function-sdk-go/request" | ||
"github.com/crossplane/function-sdk-go/resource" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
type Context map[string]any | ||
|
||
// MergeContextKey merges existing Context at a key with context data val | ||
func (f *Function) MergeContextKey(key string, val map[string]interface{}, req *fnv1beta1.RunFunctionRequest) (*unstructured.Unstructured, error) { | ||
// Check if key is already defined in the context and merge fields | ||
var mergedContext *unstructured.Unstructured | ||
if v, ok := request.GetContextKey(req, key); ok { | ||
mergedContext = &unstructured.Unstructured{} | ||
if err := resource.AsObject(v.GetStructValue(), mergedContext); err != nil { | ||
return mergedContext, errors.Wrapf(err, "cannot get Composition environment from %T context key %q", req, key) | ||
} | ||
f.log.Debug("Loaded Existing Function Context", "context-key", key) | ||
if err := mergo.Merge(&mergedContext.Object, val); err != nil { | ||
return mergedContext, errors.Wrapf(err, "cannot merge data %T at context key %q", req, key) | ||
} | ||
return mergedContext, nil | ||
} | ||
return &unstructured.Unstructured{Object: val}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters