Skip to content

Commit b6b3ca7

Browse files
committed
api: use iproto.Feature insted of ProtocolFeature
Replaced the local `ProtocolFeature` type with the `iproto.Feature`. Closes #337
1 parent f15fb8f commit b6b3ca7

14 files changed

+97
-104
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
3939
`pool.Connect` and `pool.Add` now accept context as first argument, which
4040
user may cancel in process. If `pool.Connect` is canceled in progress, an
4141
error will be returned. All created connections will be closed.
42+
- `iproto.Feature` type now used instead of `ProtocolFeature` (#337)
4243

4344
### Deprecated
4445

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ func subscribeWatchChannel(conn *Connection, key string) (chan watchState, error
14251425
return st, nil
14261426
}
14271427

1428-
func isFeatureInSlice(expected ProtocolFeature, actualSlice []ProtocolFeature) bool {
1428+
func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) bool {
14291429
for _, actual := range actualSlice {
14301430
if expected == actual {
14311431
return true

connection_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7+
"github.com/tarantool/go-iproto"
78

89
. "github.com/tarantool/go-tarantool/v2"
910
)
@@ -12,20 +13,20 @@ func TestOptsClonePreservesRequiredProtocolFeatures(t *testing.T) {
1213
original := Opts{
1314
RequiredProtocolInfo: ProtocolInfo{
1415
Version: ProtocolVersion(100),
15-
Features: []ProtocolFeature{ProtocolFeature(99), ProtocolFeature(100)},
16+
Features: []iproto.Feature{iproto.Feature(99), iproto.Feature(100)},
1617
},
1718
}
1819

1920
origCopy := original.Clone()
2021

21-
original.RequiredProtocolInfo.Features[1] = ProtocolFeature(98)
22+
original.RequiredProtocolInfo.Features[1] = iproto.Feature(98)
2223

2324
require.Equal(t,
2425
origCopy,
2526
Opts{
2627
RequiredProtocolInfo: ProtocolInfo{
2728
Version: ProtocolVersion(100),
28-
Features: []ProtocolFeature{ProtocolFeature(99), ProtocolFeature(100)},
29+
Features: []iproto.Feature{iproto.Feature(99), iproto.Feature(100)},
2930
},
3031
})
3132
}

dial_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
16+
"github.com/tarantool/go-iproto"
1617

1718
"github.com/tarantool/go-tarantool/v2"
1819
"github.com/tarantool/go-tarantool/v2/test_helpers"
@@ -72,7 +73,7 @@ func TestDialer_Dial_passedOpts(t *testing.T) {
7273
RequiredProtocol: tarantool.ProtocolInfo{
7374
Auth: tarantool.ChapSha1Auth,
7475
Version: 33,
75-
Features: []tarantool.ProtocolFeature{
76+
Features: []iproto.Feature{
7677
tarantool.ErrorExtensionFeature,
7778
},
7879
},
@@ -302,7 +303,7 @@ func TestConn_ProtocolInfo(t *testing.T) {
302303
info := tarantool.ProtocolInfo{
303304
Auth: tarantool.ChapSha1Auth,
304305
Version: 33,
305-
Features: []tarantool.ProtocolFeature{
306+
Features: []iproto.Feature{
306307
tarantool.ErrorExtensionFeature,
307308
},
308309
}

example_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/tarantool/go-iproto"
9+
810
"github.com/tarantool/go-tarantool/v2"
911
"github.com/tarantool/go-tarantool/v2/test_helpers"
1012
)
@@ -627,12 +629,12 @@ func ExampleProtocolVersion() {
627629
}
628630
// Output:
629631
// Connector client protocol version: 6
630-
// Connector client protocol feature: StreamsFeature
631-
// Connector client protocol feature: TransactionsFeature
632-
// Connector client protocol feature: ErrorExtensionFeature
633-
// Connector client protocol feature: WatchersFeature
634-
// Connector client protocol feature: PaginationFeature
635-
// Connector client protocol feature: WatchOnceFeature
632+
// Connector client protocol feature: IPROTO_FEATURE_STREAMS
633+
// Connector client protocol feature: IPROTO_FEATURE_TRANSACTIONS
634+
// Connector client protocol feature: IPROTO_FEATURE_ERROR_EXTENSION
635+
// Connector client protocol feature: IPROTO_FEATURE_WATCHERS
636+
// Connector client protocol feature: IPROTO_FEATURE_PAGINATION
637+
// Connector client protocol feature: IPROTO_FEATURE_WATCH_ONCE
636638
}
637639

638640
func getTestTxnOpts() tarantool.Opts {
@@ -641,7 +643,7 @@ func getTestTxnOpts() tarantool.Opts {
641643
// Assert that server supports expected protocol features
642644
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
643645
Version: tarantool.ProtocolVersion(1),
644-
Features: []tarantool.ProtocolFeature{
646+
Features: []iproto.Feature{
645647
tarantool.StreamsFeature,
646648
tarantool.TransactionsFeature,
647649
},
@@ -1168,7 +1170,7 @@ func ExampleConnection_NewWatcher() {
11681170
Pass: "test",
11691171
// You need to require the feature to create a watcher.
11701172
RequiredProtocolInfo: tarantool.ProtocolInfo{
1171-
Features: []tarantool.ProtocolFeature{tarantool.WatchersFeature},
1173+
Features: []iproto.Feature{tarantool.WatchersFeature},
11721174
},
11731175
}
11741176
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)

pool/connection_pool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,8 @@ func (p *ConnectionPool) NewWatcher(key string,
938938
}
939939
}
940940
if !watchersRequired {
941-
return nil, errors.New("the feature WatchersFeature must be " +
942-
"required by connection options to create a watcher")
941+
return nil, errors.New("the feature IPROTO_FEATURE_WATCHERS must " +
942+
"be required by connection options to create a watcher")
943943
}
944944

945945
watcher := &poolWatcher{

pool/connection_pool_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
"github.com/tarantool/go-iproto"
1718
"github.com/vmihailenco/msgpack/v5"
1819

1920
"github.com/tarantool/go-tarantool/v2"
@@ -2832,7 +2833,7 @@ func TestConnectionPool_NewWatcher_noWatchersFeature(t *testing.T) {
28322833
roles := []bool{true, false, false, true, true}
28332834

28342835
opts := connOpts.Clone()
2835-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{}
2836+
opts.RequiredProtocolInfo.Features = []iproto.Feature{}
28362837
err := test_helpers.SetClusterRO(servers, opts, roles)
28372838
require.Nilf(t, err, "fail to set roles for cluster")
28382839

@@ -2847,8 +2848,8 @@ func TestConnectionPool_NewWatcher_noWatchersFeature(t *testing.T) {
28472848
func(event tarantool.WatchEvent) {}, pool.ANY)
28482849
require.Nilf(t, watcher, "watcher must not be created")
28492850
require.NotNilf(t, err, "an error is expected")
2850-
expected := "the feature WatchersFeature must be required by connection " +
2851-
"options to create a watcher"
2851+
expected := "the feature IPROTO_FEATURE_WATCHERS must be required by " +
2852+
"connection options to create a watcher"
28522853
require.Equal(t, expected, err.Error())
28532854
}
28542855

@@ -2860,7 +2861,7 @@ func TestConnectionPool_NewWatcher_modes(t *testing.T) {
28602861
roles := []bool{true, false, false, true, true}
28612862

28622863
opts := connOpts.Clone()
2863-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
2864+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
28642865
tarantool.WatchersFeature,
28652866
}
28662867
err := test_helpers.SetClusterRO(servers, opts, roles)
@@ -2941,7 +2942,7 @@ func TestConnectionPool_NewWatcher_update(t *testing.T) {
29412942
roles := []bool{true, false, false, true, true}
29422943

29432944
opts := connOpts.Clone()
2944-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
2945+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
29452946
tarantool.WatchersFeature,
29462947
}
29472948
err := test_helpers.SetClusterRO(servers, opts, roles)
@@ -3030,7 +3031,7 @@ func TestWatcher_Unregister(t *testing.T) {
30303031
roles := []bool{true, false, false, true, true}
30313032

30323033
opts := connOpts.Clone()
3033-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
3034+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
30343035
tarantool.WatchersFeature,
30353036
}
30363037
err := test_helpers.SetClusterRO(servers, opts, roles)
@@ -3091,7 +3092,7 @@ func TestConnectionPool_NewWatcher_concurrent(t *testing.T) {
30913092
roles := []bool{true, false, false, true, true}
30923093

30933094
opts := connOpts.Clone()
3094-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
3095+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
30953096
tarantool.WatchersFeature,
30963097
}
30973098
err := test_helpers.SetClusterRO(servers, opts, roles)
@@ -3133,7 +3134,7 @@ func TestWatcher_Unregister_concurrent(t *testing.T) {
31333134
roles := []bool{true, false, false, true, true}
31343135

31353136
opts := connOpts.Clone()
3136-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
3137+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
31373138
tarantool.WatchersFeature,
31383139
}
31393140
err := test_helpers.SetClusterRO(servers, opts, roles)

pool/example_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package pool_test
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

8+
"github.com/tarantool/go-iproto"
9+
710
"github.com/tarantool/go-tarantool/v2"
811
"github.com/tarantool/go-tarantool/v2/pool"
912
"github.com/tarantool/go-tarantool/v2/test_helpers"
@@ -92,7 +95,7 @@ func ExampleConnectionPool_NewWatcher() {
9295
const value = "bar"
9396

9497
opts := connOpts.Clone()
95-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
98+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
9699
tarantool.WatchersFeature,
97100
}
98101

@@ -123,7 +126,7 @@ func ExampleConnectionPool_NewWatcher_noWatchersFeature() {
123126
const key = "foo"
124127

125128
opts := connOpts.Clone()
126-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{}
129+
opts.RequiredProtocolInfo.Features = []iproto.Feature{}
127130

128131
connPool, err := examplePool(testRoles, connOpts)
129132
if err != nil {
@@ -134,10 +137,17 @@ func ExampleConnectionPool_NewWatcher_noWatchersFeature() {
134137
callback := func(event tarantool.WatchEvent) {}
135138
watcher, err := connPool.NewWatcher(key, callback, pool.ANY)
136139
fmt.Println(watcher)
137-
fmt.Println(err)
140+
if err != nil {
141+
str := err.Error()
142+
fmt.Println(strings.Trim(str[:56], " "))
143+
fmt.Println(str[56:])
144+
} else {
145+
fmt.Println(err)
146+
}
138147
// Output:
139148
// <nil>
140-
// the feature WatchersFeature must be required by connection options to create a watcher
149+
// the feature IPROTO_FEATURE_WATCHERS must be required by
150+
// connection options to create a watcher
141151
}
142152

143153
func getTestTxnOpts() tarantool.Opts {
@@ -146,7 +156,7 @@ func getTestTxnOpts() tarantool.Opts {
146156
// Assert that server supports expected protocol features
147157
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
148158
Version: tarantool.ProtocolVersion(1),
149-
Features: []tarantool.ProtocolFeature{
159+
Features: []iproto.Feature{
150160
tarantool.StreamsFeature,
151161
tarantool.TransactionsFeature,
152162
},

protocol.go

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tarantool
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/tarantool/go-iproto"
87
"github.com/vmihailenco/msgpack/v5"
@@ -11,17 +10,14 @@ import (
1110
// ProtocolVersion type stores Tarantool protocol version.
1211
type ProtocolVersion uint64
1312

14-
// ProtocolVersion type stores a Tarantool protocol feature.
15-
type ProtocolFeature iproto.Feature
16-
1713
// ProtocolInfo type aggregates Tarantool protocol version and features info.
1814
type ProtocolInfo struct {
1915
// Auth is an authentication method.
2016
Auth Auth
2117
// Version is the supported protocol version.
2218
Version ProtocolVersion
2319
// Features are supported protocol features.
24-
Features []ProtocolFeature
20+
Features []iproto.Feature
2521
}
2622

2723
// Clone returns an exact copy of the ProtocolInfo object.
@@ -30,7 +26,7 @@ func (info ProtocolInfo) Clone() ProtocolInfo {
3026
infoCopy := info
3127

3228
if info.Features != nil {
33-
infoCopy.Features = make([]ProtocolFeature, len(info.Features))
29+
infoCopy.Features = make([]iproto.Feature, len(info.Features))
3430
copy(infoCopy.Features, info.Features)
3531
}
3632

@@ -39,44 +35,23 @@ func (info ProtocolInfo) Clone() ProtocolInfo {
3935

4036
const (
4137
// StreamsFeature represents streams support (supported by connector).
42-
StreamsFeature ProtocolFeature = 0
38+
StreamsFeature iproto.Feature = 0
4339
// TransactionsFeature represents interactive transactions support.
4440
// (supported by connector).
45-
TransactionsFeature ProtocolFeature = 1
41+
TransactionsFeature iproto.Feature = 1
4642
// ErrorExtensionFeature represents support of MP_ERROR objects over MessagePack
4743
// (supported by connector).
48-
ErrorExtensionFeature ProtocolFeature = 2
44+
ErrorExtensionFeature iproto.Feature = 2
4945
// WatchersFeature represents support of watchers
5046
// (supported by connector).
51-
WatchersFeature ProtocolFeature = 3
47+
WatchersFeature iproto.Feature = 3
5248
// PaginationFeature represents support of pagination
5349
// (supported by connector).
54-
PaginationFeature ProtocolFeature = 4
50+
PaginationFeature iproto.Feature = 4
5551
// WatchOnceFeature represents support of WatchOnce request types.
56-
WatchOnceFeature ProtocolFeature = 6
52+
WatchOnceFeature iproto.Feature = 6
5753
)
5854

59-
// String returns the name of a Tarantool feature.
60-
// If value X is not a known feature, returns "Unknown feature (code X)" string.
61-
func (ftr ProtocolFeature) String() string {
62-
switch ftr {
63-
case StreamsFeature:
64-
return "StreamsFeature"
65-
case TransactionsFeature:
66-
return "TransactionsFeature"
67-
case ErrorExtensionFeature:
68-
return "ErrorExtensionFeature"
69-
case WatchersFeature:
70-
return "WatchersFeature"
71-
case PaginationFeature:
72-
return "PaginationFeature"
73-
case WatchOnceFeature:
74-
return "WatchOnceFeature"
75-
default:
76-
return fmt.Sprintf("Unknown feature (code %d)", ftr)
77-
}
78-
}
79-
8055
var clientProtocolInfo ProtocolInfo = ProtocolInfo{
8156
// Protocol version supported by connector. Version 3
8257
// was introduced in Tarantool 2.10.0, version 4 was
@@ -94,7 +69,7 @@ var clientProtocolInfo ProtocolInfo = ProtocolInfo{
9469
// connector since 1.11.0.
9570
// WatchOnce request type was introduces in protocol version 6
9671
// (Tarantool 3.0.0), in connector since 2.0.0.
97-
Features: []ProtocolFeature{
72+
Features: []iproto.Feature{
9873
StreamsFeature,
9974
TransactionsFeature,
10075
ErrorExtensionFeature,

0 commit comments

Comments
 (0)