From d667bbee0cb0f08aa407cbeaededfedbdc629c6d Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Wed, 28 Feb 2024 15:06:29 +0300 Subject: [PATCH 1/4] add option metadata --- options/options.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/options/options.go b/options/options.go index cda572a6..df38591f 100644 --- a/options/options.go +++ b/options/options.go @@ -157,6 +157,22 @@ func Metadata(md metadata.Metadata) Option { } } +func MetadataAny(md any) Option { + result := metadata.Metadata{} + switch vt := md.(type) { + case metadata.Metadata: + result = metadata.Copy(vt) + case []string: + for index := 0; index < len(vt); index += 2 { + result[vt[index]] = result[vt[index]] + } + } + + return func(src interface{}) error { + return Set(src, result, ".Metadata") + } +} + // Namespace to use func Namespace(ns string) Option { return func(src interface{}) error { From 47940a5ca2c31e27e611aa0e05a87f6b2ae900d0 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Wed, 28 Feb 2024 15:06:29 +0300 Subject: [PATCH 2/4] add option metadata --- options/options.go | 16 ++++++++++ options/options_test.go | 69 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/options/options.go b/options/options.go index cda572a6..5749c15e 100644 --- a/options/options.go +++ b/options/options.go @@ -157,6 +157,22 @@ func Metadata(md metadata.Metadata) Option { } } +func MetadataAny(md any) Option { + result := metadata.Metadata{} + switch vt := md.(type) { + case metadata.Metadata: + result = metadata.Copy(vt) + case map[string]string: + result = metadata.Copy(vt) + case []string: + result.Set(vt...) + } + + return func(src interface{}) error { + return Set(src, result, ".Metadata") + } +} + // Namespace to use func Namespace(ns string) Option { return func(src interface{}) error { diff --git a/options/options_test.go b/options/options_test.go index 7d3b6a62..c4b3677c 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -1,10 +1,13 @@ package options_test import ( + "fmt" "testing" "go.unistack.org/micro/v4/codec" + "go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v4/options" + "go.unistack.org/micro/v4/util/reflect" ) func TestAddress(t *testing.T) { @@ -84,3 +87,69 @@ func TestLabels(t *testing.T) { t.Fatal("failed to set labels") } } + +func TestMetadataAny(t *testing.T) { + type s struct { + Metadata metadata.Metadata + } + + testCases := []struct { + Name string + Data any + Expected metadata.Metadata + }{ + { + "strings_even", + []string{"key1", "val1", "key2", "val2"}, + metadata.Metadata{ + "Key1": "val1", + "Key2": "val2", + }, + }, + { + "strings_odd", + []string{"key1", "val1", "key2"}, + metadata.Metadata{ + "Key1": "val1", + }, + }, + { + "map", + map[string]string{ + "key1": "val1", + "key2": "val2", + }, + metadata.Metadata{ + "Key1": "val1", + "Key2": "val2", + }, + }, + { + "metadata.Metadata", + metadata.Metadata{ + "key1": "val1", + "key2": "val2", + }, + metadata.Metadata{ + "Key1": "val1", + "Key2": "val2", + }, + }, + } + + for _, tt := range testCases { + t.Run(tt.Name, func(t *testing.T) { + src := &s{} + var opts []options.Option + opts = append(opts, options.MetadataAny(tt.Data)) + for _, o := range opts { + if err := o(src); err != nil { + t.Fatal(err) + } + if !reflect.Equal(tt.Expected, src.Metadata) { + t.Fatal(fmt.Sprintf("expected: %v, actual: %v", tt.Expected, src.Metadata)) + } + } + }) + } +} From e6feca2fb1014c42d6b1b200919fc12e2e2c9e6c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 28 Feb 2024 23:50:38 +0300 Subject: [PATCH 3/4] fixup Signed-off-by: Vasiliy Tolstov --- options/options.go | 8 +------- options/options_test.go | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/options/options.go b/options/options.go index 5749c15e..226fd5e4 100644 --- a/options/options.go +++ b/options/options.go @@ -151,13 +151,7 @@ func ContentType(ct string) Option { } // Metadata pass additional metadata -func Metadata(md metadata.Metadata) Option { - return func(src interface{}) error { - return Set(src, metadata.Copy(md), ".Metadata") - } -} - -func MetadataAny(md any) Option { +func Metadata(md any) Option { result := metadata.Metadata{} switch vt := md.(type) { case metadata.Metadata: diff --git a/options/options_test.go b/options/options_test.go index c4b3677c..1bb0ce23 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -141,7 +141,7 @@ func TestMetadataAny(t *testing.T) { t.Run(tt.Name, func(t *testing.T) { src := &s{} var opts []options.Option - opts = append(opts, options.MetadataAny(tt.Data)) + opts = append(opts, options.Metadata(tt.Data)) for _, o := range opts { if err := o(src); err != nil { t.Fatal(err) From f06e0b21e8b055287b4d912aa0616dc040a129f9 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Thu, 29 Feb 2024 01:27:33 +0300 Subject: [PATCH 4/4] add variadic (#302) Co-authored-by: Vasiliy Tolstov Reviewed-on: https://git.unistack.org/unistack-org/micro/pulls/302 Co-authored-by: Evstigneev Denis Co-committed-by: Evstigneev Denis --- options/options.go | 29 ++++++++++++++++++++--------- options/options_test.go | 18 ++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/options/options.go b/options/options.go index 226fd5e4..cbcf15fd 100644 --- a/options/options.go +++ b/options/options.go @@ -151,15 +151,26 @@ func ContentType(ct string) Option { } // Metadata pass additional metadata -func Metadata(md any) Option { - result := metadata.Metadata{} - switch vt := md.(type) { - case metadata.Metadata: - result = metadata.Copy(vt) - case map[string]string: - result = metadata.Copy(vt) - case []string: - result.Set(vt...) +func Metadata(md ...any) Option { + var result metadata.Metadata + if len(md) == 1 { + switch vt := md[0].(type) { + case metadata.Metadata: + result = metadata.Copy(vt) + case map[string]string: + result = metadata.Copy(vt) + default: + result = metadata.New(0) + } + } else { + result = metadata.New(len(md) / 2) + for idx := 0; idx < len(md)/2; idx += 2 { + k, kok := md[idx].(string) + v, vok := md[idx+1].(string) + if kok && vok { + result.Set(k, v) + } + } } return func(src interface{}) error { diff --git a/options/options_test.go b/options/options_test.go index 1bb0ce23..a2f26e8a 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -100,7 +100,7 @@ func TestMetadataAny(t *testing.T) { }{ { "strings_even", - []string{"key1", "val1", "key2", "val2"}, + []any{"key1", "val1", "key2", "val2"}, metadata.Metadata{ "Key1": "val1", "Key2": "val2", @@ -108,18 +108,18 @@ func TestMetadataAny(t *testing.T) { }, { "strings_odd", - []string{"key1", "val1", "key2"}, + []any{"key1", "val1", "key2"}, metadata.Metadata{ "Key1": "val1", }, }, { - "map", - map[string]string{ + Name: "map", + Data: map[string]string{ "key1": "val1", "key2": "val2", }, - metadata.Metadata{ + Expected: metadata.Metadata{ "Key1": "val1", "Key2": "val2", }, @@ -141,7 +141,13 @@ func TestMetadataAny(t *testing.T) { t.Run(tt.Name, func(t *testing.T) { src := &s{} var opts []options.Option - opts = append(opts, options.Metadata(tt.Data)) + switch valData := tt.Data.(type) { + case []any: + opts = append(opts, options.Metadata(valData...)) + case map[string]string, metadata.Metadata: + opts = append(opts, options.Metadata(valData)) + } + for _, o := range opts { if err := o(src); err != nil { t.Fatal(err)