metadata.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ttrpc
  14. import (
  15. "context"
  16. "strings"
  17. )
  18. // MD is the user type for ttrpc metadata
  19. type MD map[string][]string
  20. // Get returns the metadata for a given key when they exist.
  21. // If there is no metadata, a nil slice and false are returned.
  22. func (m MD) Get(key string) ([]string, bool) {
  23. key = strings.ToLower(key)
  24. list, ok := m[key]
  25. if !ok || len(list) == 0 {
  26. return nil, false
  27. }
  28. return list, true
  29. }
  30. // Set sets the provided values for a given key.
  31. // The values will overwrite any existing values.
  32. // If no values provided, a key will be deleted.
  33. func (m MD) Set(key string, values ...string) {
  34. key = strings.ToLower(key)
  35. if len(values) == 0 {
  36. delete(m, key)
  37. return
  38. }
  39. m[key] = values
  40. }
  41. // Append appends additional values to the given key.
  42. func (m MD) Append(key string, values ...string) {
  43. key = strings.ToLower(key)
  44. if len(values) == 0 {
  45. return
  46. }
  47. current, ok := m[key]
  48. if ok {
  49. m.Set(key, append(current, values...)...)
  50. } else {
  51. m.Set(key, values...)
  52. }
  53. }
  54. func (m MD) setRequest(r *Request) {
  55. for k, values := range m {
  56. for _, v := range values {
  57. r.Metadata = append(r.Metadata, &KeyValue{
  58. Key: k,
  59. Value: v,
  60. })
  61. }
  62. }
  63. }
  64. func (m MD) fromRequest(r *Request) {
  65. for _, kv := range r.Metadata {
  66. m[kv.Key] = append(m[kv.Key], kv.Value)
  67. }
  68. }
  69. type metadataKey struct{}
  70. // GetMetadata retrieves metadata from context.Context (previously attached with WithMetadata)
  71. func GetMetadata(ctx context.Context) (MD, bool) {
  72. metadata, ok := ctx.Value(metadataKey{}).(MD)
  73. return metadata, ok
  74. }
  75. // GetMetadataValue gets a specific metadata value by name from context.Context
  76. func GetMetadataValue(ctx context.Context, name string) (string, bool) {
  77. metadata, ok := GetMetadata(ctx)
  78. if !ok {
  79. return "", false
  80. }
  81. if list, ok := metadata.Get(name); ok {
  82. return list[0], true
  83. }
  84. return "", false
  85. }
  86. // WithMetadata attaches metadata map to a context.Context
  87. func WithMetadata(ctx context.Context, md MD) context.Context {
  88. return context.WithValue(ctx, metadataKey{}, md)
  89. }