From d0847cd46232c4f4f685058187b9e9efac80014a Mon Sep 17 00:00:00 2001 From: woniu317 Date: Fri, 7 Apr 2023 18:09:50 +0800 Subject: [PATCH 1/5] feat: log helper support sprint option (#2616) * feat:add sprint option * fix: with helper need sprint --------- Co-authored-by: zhoushijie --- log/helper.go | 52 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/log/helper.go b/log/helper.go index b01c947ac..cc0cb8c93 100644 --- a/log/helper.go +++ b/log/helper.go @@ -14,8 +14,10 @@ type Option func(*Helper) // Helper is a logger helper. type Helper struct { - logger Logger - msgKey string + logger Logger + msgKey string + sprint func(...interface{}) string + sprintf func(format string, a ...interface{}) string } // WithMessageKey with message key. @@ -25,11 +27,27 @@ func WithMessageKey(k string) Option { } } +// WithSprint with sprint +func WithSprint(sprint func(...interface{}) string) Option { + return func(opts *Helper) { + opts.sprint = sprint + } +} + +// WithSprintf with sprintf +func WithSprintf(sprintf func(format string, a ...interface{}) string) Option { + return func(opts *Helper) { + opts.sprintf = sprintf + } +} + // NewHelper new a logger helper. func NewHelper(logger Logger, opts ...Option) *Helper { options := &Helper{ - msgKey: DefaultMessageKey, // default message key - logger: logger, + msgKey: DefaultMessageKey, // default message key + logger: logger, + sprint: fmt.Sprint, + sprintf: fmt.Sprintf, } for _, o := range opts { o(options) @@ -41,8 +59,10 @@ func NewHelper(logger Logger, opts ...Option) *Helper { // to ctx. The provided ctx must be non-nil. func (h *Helper) WithContext(ctx context.Context) *Helper { return &Helper{ - msgKey: h.msgKey, - logger: WithContext(ctx, h.logger), + msgKey: h.msgKey, + logger: WithContext(ctx, h.logger), + sprint: h.sprint, + sprintf: h.sprintf, } } @@ -53,12 +73,12 @@ func (h *Helper) Log(level Level, keyvals ...interface{}) { // Debug logs a message at debug level. func (h *Helper) Debug(a ...interface{}) { - _ = h.logger.Log(LevelDebug, h.msgKey, fmt.Sprint(a...)) + _ = h.logger.Log(LevelDebug, h.msgKey, h.sprint(a...)) } // Debugf logs a message at debug level. func (h *Helper) Debugf(format string, a ...interface{}) { - _ = h.logger.Log(LevelDebug, h.msgKey, fmt.Sprintf(format, a...)) + _ = h.logger.Log(LevelDebug, h.msgKey, h.sprintf(format, a...)) } // Debugw logs a message at debug level. @@ -68,12 +88,12 @@ func (h *Helper) Debugw(keyvals ...interface{}) { // Info logs a message at info level. func (h *Helper) Info(a ...interface{}) { - _ = h.logger.Log(LevelInfo, h.msgKey, fmt.Sprint(a...)) + _ = h.logger.Log(LevelInfo, h.msgKey, h.sprint(a...)) } // Infof logs a message at info level. func (h *Helper) Infof(format string, a ...interface{}) { - _ = h.logger.Log(LevelInfo, h.msgKey, fmt.Sprintf(format, a...)) + _ = h.logger.Log(LevelInfo, h.msgKey, h.sprintf(format, a...)) } // Infow logs a message at info level. @@ -83,12 +103,12 @@ func (h *Helper) Infow(keyvals ...interface{}) { // Warn logs a message at warn level. func (h *Helper) Warn(a ...interface{}) { - _ = h.logger.Log(LevelWarn, h.msgKey, fmt.Sprint(a...)) + _ = h.logger.Log(LevelWarn, h.msgKey, h.sprint(a...)) } // Warnf logs a message at warnf level. func (h *Helper) Warnf(format string, a ...interface{}) { - _ = h.logger.Log(LevelWarn, h.msgKey, fmt.Sprintf(format, a...)) + _ = h.logger.Log(LevelWarn, h.msgKey, h.sprintf(format, a...)) } // Warnw logs a message at warnf level. @@ -98,12 +118,12 @@ func (h *Helper) Warnw(keyvals ...interface{}) { // Error logs a message at error level. func (h *Helper) Error(a ...interface{}) { - _ = h.logger.Log(LevelError, h.msgKey, fmt.Sprint(a...)) + _ = h.logger.Log(LevelError, h.msgKey, h.sprint(a...)) } // Errorf logs a message at error level. func (h *Helper) Errorf(format string, a ...interface{}) { - _ = h.logger.Log(LevelError, h.msgKey, fmt.Sprintf(format, a...)) + _ = h.logger.Log(LevelError, h.msgKey, h.sprintf(format, a...)) } // Errorw logs a message at error level. @@ -113,13 +133,13 @@ func (h *Helper) Errorw(keyvals ...interface{}) { // Fatal logs a message at fatal level. func (h *Helper) Fatal(a ...interface{}) { - _ = h.logger.Log(LevelFatal, h.msgKey, fmt.Sprint(a...)) + _ = h.logger.Log(LevelFatal, h.msgKey, h.sprint(a...)) os.Exit(1) } // Fatalf logs a message at fatal level. func (h *Helper) Fatalf(format string, a ...interface{}) { - _ = h.logger.Log(LevelFatal, h.msgKey, fmt.Sprintf(format, a...)) + _ = h.logger.Log(LevelFatal, h.msgKey, h.sprintf(format, a...)) os.Exit(1) } From f8c19c37af5ef2fa67721098db8608e1819085ce Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 8 Apr 2023 22:57:29 +0800 Subject: [PATCH 2/5] build(deps): bump google.golang.org/protobuf in /contrib/log/aliyun (#2751) Bumps [google.golang.org/protobuf](https://github.com/protocolbuffers/protobuf-go) from 1.28.1 to 1.30.0. - [Release notes](https://github.com/protocolbuffers/protobuf-go/releases) - [Changelog](https://github.com/protocolbuffers/protobuf-go/blob/master/release.bash) - [Commits](https://github.com/protocolbuffers/protobuf-go/compare/v1.28.1...v1.30.0) --- updated-dependencies: - dependency-name: google.golang.org/protobuf dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- contrib/log/aliyun/go.mod | 2 +- contrib/log/aliyun/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/log/aliyun/go.mod b/contrib/log/aliyun/go.mod index f75a8148d..1fcdfa34b 100644 --- a/contrib/log/aliyun/go.mod +++ b/contrib/log/aliyun/go.mod @@ -5,7 +5,7 @@ go 1.16 require ( github.com/aliyun/aliyun-log-go-sdk v0.1.43 github.com/go-kratos/kratos/v2 v2.6.1 - google.golang.org/protobuf v1.28.1 + google.golang.org/protobuf v1.30.0 ) replace ( diff --git a/contrib/log/aliyun/go.sum b/contrib/log/aliyun/go.sum index b60a8d88b..cd68d59fe 100644 --- a/contrib/log/aliyun/go.sum +++ b/contrib/log/aliyun/go.sum @@ -481,8 +481,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= -google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From e9870cb48f7f6a9fc0889881c84cd31c357850df Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 8 Apr 2023 22:58:12 +0800 Subject: [PATCH 3/5] build(deps): bump github.com/hashicorp/consul/api (#2733) Bumps [github.com/hashicorp/consul/api](https://github.com/hashicorp/consul) from 1.19.1 to 1.20.0. - [Release notes](https://github.com/hashicorp/consul/releases) - [Changelog](https://github.com/hashicorp/consul/blob/main/CHANGELOG.md) - [Commits](https://github.com/hashicorp/consul/compare/api/v1.19.1...api/v1.20.0) --- updated-dependencies: - dependency-name: github.com/hashicorp/consul/api dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: haiyux --- contrib/config/consul/go.mod | 2 +- contrib/config/consul/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/config/consul/go.mod b/contrib/config/consul/go.mod index 2425c79df..a767999a2 100644 --- a/contrib/config/consul/go.mod +++ b/contrib/config/consul/go.mod @@ -6,7 +6,7 @@ require ( github.com/armon/go-metrics v0.3.10 // indirect github.com/go-kratos/kratos/v2 v2.4.0 github.com/google/btree v1.0.0 // indirect - github.com/hashicorp/consul/api v1.19.1 + github.com/hashicorp/consul/api v1.20.0 github.com/hashicorp/go-hclog v0.14.1 // indirect github.com/hashicorp/go-immutable-radix v1.3.0 // indirect github.com/hashicorp/go-msgpack v0.5.5 // indirect diff --git a/contrib/config/consul/go.sum b/contrib/config/consul/go.sum index 9e5549a2e..1453cc50e 100644 --- a/contrib/config/consul/go.sum +++ b/contrib/config/consul/go.sum @@ -91,8 +91,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/hashicorp/consul/api v1.19.1 h1:GLeK1WD4VIRvt4wRhQKHFudztEkRb8pDs+uRiJgNwes= -github.com/hashicorp/consul/api v1.19.1/go.mod h1:jAt316eYgWGNLJtxkMQrcqRpuDE/kFJdqkEFwRXFv8U= +github.com/hashicorp/consul/api v1.20.0 h1:9IHTjNVSZ7MIwjlW3N3a7iGiykCMDpxZu8jsxFJh0yc= +github.com/hashicorp/consul/api v1.20.0/go.mod h1:nR64eD44KQ59Of/ECwt2vUmIK2DKsDzAwTmwmLl8Wpo= github.com/hashicorp/consul/sdk v0.13.1 h1:EygWVWWMczTzXGpO93awkHFzfUka6hLYJ0qhETd+6lY= github.com/hashicorp/consul/sdk v0.13.1/go.mod h1:SW/mM4LbKfqmMvcFu8v+eiQQ7oitXEFeiBe9StxERb0= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= From 3958f9d5c06ec7a9d2b1a4c85d95688825b76ad4 Mon Sep 17 00:00:00 2001 From: ibrahim albarghouthi Date: Mon, 10 Apr 2023 09:35:38 +0300 Subject: [PATCH 4/5] refactor: metadata supports key corresponding to multiple values (#2772) * Adding Add/Values fn for transport and metadata layers, fixed tests and included tests to new functionaltieis, updated metadata middleware to append instead of hard set of key-values * Remove useless function * Linit fix --------- Co-authored-by: Ibrahim Barghouthi --- metadata/metadata.go | 34 +++++-- metadata/metadata_test.go | 134 ++++++++++++++++++++------- middleware/auth/jwt/jwt_test.go | 7 ++ middleware/metadata/metadata.go | 22 +++-- middleware/metadata/metadata_test.go | 11 ++- middleware/selector/selector_test.go | 26 ++++-- middleware/tracing/tracing_test.go | 10 ++ transport/grpc/transport.go | 10 ++ transport/http/transport.go | 10 ++ transport/transport.go | 2 + 10 files changed, 212 insertions(+), 54 deletions(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index 3dabdbaef..8b072146e 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -9,22 +9,37 @@ import ( // Metadata is our way of representing request headers internally. // They're used at the RPC level and translate back and forth // from Transport headers. -type Metadata map[string]string +type Metadata map[string][]string // New creates an MD from a given key-values map. -func New(mds ...map[string]string) Metadata { +func New(mds ...map[string][]string) Metadata { md := Metadata{} for _, m := range mds { - for k, v := range m { - md.Set(k, v) + for k, vList := range m { + for _, v := range vList { + md.Add(k, v) + } } } return md } +// Add adds the key, value pair to the header. +func (m Metadata) Add(key, value string) { + if len(key) == 0 { + return + } + + m[strings.ToLower(key)] = append(m[strings.ToLower(key)], value) +} + // Get returns the value associated with the passed key. func (m Metadata) Get(key string) string { - return m[strings.ToLower(key)] + v := m[strings.ToLower(key)] + if len(v) == 0 { + return "" + } + return v[0] } // Set stores the key-value pair. @@ -32,11 +47,11 @@ func (m Metadata) Set(key string, value string) { if key == "" || value == "" { return } - m[strings.ToLower(key)] = value + m[strings.ToLower(key)] = []string{value} } // Range iterate over element in metadata. -func (m Metadata) Range(f func(k, v string) bool) { +func (m Metadata) Range(f func(k string, v []string) bool) { for k, v := range m { if !f(k, v) { break @@ -44,6 +59,11 @@ func (m Metadata) Range(f func(k, v string) bool) { } } +// Values returns a slice of values associated with the passed key. +func (m Metadata) Values(key string) []string { + return m[strings.ToLower(key)] +} + // Clone returns a deep copy of Metadata func (m Metadata) Clone() Metadata { md := make(Metadata, len(m)) diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 8653bbe55..9aa1c620b 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -8,7 +8,7 @@ import ( func TestNew(t *testing.T) { type args struct { - mds []map[string]string + mds []map[string][]string } tests := []struct { name string @@ -17,13 +17,13 @@ func TestNew(t *testing.T) { }{ { name: "hello", - args: args{[]map[string]string{{"hello": "kratos"}, {"hello2": "go-kratos"}}}, - want: Metadata{"hello": "kratos", "hello2": "go-kratos"}, + args: args{[]map[string][]string{{"hello": {"kratos"}}, {"hello2": {"go-kratos"}}}}, + want: Metadata{"hello": {"kratos"}, "hello2": {"go-kratos"}}, }, { name: "hi", - args: args{[]map[string]string{{"hi": "kratos"}, {"hi2": "go-kratos"}}}, - want: Metadata{"hi": "kratos", "hi2": "go-kratos"}, + args: args{[]map[string][]string{{"hi": {"kratos"}}, {"hi2": {"go-kratos"}}}}, + want: Metadata{"hi": {"kratos"}, "hi2": {"go-kratos"}}, }, } for _, tt := range tests { @@ -47,13 +47,13 @@ func TestMetadata_Get(t *testing.T) { }{ { name: "kratos", - m: Metadata{"kratos": "value", "env": "dev"}, + m: Metadata{"kratos": {"value"}, "env": {"dev"}}, args: args{key: "kratos"}, want: "value", }, { name: "env", - m: Metadata{"kratos": "value", "env": "dev"}, + m: Metadata{"kratos": {"value"}, "env": {"dev"}}, args: args{key: "env"}, want: "dev", }, @@ -67,6 +67,38 @@ func TestMetadata_Get(t *testing.T) { } } +func TestMetadata_Values(t *testing.T) { + type args struct { + key string + } + tests := []struct { + name string + m Metadata + args args + want []string + }{ + { + name: "kratos", + m: Metadata{"kratos": {"value", "value2"}, "env": {"dev"}}, + args: args{key: "kratos"}, + want: []string{"value", "value2"}, + }, + { + name: "env", + m: Metadata{"kratos": {"value", "value2"}, "env": {"dev"}}, + args: args{key: "env"}, + want: []string{"dev"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.m.Values(tt.args.key); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Get() = %v, want %v", got, tt.want) + } + }) + } +} + func TestMetadata_Set(t *testing.T) { type args struct { key string @@ -82,13 +114,13 @@ func TestMetadata_Set(t *testing.T) { name: "kratos", m: Metadata{}, args: args{key: "hello", value: "kratos"}, - want: Metadata{"hello": "kratos"}, + want: Metadata{"hello": {"kratos"}}, }, { name: "env", - m: Metadata{"hello": "kratos"}, + m: Metadata{"hello": {"kratos"}}, args: args{key: "env", value: "pro"}, - want: Metadata{"hello": "kratos", "env": "pro"}, + want: Metadata{"hello": {"kratos"}, "env": {"pro"}}, }, { name: "empty", @@ -107,6 +139,46 @@ func TestMetadata_Set(t *testing.T) { } } +func TestMetadata_Add(t *testing.T) { + type args struct { + key string + value string + } + tests := []struct { + name string + m Metadata + args args + want Metadata + }{ + { + name: "kratos", + m: Metadata{}, + args: args{key: "hello", value: "kratos"}, + want: Metadata{"hello": {"kratos"}}, + }, + { + name: "env", + m: Metadata{"hello": {"kratos"}}, + args: args{key: "hello", value: "again"}, + want: Metadata{"hello": {"kratos", "again"}}, + }, + { + name: "empty", + m: Metadata{}, + args: args{key: "", value: ""}, + want: Metadata{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.m.Add(tt.args.key, tt.args.value) + if !reflect.DeepEqual(tt.m, tt.want) { + t.Errorf("Set() = %v, want %v", tt.m, tt.want) + } + }) + } +} + func TestClientContext(t *testing.T) { type args struct { ctx context.Context @@ -118,11 +190,11 @@ func TestClientContext(t *testing.T) { }{ { name: "kratos", - args: args{context.Background(), Metadata{"hello": "kratos", "kratos": "https://go-kratos.dev"}}, + args: args{context.Background(), Metadata{"hello": {"kratos"}, "kratos": {"https://go-kratos.dev"}}}, }, { name: "hello", - args: args{context.Background(), Metadata{"hello": "kratos", "hello2": "https://go-kratos.dev"}}, + args: args{context.Background(), Metadata{"hello": {"kratos"}, "hello2": {"https://go-kratos.dev"}}}, }, } for _, tt := range tests { @@ -151,11 +223,11 @@ func TestServerContext(t *testing.T) { }{ { name: "kratos", - args: args{context.Background(), Metadata{"hello": "kratos", "kratos": "https://go-kratos.dev"}}, + args: args{context.Background(), Metadata{"hello": {"kratos"}, "kratos": {"https://go-kratos.dev"}}}, }, { name: "hello", - args: args{context.Background(), Metadata{"hello": "kratos", "hello2": "https://go-kratos.dev"}}, + args: args{context.Background(), Metadata{"hello": {"kratos"}, "hello2": {"https://go-kratos.dev"}}}, }, } for _, tt := range tests { @@ -186,12 +258,12 @@ func TestAppendToClientContext(t *testing.T) { { name: "kratos", args: args{Metadata{}, []string{"hello", "kratos", "env", "dev"}}, - want: Metadata{"hello": "kratos", "env": "dev"}, + want: Metadata{"hello": {"kratos"}, "env": {"dev"}}, }, { name: "hello", - args: args{Metadata{"hi": "https://go-kratos.dev/"}, []string{"hello", "kratos", "env", "dev"}}, - want: Metadata{"hello": "kratos", "env": "dev", "hi": "https://go-kratos.dev/"}, + args: args{Metadata{"hi": {"https://go-kratos.dev/"}}, []string{"hello", "kratos", "env", "dev"}}, + want: Metadata{"hello": {"kratos"}, "env": {"dev"}, "hi": {"https://go-kratos.dev/"}}, }, } for _, tt := range tests { @@ -240,13 +312,13 @@ func TestMergeToClientContext(t *testing.T) { }{ { name: "kratos", - args: args{Metadata{}, Metadata{"hello": "kratos", "env": "dev"}}, - want: Metadata{"hello": "kratos", "env": "dev"}, + args: args{Metadata{}, Metadata{"hello": {"kratos"}, "env": {"dev"}}}, + want: Metadata{"hello": {"kratos"}, "env": {"dev"}}, }, { name: "hello", - args: args{Metadata{"hi": "https://go-kratos.dev/"}, Metadata{"hello": "kratos", "env": "dev"}}, - want: Metadata{"hello": "kratos", "env": "dev", "hi": "https://go-kratos.dev/"}, + args: args{Metadata{"hi": {"https://go-kratos.dev/"}}, Metadata{"hello": {"kratos"}, "env": {"dev"}}}, + want: Metadata{"hello": {"kratos"}, "env": {"dev"}, "hi": {"https://go-kratos.dev/"}}, }, } for _, tt := range tests { @@ -265,19 +337,19 @@ func TestMergeToClientContext(t *testing.T) { } func TestMetadata_Range(t *testing.T) { - md := Metadata{"kratos": "kratos", "https://go-kratos.dev/": "https://go-kratos.dev/", "go-kratos": "go-kratos"} + md := Metadata{"kratos": {"kratos"}, "https://go-kratos.dev/": {"https://go-kratos.dev/"}, "go-kratos": {"go-kratos"}} tmp := Metadata{} - md.Range(func(k, v string) bool { + md.Range(func(k string, v []string) bool { if k == "https://go-kratos.dev/" || k == "kratos" { tmp[k] = v } return true }) - if !reflect.DeepEqual(tmp, Metadata{"https://go-kratos.dev/": "https://go-kratos.dev/", "kratos": "kratos"}) { - t.Errorf("metadata = %v, want %v", tmp, Metadata{"https://go-kratos.dev/": "https://go-kratos.dev/", "kratos": "kratos"}) + if !reflect.DeepEqual(tmp, Metadata{"https://go-kratos.dev/": {"https://go-kratos.dev/"}, "kratos": {"kratos"}}) { + t.Errorf("metadata = %v, want %v", tmp, Metadata{"https://go-kratos.dev/": {"https://go-kratos.dev/"}, "kratos": {"kratos"}}) } tmp = Metadata{} - md.Range(func(k, v string) bool { + md.Range(func(k string, v []string) bool { return false }) if !reflect.DeepEqual(tmp, Metadata{}) { @@ -293,13 +365,13 @@ func TestMetadata_Clone(t *testing.T) { }{ { name: "kratos", - m: Metadata{"kratos": "kratos", "https://go-kratos.dev/": "https://go-kratos.dev/", "go-kratos": "go-kratos"}, - want: Metadata{"kratos": "kratos", "https://go-kratos.dev/": "https://go-kratos.dev/", "go-kratos": "go-kratos"}, + m: Metadata{"kratos": {"kratos"}, "https://go-kratos.dev/": {"https://go-kratos.dev/"}, "go-kratos": {"go-kratos"}}, + want: Metadata{"kratos": {"kratos"}, "https://go-kratos.dev/": {"https://go-kratos.dev/"}, "go-kratos": {"go-kratos"}}, }, { name: "go", - m: Metadata{"language": "golang"}, - want: Metadata{"language": "golang"}, + m: Metadata{"language": {"golang"}}, + want: Metadata{"language": {"golang"}}, }, } for _, tt := range tests { @@ -308,7 +380,7 @@ func TestMetadata_Clone(t *testing.T) { if !reflect.DeepEqual(got, tt.want) { t.Errorf("Clone() = %v, want %v", got, tt.want) } - got["kratos"] = "go" + got["kratos"] = []string{"go"} if reflect.DeepEqual(got, tt.want) { t.Errorf("want got != want got %v want %v", got, tt.want) } diff --git a/middleware/auth/jwt/jwt_test.go b/middleware/auth/jwt/jwt_test.go index 711757bcf..dc2cc9e02 100644 --- a/middleware/auth/jwt/jwt_test.go +++ b/middleware/auth/jwt/jwt_test.go @@ -24,6 +24,8 @@ func (hc headerCarrier) Get(key string) string { return http.Header(hc).Get(key) func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) } +func (hc headerCarrier) Add(key string, value string) { http.Header(hc).Add(key, value) } + // Keys lists the keys stored in this carrier. func (hc headerCarrier) Keys() []string { keys := make([]string, 0, len(hc)) @@ -33,6 +35,11 @@ func (hc headerCarrier) Keys() []string { return keys } +// Values returns a slice value associated with the passed key. +func (hc headerCarrier) Values(key string) []string { + return http.Header(hc).Values(key) +} + func newTokenHeader(headerKey string, token string) *headerCarrier { header := &headerCarrier{} header.Set(headerKey, token) diff --git a/middleware/metadata/metadata.go b/middleware/metadata/metadata.go index da9e43a96..30cb8df90 100644 --- a/middleware/metadata/metadata.go +++ b/middleware/metadata/metadata.go @@ -60,7 +60,9 @@ func Server(opts ...Option) middleware.Middleware { header := tr.RequestHeader() for _, k := range header.Keys() { if options.hasPrefix(k) { - md.Set(k, header.Get(k)) + for _, v := range header.Values(k) { + md.Add(k, v) + } } } ctx = metadata.NewServerContext(ctx, md) @@ -86,19 +88,25 @@ func Client(opts ...Option) middleware.Middleware { header := tr.RequestHeader() // x-md-local- - for k, v := range options.md { - header.Set(k, v) + for k, vList := range options.md { + for _, v := range vList { + header.Add(k, v) + } } if md, ok := metadata.FromClientContext(ctx); ok { - for k, v := range md { - header.Set(k, v) + for k, vList := range md { + for _, v := range vList { + header.Add(k, v) + } } } // x-md-global- if md, ok := metadata.FromServerContext(ctx); ok { - for k, v := range md { + for k, vList := range md { if options.hasPrefix(k) { - header.Set(k, v) + for _, v := range vList { + header.Add(k, v) + } } } } diff --git a/middleware/metadata/metadata_test.go b/middleware/metadata/metadata_test.go index b083f9c10..9e9436d75 100644 --- a/middleware/metadata/metadata_test.go +++ b/middleware/metadata/metadata_test.go @@ -17,6 +17,8 @@ func (hc headerCarrier) Get(key string) string { return http.Header(hc).Get(key) func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) } +func (hc headerCarrier) Add(key string, value string) { http.Header(hc).Add(key, value) } + // Keys lists the keys stored in this carrier. func (hc headerCarrier) Keys() []string { keys := make([]string, 0, len(hc)) @@ -26,6 +28,11 @@ func (hc headerCarrier) Keys() []string { return keys } +// Values returns a slice value associated with the passed key. +func (hc headerCarrier) Values(key string) []string { + return http.Header(hc).Values(key) +} + type testTransport struct{ header headerCarrier } func (tr *testTransport) Kind() transport.Kind { return transport.KindHTTP } @@ -123,11 +130,11 @@ func TestClient(t *testing.T) { func TestWithConstants(t *testing.T) { md := metadata.Metadata{ - constKey: constValue, + constKey: {constValue}, } options := &options{ md: metadata.Metadata{ - "override": "override", + "override": {"override"}, }, } diff --git a/middleware/selector/selector_test.go b/middleware/selector/selector_test.go index f12943e43..0835ef844 100644 --- a/middleware/selector/selector_test.go +++ b/middleware/selector/selector_test.go @@ -40,15 +40,23 @@ func (tr *Transport) ReplyHeader() transport.Header { } type mockHeader struct { - m map[string]string + m map[string][]string } func (m *mockHeader) Get(key string) string { - return m.m[key] + vals := m.m[key] + if len(vals) > 0 { + return vals[0] + } + return "" } func (m *mockHeader) Set(key, value string) { - m.m[key] = value + m.m[key] = []string{value} +} + +func (m *mockHeader) Add(key, value string) { + m.m[key] = append(m.m[key], value) } func (m *mockHeader) Keys() []string { @@ -59,6 +67,10 @@ func (m *mockHeader) Keys() []string { return keys } +func (m *mockHeader) Values(key string) []string { + return m.m[key] +} + func TestMatch(t *testing.T) { tests := []struct { name string @@ -185,28 +197,28 @@ func TestHeaderFunc(t *testing.T) { name: "/hello.Update/world", ctx: transport.NewServerContext(context.Background(), &Transport{ operation: "/hello.Update/world", - headers: &mockHeader{map[string]string{"X-Test": "test"}}, + headers: &mockHeader{map[string][]string{"X-Test": {"test"}}}, }), }, { name: "/hi.Create/world", ctx: transport.NewServerContext(context.Background(), &Transport{ operation: "/hi.Create/world", - headers: &mockHeader{map[string]string{"X-Test": "test2", "go-kratos": "kratos"}}, + headers: &mockHeader{map[string][]string{"X-Test": {"test2"}, "go-kratos": {"kratos"}}}, }), }, { name: "/test.Name/1234", ctx: transport.NewServerContext(context.Background(), &Transport{ operation: "/test.Name/1234", - headers: &mockHeader{map[string]string{"X-Test": "test3"}}, + headers: &mockHeader{map[string][]string{"X-Test": {"test3"}}}, }), }, { name: "/go-kratos.dev/kratos", ctx: transport.NewServerContext(context.Background(), &Transport{ operation: "/go-kratos.dev/kratos", - headers: &mockHeader{map[string]string{"X-Test": "test"}}, + headers: &mockHeader{map[string][]string{"X-Test": {"test"}}}, }), }, } diff --git a/middleware/tracing/tracing_test.go b/middleware/tracing/tracing_test.go index b95771ab9..72740bafa 100644 --- a/middleware/tracing/tracing_test.go +++ b/middleware/tracing/tracing_test.go @@ -29,6 +29,11 @@ func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) } +// Add value to the key-value pair. +func (hc headerCarrier) Add(key string, value string) { + http.Header(hc).Add(key, value) +} + // Keys lists the keys stored in this carrier. func (hc headerCarrier) Keys() []string { keys := make([]string, 0, len(hc)) @@ -38,6 +43,11 @@ func (hc headerCarrier) Keys() []string { return keys } +// Values returns a slice value associated with the passed key. +func (hc headerCarrier) Values(key string) []string { + return http.Header(hc).Values(key) +} + type mockTransport struct { kind transport.Kind endpoint string diff --git a/transport/grpc/transport.go b/transport/grpc/transport.go index 50ca5c91d..56e21a863 100644 --- a/transport/grpc/transport.go +++ b/transport/grpc/transport.go @@ -64,6 +64,11 @@ func (mc headerCarrier) Set(key string, value string) { metadata.MD(mc).Set(key, value) } +// Add append value to key-values pair. +func (mc headerCarrier) Add(key string, value string) { + metadata.MD(mc).Append(key, value) +} + // Keys lists the keys stored in this carrier. func (mc headerCarrier) Keys() []string { keys := make([]string, 0, len(mc)) @@ -72,3 +77,8 @@ func (mc headerCarrier) Keys() []string { } return keys } + +// Values returns a slice of values associated with the passed key. +func (mc headerCarrier) Values(key string) []string { + return metadata.MD(mc).Get(key) +} diff --git a/transport/http/transport.go b/transport/http/transport.go index 32f054edd..686baed76 100644 --- a/transport/http/transport.go +++ b/transport/http/transport.go @@ -92,6 +92,11 @@ func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) } +// Add append value to key-values pair. +func (hc headerCarrier) Add(key string, value string) { + http.Header(hc).Add(key, value) +} + // Keys lists the keys stored in this carrier. func (hc headerCarrier) Keys() []string { keys := make([]string, 0, len(hc)) @@ -100,3 +105,8 @@ func (hc headerCarrier) Keys() []string { } return keys } + +// Values returns a slice of values associated with the passed key. +func (hc headerCarrier) Values(key string) []string { + return http.Header(hc).Values(key) +} diff --git a/transport/transport.go b/transport/transport.go index 42510df30..c1a5396f1 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -27,7 +27,9 @@ type Endpointer interface { type Header interface { Get(key string) string Set(key string, value string) + Add(key string, value string) Keys() []string + Values(key string) []string } // Transporter is transport context value interface. From 520b321fe99bb2d79572539e91cbac448e3c9149 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 24 Apr 2023 23:48:14 +0800 Subject: [PATCH 5/5] build(deps): bump golang.org/x/net in /contrib/opensergo (#2779) Bumps [golang.org/x/net](https://github.com/golang/net) from 0.8.0 to 0.9.0. - [Release notes](https://github.com/golang/net/releases) - [Commits](https://github.com/golang/net/compare/v0.8.0...v0.9.0) --- updated-dependencies: - dependency-name: golang.org/x/net dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- contrib/opensergo/go.mod | 6 +++--- contrib/opensergo/go.sum | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/contrib/opensergo/go.mod b/contrib/opensergo/go.mod index 9e54df772..27aca4f04 100644 --- a/contrib/opensergo/go.mod +++ b/contrib/opensergo/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/go-kratos/kratos/v2 v2.6.1 github.com/opensergo/opensergo-go v0.0.0-20220331070310-e5b01fee4d1c - golang.org/x/net v0.8.0 + golang.org/x/net v0.9.0 google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd google.golang.org/grpc v1.50.1 google.golang.org/protobuf v1.28.1 @@ -16,8 +16,8 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/uuid v1.3.0 // indirect golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect - golang.org/x/sys v0.6.0 // indirect - golang.org/x/text v0.8.0 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/text v0.9.0 // indirect gopkg.in/yaml.v3 v3.0.0 // indirect ) diff --git a/contrib/opensergo/go.sum b/contrib/opensergo/go.sum index 24fbc62b6..e05c56d05 100644 --- a/contrib/opensergo/go.sum +++ b/contrib/opensergo/go.sum @@ -108,8 +108,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -138,19 +138,19 @@ golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= -golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=