-
Notifications
You must be signed in to change notification settings - Fork 786
/
patch.go
60 lines (50 loc) · 1.4 KB
/
patch.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package json
import (
"encoding/json"
"errors"
"github.com/mattbaird/jsonpatch"
)
// CreatePatch creates a patch as specified in http://jsonpatch.com/.
//
// 'before' is original, 'after' is the modified struct.
// The function will return the patch as byte array.
//
// An error will be returned if any of the two structs is nil.
func CreatePatch(before, after interface{}) ([]byte, error) {
if before == nil {
return nil, errors.New("'before' cannot be nil when creating a JSON patch")
}
if after == nil {
return nil, errors.New("'after' cannot be nil when creating a JSON patch")
}
rawBefore, rawAfter, err := marshallBeforeAfter(before, after)
if err != nil {
return nil, err
}
patch, err := jsonpatch.CreatePatch(rawBefore, rawAfter)
if err != nil {
return nil, err
}
jsonPatch, err := json.Marshal(patch)
if err != nil {
return nil, err
}
return jsonPatch, nil
}
func marshallBeforeAfter(before, after interface{}) ([]byte, []byte, error) {
rawBefore, err := json.Marshal(before)
if err != nil {
return nil, nil, err
}
rawAfter, err := json.Marshal(after)
if err != nil {
return rawBefore, nil, err
}
return rawBefore, rawAfter, nil
}
// Patch is a slice of JsonPatchOperations
type Patch []jsonpatch.JsonPatchOperation
// MarshalJSON converts the Patch into a byte array
func (p Patch) MarshalJSON() ([]byte, error) {
return json.Marshal([]jsonpatch.JsonPatchOperation(p))
}