Skip to content

Commit

Permalink
feat: Add getters to server response
Browse files Browse the repository at this point in the history
Allow different steps inside a server function to get the current state
of the response objects.

Signed-off-by: Maximilian Blatt <mbxd12@web.de>
  • Loading branch information
MisterMX committed Jan 25, 2024
1 parent 41597fa commit 60ee39a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions server_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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.
//
Expand Down

0 comments on commit 60ee39a

Please sign in to comment.