-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathops.go
152 lines (117 loc) · 2.21 KB
/
ops.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// This package implements a Mendoza differ and patcher.
//
// You use CreatePatch (or CreateDoublePatch) to create patches,
// which can then be applied with ApplyPatch.
//
// The Patch type is already JSON serializable, but you can implement Reader/Writer
// (and use WriteTo/ReadFrom) if you need a custom serialization.
//
// Supported types
//
// The differ/patcher is only implemented to work on the following types:
// bool
// float64
// string
// map[string]interface{}
// []interface{}
// nil
//
// If you need to support additional types you can use the option WithConvertFunc which
// defines a function that is applied to every value.
package mendoza
//go-sumtype:decl Op
// Op is the interface for an operation.
type Op interface {
applyTo(p *patcher)
readParams(r Reader) error
writeParams(w Writer) error
}
// A patch is a list of operations.
type Patch []Op
// Output stack operators
type OpValue struct {
Value interface{}
}
type OpCopy struct {
}
type OpBlank struct {
}
type OpReturnIntoObject struct {
Key string
}
type OpReturnIntoObjectSameKey struct {
}
type OpReturnIntoArray struct {
}
// Input stack operators
type OpPushField struct {
Index int
}
type OpPushElement struct {
Index int
}
type OpPushParent struct {
N int
}
type OpPop struct {
}
// Combined input and output
type OpPushFieldCopy struct {
OpPushField
OpCopy
}
type OpPushFieldBlank struct {
OpPushField
OpBlank
}
type OpPushElementCopy struct {
OpPushElement
OpCopy
}
type OpPushElementBlank struct {
OpPushElement
OpBlank
}
type OpReturnIntoObjectPop struct {
OpReturnIntoObject
OpPop
}
type OpReturnIntoObjectSameKeyPop struct {
OpReturnIntoObjectSameKey
OpPop
}
type OpReturnIntoArrayPop struct {
OpReturnIntoArray
OpPop
}
// Object helpers
type OpObjectSetFieldValue struct {
OpValue
OpReturnIntoObject
}
type OpObjectCopyField struct {
OpPushField
OpCopy
OpReturnIntoObjectSameKey
OpPop
}
//
type OpObjectDeleteField struct {
Index int
}
// Array helpers
type OpArrayAppendValue struct {
Value interface{}
}
type OpArrayAppendSlice struct {
Left int
Right int
}
// String helpers
type OpStringAppendString struct {
String string
}
type OpStringAppendSlice struct {
Left int
Right int
}