1616
1717package ttrpc
1818
19- import "context"
19+ import (
20+ "context"
21+ "strings"
22+ )
2023
21- // Metadata represents the key-value pairs (similar to http.Header) to be passed to ttrpc server from a client.
22- type Metadata map [string ]StringList
24+ // MD is the user type for ttrpc metadata
25+ type MD map [string ][] string
2326
2427// Get returns the metadata for a given key when they exist.
2528// If there is no metadata, a nil slice and false are returned.
26- func (m Metadata ) Get (key string ) ([]string , bool ) {
29+ func (m MD ) Get (key string ) ([]string , bool ) {
30+ key = strings .ToLower (key )
2731 list , ok := m [key ]
28- if ! ok || len (list . List ) == 0 {
32+ if ! ok || len (list ) == 0 {
2933 return nil , false
3034 }
3135
32- return list . List , true
36+ return list , true
3337}
3438
3539// Set sets the provided values for a given key.
3640// The values will overwrite any existing values.
3741// If no values provided, a key will be deleted.
38- func (m Metadata ) Set (key string , values ... string ) {
42+ func (m MD ) Set (key string , values ... string ) {
43+ key = strings .ToLower (key )
3944 if len (values ) == 0 {
4045 delete (m , key )
4146 return
4247 }
43-
44- m [key ] = StringList {List : values }
48+ m [key ] = values
4549}
4650
4751// Append appends additional values to the given key.
48- func (m Metadata ) Append (key string , values ... string ) {
52+ func (m MD ) Append (key string , values ... string ) {
53+ key = strings .ToLower (key )
4954 if len (values ) == 0 {
5055 return
5156 }
52-
53- list , ok := m [key ]
57+ current , ok := m [key ]
5458 if ok {
55- m .Set (key , append (list . List , values ... )... )
59+ m .Set (key , append (current , values ... )... )
5660 } else {
5761 m .Set (key , values ... )
5862 }
5963}
6064
65+ func (m MD ) setRequest (r * Request ) {
66+ for k , values := range m {
67+ for _ , v := range values {
68+ r .Metadata = append (r .Metadata , & KeyValue {
69+ Key : k ,
70+ Value : v ,
71+ })
72+ }
73+ }
74+ }
75+
76+ func (m MD ) fromRequest (r * Request ) {
77+ for _ , kv := range r .Metadata {
78+ m [kv .Key ] = append (m [kv .Key ], kv .Value )
79+ }
80+ }
81+
6182type metadataKey struct {}
6283
6384// GetMetadata retrieves metadata from context.Context (previously attached with WithMetadata)
64- func GetMetadata (ctx context.Context ) (Metadata , bool ) {
65- metadata , ok := ctx .Value (metadataKey {}).(Metadata )
85+ func GetMetadata (ctx context.Context ) (MD , bool ) {
86+ metadata , ok := ctx .Value (metadataKey {}).(MD )
6687 return metadata , ok
6788}
6889
@@ -81,6 +102,6 @@ func GetMetadataValue(ctx context.Context, name string) (string, bool) {
81102}
82103
83104// WithMetadata attaches metadata map to a context.Context
84- func WithMetadata (ctx context.Context , headers Metadata ) context.Context {
85- return context .WithValue (ctx , metadataKey {}, headers )
105+ func WithMetadata (ctx context.Context , md MD ) context.Context {
106+ return context .WithValue (ctx , metadataKey {}, md )
86107}
0 commit comments