(r *http.Request, id string, operations []PatchOperation)
| 121 | } |
| 122 | |
| 123 | func (h testResourceHandler) Patch(r *http.Request, id string, operations []PatchOperation) (Resource, error) { |
| 124 | if h.shouldReturnNoContent(id, operations) { |
| 125 | return Resource{}, nil |
| 126 | } |
| 127 | |
| 128 | for _, op := range operations { |
| 129 | switch op.Op { |
| 130 | case PatchOperationAdd: |
| 131 | if op.Path != nil { |
| 132 | h.data[id].resourceAttributes[op.Path.String()] = op.Value |
| 133 | } else { |
| 134 | valueMap := op.Value.(map[string]interface{}) |
| 135 | for k, v := range valueMap { |
| 136 | if arr, ok := h.data[id].resourceAttributes[k].([]interface{}); ok { |
| 137 | arr = append(arr, v) |
| 138 | h.data[id].resourceAttributes[k] = arr |
| 139 | } else { |
| 140 | h.data[id].resourceAttributes[k] = v |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | case PatchOperationReplace: |
| 145 | if op.Path != nil { |
| 146 | h.data[id].resourceAttributes[op.Path.String()] = op.Value |
| 147 | } else { |
| 148 | valueMap := op.Value.(map[string]interface{}) |
| 149 | for k, v := range valueMap { |
| 150 | h.data[id].resourceAttributes[k] = v |
| 151 | } |
| 152 | } |
| 153 | case PatchOperationRemove: |
| 154 | h.data[id].resourceAttributes[op.Path.String()] = nil |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | created, _ := time.ParseInLocation(time.RFC3339, fmt.Sprintf("%v", h.data[id].meta["created"]), time.UTC) |
| 159 | now := time.Now() |
| 160 | |
| 161 | // return resource with replaced attributes |
| 162 | return Resource{ |
| 163 | ID: id, |
| 164 | ExternalID: h.externalID(h.data[id].resourceAttributes), |
| 165 | Attributes: h.data[id].resourceAttributes, |
| 166 | Meta: Meta{ |
| 167 | Created: &created, |
| 168 | LastModified: &now, |
| 169 | Version: fmt.Sprintf("%s.patch", h.data[id].meta["version"]), |
| 170 | }, |
| 171 | }, nil |
| 172 | } |
| 173 | |
| 174 | func (h testResourceHandler) Replace(r *http.Request, id string, attributes ResourceAttributes) (Resource, error) { |
| 175 | // check if resource exists |
nothing calls this directly
no test coverage detected