fix for latest micro

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-11-24 15:23:47 +03:00
parent f38d49c30b
commit 764e5dc1d7
2 changed files with 41 additions and 31 deletions

View File

@ -40,11 +40,11 @@ func (r *rkv) Disconnect(ctx context.Context) error {
}
func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
//options := store.NewReadOptions(opts...)
//if len(options.Table) == 0 {
// options.Table = r.opts.Table
//}
rkey := fmt.Sprintf("%s%s", r.opts.Table, key)
options := store.NewExistsOptions(opts...)
if len(options.Namespace) == 0 {
options.Namespace = r.opts.Namespace
}
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
st, err := r.cli.Exists(ctx, rkey).Result()
if err != nil {
return err
@ -57,11 +57,11 @@ func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption
func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
options := store.NewReadOptions(opts...)
if len(options.Table) == 0 {
options.Table = r.opts.Table
if len(options.Namespace) == 0 {
options.Namespace = r.opts.Namespace
}
rkey := fmt.Sprintf("%s%s", options.Table, key)
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
buf, err := r.cli.Get(ctx, rkey).Bytes()
if err != nil && err == redis.Nil {
return store.ErrNotFound
@ -89,21 +89,21 @@ func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...sto
func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
options := store.NewDeleteOptions(opts...)
if len(options.Table) == 0 {
options.Table = r.opts.Table
if len(options.Namespace) == 0 {
options.Namespace = r.opts.Namespace
}
rkey := fmt.Sprintf("%s%s", options.Table, key)
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
return r.cli.Del(ctx, rkey).Err()
}
func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
options := store.NewWriteOptions(opts...)
if len(options.Table) == 0 {
options.Table = r.opts.Table
if len(options.Namespace) == 0 {
options.Namespace = r.opts.Namespace
}
rkey := fmt.Sprintf("%s%s", options.Table, key)
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
buf, err := r.opts.Codec.Marshal(val)
if err != nil {
return err
@ -114,8 +114,8 @@ func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...st
func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
options := store.NewListOptions(opts...)
if len(options.Table) == 0 {
options.Table = r.opts.Table
if len(options.Namespace) == 0 {
options.Namespace = r.opts.Namespace
}
// TODO: add support for prefix/suffix/limit
@ -148,7 +148,7 @@ func (r *rkv) configure() error {
var redisClusterOptions *redis.ClusterOptions
var err error
nodes := r.opts.Nodes
nodes := r.opts.Addrs
if len(nodes) == 0 {
nodes = []string{"redis://127.0.0.1:6379"}
@ -171,7 +171,7 @@ func (r *rkv) configure() error {
if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 {
redisOptions, err = redis.ParseURL(nodes[0])
if err != nil {
//Backwards compatibility
// Backwards compatibility
redisOptions = &redis.Options{
Addr: nodes[0],
Password: "", // no password set

View File

@ -27,36 +27,46 @@ func Test_rkv_configure(t *testing.T) {
wantErr bool
want wantValues
}{
{name: "No Url", fields: fields{options: store.Options{}, Client: nil},
{
name: "No Url", fields: fields{options: store.Options{}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "",
address: "127.0.0.1:6379",
}},
{name: "legacy Url", fields: fields{options: store.Options{Nodes: []string{"127.0.0.1:6379"}}, Client: nil},
},
},
{
name: "legacy Url", fields: fields{options: store.Options{Addrs: []string{"127.0.0.1:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "",
address: "127.0.0.1:6379",
}},
{name: "New Url", fields: fields{options: store.Options{Nodes: []string{"redis://127.0.0.1:6379"}}, Client: nil},
},
},
{
name: "New Url", fields: fields{options: store.Options{Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "",
address: "127.0.0.1:6379",
}},
{name: "Url with Pwd", fields: fields{options: store.Options{Nodes: []string{"redis://:password@redis:6379"}}, Client: nil},
},
},
{
name: "Url with Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://:password@redis:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "password",
address: "redis:6379",
}},
{name: "Url with username and Pwd", fields: fields{options: store.Options{Nodes: []string{"redis://username:password@redis:6379"}}, Client: nil},
},
},
{
name: "Url with username and Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "username",
password: "password",
address: "redis:6379",
}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -81,10 +91,10 @@ func Test_Store(t *testing.T) {
}
r := new(rkv)
//r.options = store.Options{Nodes: []string{"redis://:password@127.0.0.1:6379"}}
//r.options = store.Options{Nodes: []string{"127.0.0.1:6379"}}
// r.options = store.Options{Nodes: []string{"redis://:password@127.0.0.1:6379"}}
// r.options = store.Options{Nodes: []string{"127.0.0.1:6379"}}
r.opts = store.NewOptions(store.Nodes(os.Getenv("STORE_NODES")))
r.opts = store.NewOptions(store.Addrs(os.Getenv("STORE_NODES")))
if err := r.configure(); err != nil {
t.Fatal(err)