Skip to content

Commit ccc856f

Browse files
bigbesoleg-jukovec
authored andcommitted
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 4567442 commit ccc856f

File tree

8 files changed

+347
-1122
lines changed

8 files changed

+347
-1122
lines changed

export_test.go

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

future_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (req *futureMockRequest) Async() bool {
2727
return false
2828
}
2929

30-
func (req *futureMockRequest) Body(resolver SchemaResolver, enc *msgpack.Encoder) error {
30+
func (req *futureMockRequest) Body(_ SchemaResolver, _ *msgpack.Encoder) error {
3131
return nil
3232
}
3333

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+
if err := enc.EncodeMapLen(1); err != nil {
66+
return err
67+
}
68+
69+
if err := enc.EncodeUint(uint64(iproto.IPROTO_SQL_TEXT)); err != nil {
70+
return err
71+
}
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+
if err := enc.EncodeMapLen(1); err != nil {
119+
return err
120+
}
121+
122+
if err := enc.EncodeUint(uint64(iproto.IPROTO_STMT_ID)); err != nil {
123+
return err
124+
}
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+
if err := enc.EncodeMapLen(2); err != nil {
172+
return err
173+
}
174+
175+
if err := enc.EncodeUint(uint64(iproto.IPROTO_STMT_ID)); err != nil {
176+
return err
177+
}
178+
179+
if err := enc.EncodeUint(uint64(req.stmt.StatementID)); err != nil {
180+
return err
181+
}
182+
183+
if err := enc.EncodeUint(uint64(iproto.IPROTO_SQL_BIND)); err != nil {
184+
return err
185+
}
186+
187+
return encodeSQLBind(enc, req.args)
176188
}
177189

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

protocol.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,37 @@ 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+
if err := enc.EncodeMapLen(2); err != nil {
7682
return err
7783
}
7884

79-
enc.EncodeUint(uint64(iproto.IPROTO_FEATURES))
85+
if err := enc.EncodeUint(uint64(iproto.IPROTO_VERSION)); err != nil {
86+
return err
87+
}
8088

81-
t := len(protocolInfo.Features)
82-
if err := enc.EncodeArrayLen(t); err != nil {
89+
if err := enc.Encode(req.protocolInfo.Version); err != nil {
8390
return err
8491
}
8592

86-
for _, feature := range protocolInfo.Features {
93+
if err := enc.EncodeUint(uint64(iproto.IPROTO_FEATURES)); err != nil {
94+
return err
95+
}
96+
97+
if err := enc.EncodeArrayLen(len(req.protocolInfo.Features)); err != nil {
98+
return err
99+
}
100+
101+
for _, feature := range req.protocolInfo.Features {
87102
if err := enc.Encode(feature); err != nil {
88103
return err
89104
}
@@ -92,19 +107,6 @@ func fillId(enc *msgpack.Encoder, protocolInfo ProtocolInfo) error {
92107
return nil
93108
}
94109

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-
108110
// Context sets a passed context to the request.
109111
//
110112
// Pay attention that when using context with request objects,

0 commit comments

Comments
 (0)