Skip to content

Commit

Permalink
Make desired resources map values pointers to struct
Browse files Browse the repository at this point in the history
This makes it possible to mutate struct values while iterating over the
map of composed resources. I found myself naturally wanting to do this
while writing a Function. e.g.:

desired, _ = request.GetDesiredComposedResources(req)

for _, dr := range desired {
	dr.Ready = resource.ReadyTrue
}

_ = response.SetDesiredComposedResources(desired)

I haven't made the same change for observed resources, since they're
supposed to be read-only to a Function.

Signed-off-by: Nic Cope <nicc@rk0n.org>
  • Loading branch information
negz committed Sep 16, 2023
1 parent ef3a2e0 commit fce65c0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func GetDesiredCompositeResource(req *v1beta1.RunFunctionRequest) (*resource.Com
}

// GetDesiredComposedResources from the supplied request.
func GetDesiredComposedResources(req *v1beta1.RunFunctionRequest) (map[resource.Name]resource.DesiredComposed, error) {
dcds := map[resource.Name]resource.DesiredComposed{}
func GetDesiredComposedResources(req *v1beta1.RunFunctionRequest) (map[resource.Name]*resource.DesiredComposed, error) {
dcds := map[resource.Name]*resource.DesiredComposed{}
for name, r := range req.GetDesired().GetResources() {
dcd := resource.DesiredComposed{Resource: composed.New()}
dcd := &resource.DesiredComposed{Resource: composed.New()}
if err := resource.AsObject(r.GetResource(), dcd.Resource); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func SetDesiredCompositeResource(rsp *v1beta1.RunFunctionResponse, xr *resource.
// supplied response. The caller must be sure to avoid overwriting the desired
// state that may have been accumulated by previous Functions in the pipeline,
// unless they intend to.
func SetDesiredComposedResources(rsp *v1beta1.RunFunctionResponse, dcds map[resource.Name]resource.DesiredComposed) error {
func SetDesiredComposedResources(rsp *v1beta1.RunFunctionResponse, dcds map[resource.Name]*resource.DesiredComposed) error {
if rsp.Desired == nil {
rsp.Desired = &v1beta1.State{}
}
Expand Down

0 comments on commit fce65c0

Please sign in to comment.