-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation_remove.go
More file actions
33 lines (26 loc) · 838 Bytes
/
operation_remove.go
File metadata and controls
33 lines (26 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package jsonpatch
import (
"encoding/json"
"fmt"
"github.com/ohler55/ojg/jp"
)
func (pr *PatchRequest[T]) remove(initialResource *T, emptyResource *T) (*T, error) {
compiledPath, err := jp.ParseString(pr.Path)
if err != nil {
return nil, fmt.Errorf("fail to compile path %s %s", pr.Path, err.Error())
}
b, err := json.Marshal(initialResource)
if err != nil {
return nil, fmt.Errorf("match fail to marshal input resource %s", err.Error())
}
var resourceAsMap interface{}
err = json.Unmarshal(b, &resourceAsMap)
if err != nil {
return nil, fmt.Errorf("match fail to unmarshal %s", err.Error())
}
resourceAsMapModified, err := compiledPath.Remove(resourceAsMap)
if err != nil {
return nil, fmt.Errorf("error while deleting nodes %s", err.Error())
}
return pr.remarshal(resourceAsMapModified, emptyResource)
}