Skip to content

Commit 73458f1

Browse files
Speshal71viciious
authored andcommitted
added support for setting result' unmarshal mode for the whole connection
1 parent 8ea808e commit 73458f1

File tree

5 files changed

+76
-21
lines changed

5 files changed

+76
-21
lines changed

call17_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestCall17(t *testing.T) {
141141
}
142142
res := conn.Exec(context.Background(), params.query, opts...)
143143

144-
if assert.NoError(err) {
144+
if assert.NoError(res.Error) {
145145
assert.Equal(params.expectedData, res.Data)
146146
assert.Equal(params.expectedRawData, res.RawData)
147147
}

call_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestCall(t *testing.T) {
104104
}
105105
res := conn.Exec(context.Background(), params.query, opts...)
106106

107-
if assert.NoError(err) {
107+
if assert.NoError(res.Error) {
108108
assert.Equal(params.expectedData, res.Data)
109109
assert.Equal(params.expectedRawData, res.RawData)
110110
}

connect_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tarantool
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -118,3 +119,51 @@ func TestConnectOptionsDSN(t *testing.T) {
118119
}
119120

120121
}
122+
123+
// TestConnectionWithDefaultResultUnmarshalMode tests that
124+
// overwriting the result' unmarshal mode doesn't interferer with internal queries
125+
// like auth and schema pulling.
126+
func TestConnectionWithDefaultResultUnmarshalMode(t *testing.T) {
127+
assert := assert.New(t)
128+
require := require.New(t)
129+
130+
config := `
131+
local s = box.schema.space.create('tester', {id = 42})
132+
s:create_index('tester_id', {
133+
type = 'hash',
134+
parts = {1, 'NUM'}
135+
})
136+
local t = s:insert({33, 45})
137+
138+
box.schema.user.create("tester", {password = "12345678"})
139+
box.schema.user.grant('tester', 'read', 'space', 'tester')
140+
`
141+
142+
box, err := NewBox(config, nil)
143+
require.NoError(err)
144+
defer box.Close()
145+
146+
conn, err := Connect(box.Addr(), &Options{
147+
DefaultSpace: "tester",
148+
User: "tester",
149+
Password: "12345678",
150+
ResultUnmarshalMode: ResultAsRawData,
151+
})
152+
require.NoError(err)
153+
defer conn.Close()
154+
155+
res := conn.Exec(context.Background(), &Select{
156+
Key: 33,
157+
Index: "tester_id",
158+
})
159+
require.NoError(res.Error)
160+
assert.Nil(res.Data)
161+
assert.Equal([]interface{}{[]interface{}{int64(33), int64(45)}}, res.RawData)
162+
163+
tuples, err := conn.Execute(&Select{
164+
Key: 33,
165+
Index: "tester_id",
166+
})
167+
require.NoError(err)
168+
assert.Equal([][]interface{}{{int64(33), int64(45)}}, tuples)
169+
}

connection.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Options struct {
3737
// that can be added to packet pool.
3838
// If the packet size is 0, option is ignored.
3939
PoolMaxPacketSize int
40+
41+
ResultUnmarshalMode resultUnmarshalMode // Result unmarshal mode for user made requests
4042
}
4143

4244
type Greeting struct {
@@ -57,14 +59,15 @@ type Connection struct {
5759
ccw io.Writer
5860

5961
// options
60-
queryTimeout time.Duration
61-
greeting *Greeting
62-
packData *packData
63-
remoteAddr string
64-
firstError error
65-
firstErrorLock *sync.Mutex
66-
perf PerfCount
67-
poolMaxPacketSize int
62+
queryTimeout time.Duration
63+
greeting *Greeting
64+
packData *packData
65+
remoteAddr string
66+
firstError error
67+
firstErrorLock *sync.Mutex
68+
perf PerfCount
69+
poolMaxPacketSize int
70+
resultUnmarshalMode resultUnmarshalMode
6871
}
6972

7073
// Connect to tarantool instance with options using the provided context.
@@ -125,16 +128,17 @@ func newConn(ctx context.Context, scheme, addr string, opts Options) (conn *Conn
125128
}()
126129

127130
conn = &Connection{
128-
remoteAddr: addr,
129-
requests: newRequestMap(),
130-
writeChan: make(chan *request, 256),
131-
exit: make(chan bool),
132-
closed: make(chan bool),
133-
firstErrorLock: &sync.Mutex{},
134-
packData: newPackData(opts.DefaultSpace),
135-
queryTimeout: opts.QueryTimeout,
136-
perf: opts.Perf,
137-
poolMaxPacketSize: opts.PoolMaxPacketSize,
131+
remoteAddr: addr,
132+
requests: newRequestMap(),
133+
writeChan: make(chan *request, 256),
134+
exit: make(chan bool),
135+
closed: make(chan bool),
136+
firstErrorLock: &sync.Mutex{},
137+
packData: newPackData(opts.DefaultSpace),
138+
queryTimeout: opts.QueryTimeout,
139+
perf: opts.Perf,
140+
poolMaxPacketSize: opts.PoolMaxPacketSize,
141+
resultUnmarshalMode: opts.ResultUnmarshalMode,
138142
}
139143

140144
d := &net.Dialer{

execute.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (conn *Connection) Exec(ctx context.Context, q Query, options ...ExecOption
140140

141141
request := requestPool.Get()
142142
request.replyChan = replyChan
143+
request.resultMode = conn.resultUnmarshalMode // could also by overwritten by options
143144
for i := 0; i < len(options); i++ {
144145
options[i].apply(request)
145146
}
@@ -195,6 +196,7 @@ func (conn *Connection) ExecAsync(
195196
request := requestPool.Get()
196197
request.opaque = opaque
197198
request.replyChan = replyChan
199+
request.resultMode = conn.resultUnmarshalMode // could also by overwritten by options
198200
for i := 0; i < len(options); i++ {
199201
options[i].apply(request)
200202
}
@@ -206,6 +208,6 @@ func (conn *Connection) ExecAsync(
206208
}
207209

208210
func (conn *Connection) Execute(q Query) ([][]interface{}, error) {
209-
res := conn.Exec(context.Background(), q)
211+
res := conn.Exec(context.Background(), q, ResultModeExecOption(ResultDefaultMode))
210212
return res.Data, res.Error
211213
}

0 commit comments

Comments
 (0)