-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
69 lines (59 loc) · 1.13 KB
/
operation.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
61
62
63
64
65
66
67
68
69
package mjsonpatch
import (
"bytes"
"encoding/json"
"errors"
"sync"
)
const (
REMOVE = "remove"
ADD = "add"
REPLACE = "replace"
MOVE = "move"
COPY = "copy"
TEST = "test"
)
var (
valPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
)
type Operation struct {
OP string `json:"op,omitempty"`
Path string `json:"path,omitempty"`
Value json.RawMessage `json:"value,omitempty"`
}
type Patch []*Operation
func (op *Operation) path() (string, error) {
if op.Path == "" {
return "", errors.New("path is empty")
}
return op.Path, nil
}
func (op *Operation) action() (string, error) {
if op.OP == "" {
return "", errors.New("op is empty")
}
return op.OP, nil
}
func (o *Operation) valueInterface() (interface{}, error) {
if len(o.Value) > 0 {
buf := valPool.Get().(*bytes.Buffer)
buf.Write(o.Value)
defer func() {
buf.Reset()
valPool.Put(buf)
}()
dec := json.NewDecoder(buf)
dec.UseNumber()
var v interface{}
err := dec.Decode(&v)
if err != nil {
return nil, err
}
return v, nil
}
return nil, errors.New("missing value field")
}