Skip to content

Question: is implementing a patch application function reasonable? #171

@icamys

Description

@icamys

I was looking at how to implement data patching, and it appears that a working algorithm looks as follows:

  1. Get the patch operations in the HTTP handler (the package already does this);
  2. Get data you want to patch from a data store and transform it to a map[string]interface{};
  3. Apply the patch operations to the data;
  4. Validate the patched data using Schema.Validate() function. If not valid, return a patch error;
  5. Transform the patched data to resource and return the resource.

If this is how the package is designed to work, it seems reasonable to implement a generic function that does data patching. Something like this:

// Apply applies the patch operation to the provided data.
func (o *PatchOperation) Apply (data map[string]interface{})

Then, the handler will have a shape like this:

func (h *Handler) Patch(r *http.Request, id string, operations []scim.PatchOperation) (scim.Resource, error) {
	entity := h.dataStore.Get(id)

        data := userEntity.ToMap()

        for _, op := range operations {
                op.Apply(data)
        }

        if err := h.schema.Validate(data); err != nil {
                // return an appropriate error
        }

       entity := newEntityFromData(data)
       h.dataStore.Save(entity)

       resource := newResourceFromEntity(entity)

	return resource, nil
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions