@@ -59,12 +59,38 @@ type Op struct {
5959 Op string
6060 Field int
6161 Arg interface {}
62+ // Pos, Len, Replace fields used in the Splice operation.
63+ Pos int
64+ Len int
65+ Replace string
6266}
6367
6468func (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+
6894 return enc .Encode (o .Arg )
6995}
7096
@@ -92,7 +118,12 @@ func NewOperations() *Operations {
92118}
93119
94120func (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 })
96127 return ops
97128}
98129
@@ -122,8 +153,8 @@ func (ops *Operations) BitwiseXor(field int, arg interface{}) *Operations {
122153}
123154
124155// 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 )
127158}
128159
129160// Insert adds an insert operation to the collection of update operations.
@@ -140,21 +171,3 @@ func (ops *Operations) Delete(field int, arg interface{}) *Operations {
140171func (ops * Operations ) Assign (field int , arg interface {}) * Operations {
141172 return ops .append (assignOperator , field , arg )
142173}
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