@@ -59,12 +59,38 @@ type Op struct {
59
59
Op string
60
60
Field int
61
61
Arg interface {}
62
+ // Pos, Len, Replace fields used in the Splice operation
63
+ Pos int
64
+ Len int
65
+ Replace string
62
66
}
63
67
64
68
func (o Op ) EncodeMsgpack (enc * msgpack.Encoder ) error {
65
- enc .EncodeArrayLen (3 )
66
- enc .EncodeString (o .Op )
67
- enc .EncodeInt (int64 (o .Field ))
69
+ isSpliceOperation := o .Op == spliceOperator
70
+ argsLen := 3
71
+ if isSpliceOperation {
72
+ argsLen = 5
73
+ }
74
+ if err := enc .EncodeArrayLen (argsLen ); err != nil {
75
+ return err
76
+ }
77
+ if err := enc .EncodeString (o .Op ); err != nil {
78
+ return err
79
+ }
80
+ if err := enc .EncodeInt (int64 (o .Field )); err != nil {
81
+ return err
82
+ }
83
+
84
+ if isSpliceOperation {
85
+ if err := enc .EncodeInt (int64 (o .Pos )); err != nil {
86
+ return err
87
+ }
88
+ if err := enc .EncodeInt (int64 (o .Len )); err != nil {
89
+ return err
90
+ }
91
+ return enc .EncodeString (o .Replace )
92
+ }
93
+
68
94
return enc .Encode (o .Arg )
69
95
}
70
96
@@ -92,7 +118,12 @@ func NewOperations() *Operations {
92
118
}
93
119
94
120
func (ops * Operations ) append (op string , field int , arg interface {}) * Operations {
95
- ops .ops = append (ops .ops , Op {op , field , arg })
121
+ ops .ops = append (ops .ops , Op {Op : op , Field : field , Arg : arg })
122
+ return ops
123
+ }
124
+
125
+ func (ops * Operations ) appendSplice (op string , field , pos , len int , replace string ) * Operations {
126
+ ops .ops = append (ops .ops , Op {Op : op , Field : field , Pos : pos , Len : len , Replace : replace })
96
127
return ops
97
128
}
98
129
@@ -122,8 +153,8 @@ func (ops *Operations) BitwiseXor(field int, arg interface{}) *Operations {
122
153
}
123
154
124
155
// Splice adds a splice operation to the collection of update operations.
125
- func (ops * Operations ) Splice (field int , arg interface {} ) * Operations {
126
- return ops .append (spliceOperator , field , arg )
156
+ func (ops * Operations ) Splice (field , pos , len int , replace string ) * Operations {
157
+ return ops .appendSplice (spliceOperator , field , pos , len , replace )
127
158
}
128
159
129
160
// Insert adds an insert operation to the collection of update operations.
@@ -140,21 +171,3 @@ func (ops *Operations) Delete(field int, arg interface{}) *Operations {
140
171
func (ops * Operations ) Assign (field int , arg interface {}) * Operations {
141
172
return ops .append (assignOperator , field , arg )
142
173
}
143
-
144
- type OpSplice struct {
145
- Op string
146
- Field int
147
- Pos int
148
- Len int
149
- Replace string
150
- }
151
-
152
- func (o OpSplice ) EncodeMsgpack (enc * msgpack.Encoder ) error {
153
- enc .EncodeArrayLen (5 )
154
- enc .EncodeString (o .Op )
155
- enc .EncodeInt (int64 (o .Field ))
156
- enc .EncodeInt (int64 (o .Pos ))
157
- enc .EncodeInt (int64 (o .Len ))
158
- enc .EncodeString (o .Replace )
159
- return nil
160
- }
0 commit comments