Merge branch 'master' into dependabot/go_modules/go.unistack.org/micro/v3-3.8.11

This commit is contained in:
Василий Толстов 2021-11-24 15:24:22 +03:00 committed by GitHub
commit 5ea9bb0e7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 { func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
//options := store.NewReadOptions(opts...) options := store.NewExistsOptions(opts...)
//if len(options.Table) == 0 { if len(options.Namespace) == 0 {
// options.Table = r.opts.Table options.Namespace = r.opts.Namespace
//} }
rkey := fmt.Sprintf("%s%s", r.opts.Table, key) rkey := fmt.Sprintf("%s%s", options.Namespace, key)
st, err := r.cli.Exists(ctx, rkey).Result() st, err := r.cli.Exists(ctx, rkey).Result()
if err != nil { if err != nil {
return err 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 { func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
options := store.NewReadOptions(opts...) options := store.NewReadOptions(opts...)
if len(options.Table) == 0 { if len(options.Namespace) == 0 {
options.Table = r.opts.Table 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() buf, err := r.cli.Get(ctx, rkey).Bytes()
if err != nil && err == redis.Nil { if err != nil && err == redis.Nil {
return store.ErrNotFound 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 { func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
options := store.NewDeleteOptions(opts...) options := store.NewDeleteOptions(opts...)
if len(options.Table) == 0 { if len(options.Namespace) == 0 {
options.Table = r.opts.Table 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() return r.cli.Del(ctx, rkey).Err()
} }
func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error { func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
options := store.NewWriteOptions(opts...) options := store.NewWriteOptions(opts...)
if len(options.Table) == 0 { if len(options.Namespace) == 0 {
options.Table = r.opts.Table 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) buf, err := r.opts.Codec.Marshal(val)
if err != nil { if err != nil {
return err 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) { func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
options := store.NewListOptions(opts...) options := store.NewListOptions(opts...)
if len(options.Table) == 0 { if len(options.Namespace) == 0 {
options.Table = r.opts.Table options.Namespace = r.opts.Namespace
} }
// TODO: add support for prefix/suffix/limit // TODO: add support for prefix/suffix/limit
@ -148,7 +148,7 @@ func (r *rkv) configure() error {
var redisClusterOptions *redis.ClusterOptions var redisClusterOptions *redis.ClusterOptions
var err error var err error
nodes := r.opts.Nodes nodes := r.opts.Addrs
if len(nodes) == 0 { if len(nodes) == 0 {
nodes = []string{"redis://127.0.0.1:6379"} nodes = []string{"redis://127.0.0.1:6379"}

View File

@ -27,36 +27,46 @@ func Test_rkv_configure(t *testing.T) {
wantErr bool wantErr bool
want wantValues 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{ wantErr: false, want: wantValues{
username: "", username: "",
password: "", password: "",
address: "127.0.0.1:6379", 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{ wantErr: false, want: wantValues{
username: "", username: "",
password: "", password: "",
address: "127.0.0.1:6379", 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{ wantErr: false, want: wantValues{
username: "", username: "",
password: "", password: "",
address: "127.0.0.1:6379", 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{ wantErr: false, want: wantValues{
username: "", username: "",
password: "password", password: "password",
address: "redis:6379", 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{ wantErr: false, want: wantValues{
username: "username", username: "username",
password: "password", password: "password",
address: "redis:6379", address: "redis:6379",
}}, },
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -84,7 +94,7 @@ func Test_Store(t *testing.T) {
// r.options = store.Options{Nodes: []string{"redis://:password@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.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 { if err := r.configure(); err != nil {
t.Fatal(err) t.Fatal(err)