@@ -18,9 +18,13 @@ import (
1818 "github.com/tarantool/go-storage/watch"
1919)
2020
21- // Client defines the minimal interface needed for etcd operations.
21+ // Client defines the minimal interface needed for etcd operations. It
22+ // is compatible with etcdclientv3.Client.
23+ //
2224// This allows for easier testing and mock implementations.
2325type Client interface {
26+ Watcher
27+
2428 // Txn creates a new transaction.
2529 Txn (ctx context.Context ) etcd.Txn
2630}
5963 errUnsupportedOperationType = errors .New ("unsupported operation type" )
6064)
6165
62- // etcdClientAdapter wraps etcd.Client to implement our Client interface.
63- type etcdClientAdapter struct {
64- client * etcd.Client
65- }
66-
67- func (a * etcdClientAdapter ) Txn (ctx context.Context ) etcd.Txn {
68- return a .client .Txn (ctx )
69- }
70-
7166// etcdWatcherAdapter wraps etcd.Watcher to implement our Watcher interface.
7267type etcdWatcherAdapter struct {
7368 watcher etcd.Watcher
@@ -85,15 +80,7 @@ func (a *etcdWatcherAdapter) Close() error {
8580type etcdWatcherFactory struct {}
8681
8782func (f * etcdWatcherFactory ) NewWatcher (client Client ) Watcher {
88- // For etcd clients, we need access to the underlying client.
89- if adapter , ok := client .(* etcdClientAdapter ); ok {
90- return & etcdWatcherAdapter {
91- watcher : etcd .NewWatcher (adapter .client ),
92- }
93- }
94-
95- // For other implementations, return a no-op watcher.
96- return & noopWatcher {}
83+ return client
9784}
9885
9986// noopWatcher is a no-op implementation of Watcher for non-etcd clients.
@@ -112,9 +99,9 @@ func (w *noopWatcher) Close() error {
11299
113100// New creates a new etcd driver instance using an existing etcd client.
114101// The client should be properly configured and connected to an etcd cluster.
115- func New (client * etcd. Client ) * Driver {
102+ func New (client Client ) * Driver {
116103 return & Driver {
117- client : & etcdClientAdapter { client : client } ,
104+ client : client ,
118105 watcherFactory : & etcdWatcherFactory {},
119106 }
120107}
0 commit comments