unmarshaler.go
2.9 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gojsondiff
import (
"encoding/json"
"errors"
dmp "github.com/sergi/go-diff/diffmatchpatch"
"io"
"strconv"
)
type Unmarshaller struct {
}
func NewUnmarshaller() *Unmarshaller {
return &Unmarshaller{}
}
func (um *Unmarshaller) UnmarshalBytes(diffBytes []byte) (Diff, error) {
var diffObj map[string]interface{}
json.Unmarshal(diffBytes, &diffObj)
return um.UnmarshalObject(diffObj)
}
func (um *Unmarshaller) UnmarshalString(diffString string) (Diff, error) {
return um.UnmarshalBytes([]byte(diffString))
}
func (um *Unmarshaller) UnmarshalReader(diffReader io.Reader) (Diff, error) {
var diffBytes []byte
io.ReadFull(diffReader, diffBytes)
return um.UnmarshalBytes(diffBytes)
}
func (um *Unmarshaller) UnmarshalObject(diffObj map[string]interface{}) (Diff, error) {
result, err := process(Name(""), diffObj)
if err != nil {
return nil, err
}
return &diff{deltas: result.(*Object).Deltas}, nil
}
func process(position Position, object interface{}) (Delta, error) {
var delta Delta
switch object.(type) {
case map[string]interface{}:
o := object.(map[string]interface{})
if isArray, typed := o["_t"]; typed && isArray == "a" {
deltas := make([]Delta, 0, len(o))
for name, value := range o {
if name == "_t" {
continue
}
normalizedName := name
if normalizedName[0] == '_' {
normalizedName = name[1:]
}
index, err := strconv.Atoi(normalizedName)
if err != nil {
return nil, err
}
childDelta, err := process(Index(index), value)
if err != nil {
return nil, err
}
deltas = append(deltas, childDelta)
}
for _, d := range deltas {
switch d.(type) {
case *Moved:
moved := d.(*Moved)
var dd interface{}
var i int
for i, dd = range deltas {
switch dd.(type) {
case *Moved:
case PostDelta:
pd := dd.(PostDelta)
if moved.PostPosition() == pd.PostPosition() {
moved.Delta = pd
deltas = append(deltas[:i], deltas[i+1:]...)
}
}
}
}
}
delta = NewArray(position, deltas)
} else {
deltas := make([]Delta, 0, len(o))
for name, value := range o {
childDelta, err := process(Name(name), value)
if err != nil {
return nil, err
}
deltas = append(deltas, childDelta)
}
delta = NewObject(position, deltas)
}
case []interface{}:
o := object.([]interface{})
switch len(o) {
case 1:
delta = NewAdded(position, o[0])
case 2:
delta = NewModified(position, o[0], o[1])
case 3:
switch o[2] {
case float64(0):
delta = NewDeleted(position, o[0])
case float64(2):
dmp := dmp.New()
patches, err := dmp.PatchFromText(o[0].(string))
if err != nil {
return nil, err
}
delta = NewTextDiff(position, patches, nil, nil)
case float64(3):
delta = NewMoved(position, Index(int(o[1].(float64))), nil, nil)
default:
return nil, errors.New("Unknown delta type")
}
}
}
return delta, nil
}