From 60ee39a428da01a0075cff63ea7c732fbb7730d2 Mon Sep 17 00:00:00 2001 From: Maximilian Blatt Date: Thu, 25 Jan 2024 03:38:15 +0100 Subject: [PATCH] feat: Add getters to server response Allow different steps inside a server function to get the current state of the response objects. Signed-off-by: Maximilian Blatt --- error.go | 14 ++++++++++++++ server.go | 15 +++++++++++++++ server_function.go | 8 ++++++++ 3 files changed, 37 insertions(+) create mode 100644 error.go diff --git a/error.go b/error.go new file mode 100644 index 0000000..9bce603 --- /dev/null +++ b/error.go @@ -0,0 +1,14 @@ +package server + +import "fmt" + +type errNotFound string + +func (e errNotFound) Error() string { + return fmt.Sprintf("not found: %s", string(e)) +} + +func IsNotFoundError(err error) bool { + _, ok := err.(*errNotFound) + return ok +} diff --git a/server.go b/server.go index 3a05ff0..25c3b56 100644 --- a/server.go +++ b/server.go @@ -114,6 +114,13 @@ func (r *RunServerFunctionResponse) SetComposite(o runtime.Object, mods ...Resou return nil } +func (r *RunServerFunctionResponse) GetComposite(target runtime.Object) error { + if r.DesiredComposite == nil || r.DesiredComposite.Resource == nil { + return nil // Return an error here? + } + return resource.AsObject(r.DesiredComposite.Resource, target) +} + func (r *RunServerFunctionResponse) SetComposedRaw(name string, res *fnapi.Resource) { if r.DesiredComposed == nil { r.DesiredComposed = map[string]*fnapi.Resource{} @@ -136,6 +143,14 @@ func (r *RunServerFunctionResponse) SetComposed(name string, o runtime.Object, m return nil } +func (r *RunServerFunctionResponse) GetComposed(name string, target runtime.Object) error { + state, exists := r.DesiredComposed[name] + if !exists { + return errNotFound(name) + } + return resource.AsObject(state.Resource, target) +} + func (r *RunServerFunctionResponse) SetContextField(key string, value any) error { if r.DesiredContext == nil { r.DesiredContext = &structpb.Struct{ diff --git a/server_function.go b/server_function.go index a017119..8aaef6d 100644 --- a/server_function.go +++ b/server_function.go @@ -45,6 +45,10 @@ type ServerFunctionResponse interface { // resource for the given name. SetComposite(o runtime.Object, mods ...ResourceModifier) error + // GetComposite gets the current state of the composite resource of this + // response and writes its contents into the given target object. + GetComposite(target runtime.Object) error + // SetCompositeRaw sets the desired response state directly using the // native SDK types. // @@ -56,6 +60,10 @@ type ServerFunctionResponse interface { // identified by the given name for this function's response. SetComposed(name string, o runtime.Object, mods ...ResourceModifier) error + // GetComposed looks up the composed resource in the current response object + // and writes its contents into the given target object. + GetComposed(name string, target runtime.Object) error + // SetComposedRaw sets the desired composed resource state directly using // the native SDK types. //