Merge branch 'main' into fix-PathQuery

pull/2774/head
下下 2 years ago committed by GitHub
commit dd77a97664
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      contrib/config/consul/go.mod
  2. 4
      contrib/config/consul/go.sum
  3. 2
      contrib/log/aliyun/go.mod
  4. 4
      contrib/log/aliyun/go.sum
  5. 6
      contrib/opensergo/go.mod
  6. 14
      contrib/opensergo/go.sum
  7. 52
      log/helper.go
  8. 34
      metadata/metadata.go
  9. 134
      metadata/metadata_test.go
  10. 7
      middleware/auth/jwt/jwt_test.go
  11. 22
      middleware/metadata/metadata.go
  12. 11
      middleware/metadata/metadata_test.go
  13. 26
      middleware/selector/selector_test.go
  14. 10
      middleware/tracing/tracing_test.go
  15. 10
      transport/grpc/transport.go
  16. 10
      transport/http/transport.go
  17. 2
      transport/transport.go

@ -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

@ -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=

@ -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 (

@ -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=

@ -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
)

@ -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=

@ -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)
}

@ -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))

@ -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)
}

@ -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)

@ -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)
}
}
}
}

@ -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"},
},
}

@ -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"}}},
}),
},
}

@ -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

@ -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)
}

@ -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)
}

@ -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.

Loading…
Cancel
Save