Skip to content

Commit aeb3d88

Browse files
committed
refactor: get rid of fillXXX functions, in favor of Body method
NB: all deleted tests were checking that functions passed from constructor object to fillXXX function were passed in right order, so removing them is not a problem
1 parent 20494e9 commit aeb3d88

File tree

6 files changed

+308
-1116
lines changed

6 files changed

+308
-1116
lines changed

export_test.go

Lines changed: 0 additions & 158 deletions
This file was deleted.

prepared.go

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,6 @@ type Prepared struct {
2222
Conn *Connection
2323
}
2424

25-
func fillPrepare(enc *msgpack.Encoder, expr string) error {
26-
enc.EncodeMapLen(1)
27-
enc.EncodeUint(uint64(iproto.IPROTO_SQL_TEXT))
28-
return enc.EncodeString(expr)
29-
}
30-
31-
func fillUnprepare(enc *msgpack.Encoder, stmt Prepared) error {
32-
enc.EncodeMapLen(1)
33-
enc.EncodeUint(uint64(iproto.IPROTO_STMT_ID))
34-
return enc.EncodeUint(uint64(stmt.StatementID))
35-
}
36-
37-
func fillExecutePrepared(enc *msgpack.Encoder, stmt Prepared, args interface{}) error {
38-
enc.EncodeMapLen(2)
39-
enc.EncodeUint(uint64(iproto.IPROTO_STMT_ID))
40-
enc.EncodeUint(uint64(stmt.StatementID))
41-
enc.EncodeUint(uint64(iproto.IPROTO_SQL_BIND))
42-
return encodeSQLBind(enc, args)
43-
}
44-
4525
// NewPreparedFromResponse constructs a Prepared object.
4626
func NewPreparedFromResponse(conn *Connection, resp Response) (*Prepared, error) {
4727
if resp == nil {
@@ -81,8 +61,16 @@ func NewPrepareRequest(expr string) *PrepareRequest {
8161
}
8262

8363
// Body fills an msgpack.Encoder with the execute request body.
84-
func (req *PrepareRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
85-
return fillPrepare(enc, req.expr)
64+
func (req *PrepareRequest) Body(_ SchemaResolver, enc *msgpack.Encoder) error {
65+
err := enc.EncodeMapLen(1)
66+
if err != nil {
67+
return err
68+
}
69+
err = enc.EncodeUint(uint64(iproto.IPROTO_SQL_TEXT))
70+
if err != nil {
71+
return err
72+
}
73+
return enc.EncodeString(req.expr)
8674
}
8775

8876
// Context sets a passed context to the request.
@@ -126,8 +114,16 @@ func (req *UnprepareRequest) Conn() *Connection {
126114
}
127115

128116
// Body fills an msgpack.Encoder with the execute request body.
129-
func (req *UnprepareRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
130-
return fillUnprepare(enc, *req.stmt)
117+
func (req *UnprepareRequest) Body(_ SchemaResolver, enc *msgpack.Encoder) error {
118+
err := enc.EncodeMapLen(1)
119+
if err != nil {
120+
return err
121+
}
122+
err = enc.EncodeUint(uint64(iproto.IPROTO_STMT_ID))
123+
if err != nil {
124+
return err
125+
}
126+
return enc.EncodeUint(uint64(req.stmt.StatementID))
131127
}
132128

133129
// Context sets a passed context to the request.
@@ -171,8 +167,24 @@ func (req *ExecutePreparedRequest) Args(args interface{}) *ExecutePreparedReques
171167
}
172168

173169
// Body fills an msgpack.Encoder with the execute request body.
174-
func (req *ExecutePreparedRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
175-
return fillExecutePrepared(enc, *req.stmt, req.args)
170+
func (req *ExecutePreparedRequest) Body(_ SchemaResolver, enc *msgpack.Encoder) error {
171+
err := enc.EncodeMapLen(2)
172+
if err != nil {
173+
return err
174+
}
175+
err = enc.EncodeUint(uint64(iproto.IPROTO_STMT_ID))
176+
if err != nil {
177+
return err
178+
}
179+
err = enc.EncodeUint(uint64(req.stmt.StatementID))
180+
if err != nil {
181+
return err
182+
}
183+
err = enc.EncodeUint(uint64(iproto.IPROTO_SQL_BIND))
184+
if err != nil {
185+
return err
186+
}
187+
return encodeSQLBind(enc, req.args)
176188
}
177189

178190
// Context sets a passed context to the request.

protocol.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,48 @@ type IdRequest struct {
6868
protocolInfo ProtocolInfo
6969
}
7070

71-
func fillId(enc *msgpack.Encoder, protocolInfo ProtocolInfo) error {
72-
enc.EncodeMapLen(2)
71+
// NewIdRequest returns a new IdRequest.
72+
func NewIdRequest(protocolInfo ProtocolInfo) *IdRequest {
73+
req := new(IdRequest)
74+
req.rtype = iproto.IPROTO_ID
75+
req.protocolInfo = protocolInfo.Clone()
76+
return req
77+
}
7378

74-
enc.EncodeUint(uint64(iproto.IPROTO_VERSION))
75-
if err := enc.Encode(protocolInfo.Version); err != nil {
79+
// Body fills an msgpack.Encoder with the id request body.
80+
func (req *IdRequest) Body(_ SchemaResolver, enc *msgpack.Encoder) error {
81+
err := enc.EncodeMapLen(2)
82+
if err != nil {
7683
return err
7784
}
7885

79-
enc.EncodeUint(uint64(iproto.IPROTO_FEATURES))
80-
81-
t := len(protocolInfo.Features)
82-
if err := enc.EncodeArrayLen(t); err != nil {
86+
err = enc.EncodeUint(uint64(iproto.IPROTO_VERSION))
87+
if err != nil {
88+
return err
89+
}
90+
err = enc.Encode(req.protocolInfo.Version)
91+
if err != nil {
8392
return err
8493
}
8594

86-
for _, feature := range protocolInfo.Features {
87-
if err := enc.Encode(feature); err != nil {
95+
err = enc.EncodeUint(uint64(iproto.IPROTO_FEATURES))
96+
if err != nil {
97+
return err
98+
}
99+
err = enc.EncodeArrayLen(len(req.protocolInfo.Features))
100+
if err != nil {
101+
return err
102+
}
103+
for _, feature := range req.protocolInfo.Features {
104+
err = enc.Encode(feature)
105+
if err != nil {
88106
return err
89107
}
90108
}
91109

92110
return nil
93111
}
94112

95-
// NewIdRequest returns a new IdRequest.
96-
func NewIdRequest(protocolInfo ProtocolInfo) *IdRequest {
97-
req := new(IdRequest)
98-
req.rtype = iproto.IPROTO_ID
99-
req.protocolInfo = protocolInfo.Clone()
100-
return req
101-
}
102-
103-
// Body fills an msgpack.Encoder with the id request body.
104-
func (req *IdRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
105-
return fillId(enc, req.protocolInfo)
106-
}
107-
108113
// Context sets a passed context to the request.
109114
//
110115
// Pay attention that when using context with request objects,

0 commit comments

Comments
 (0)