stack_values.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package middleware
  2. import (
  3. "context"
  4. "reflect"
  5. "strings"
  6. )
  7. // WithStackValue adds a key value pair to the context that is intended to be
  8. // scoped to a stack. Use ClearStackValues to get a new context with all stack
  9. // values cleared.
  10. func WithStackValue(ctx context.Context, key, value interface{}) context.Context {
  11. md, _ := ctx.Value(stackValuesKey{}).(*stackValues)
  12. md = withStackValue(md, key, value)
  13. return context.WithValue(ctx, stackValuesKey{}, md)
  14. }
  15. // ClearStackValues returns a context without any stack values.
  16. func ClearStackValues(ctx context.Context) context.Context {
  17. return context.WithValue(ctx, stackValuesKey{}, nil)
  18. }
  19. // GetStackValues returns the value pointed to by the key within the stack
  20. // values, if it is present.
  21. func GetStackValue(ctx context.Context, key interface{}) interface{} {
  22. md, _ := ctx.Value(stackValuesKey{}).(*stackValues)
  23. if md == nil {
  24. return nil
  25. }
  26. return md.Value(key)
  27. }
  28. type stackValuesKey struct{}
  29. type stackValues struct {
  30. key interface{}
  31. value interface{}
  32. parent *stackValues
  33. }
  34. func withStackValue(parent *stackValues, key, value interface{}) *stackValues {
  35. if key == nil {
  36. panic("nil key")
  37. }
  38. if !reflect.TypeOf(key).Comparable() {
  39. panic("key is not comparable")
  40. }
  41. return &stackValues{key: key, value: value, parent: parent}
  42. }
  43. func (m *stackValues) Value(key interface{}) interface{} {
  44. if key == m.key {
  45. return m.value
  46. }
  47. if m.parent == nil {
  48. return nil
  49. }
  50. return m.parent.Value(key)
  51. }
  52. func (c *stackValues) String() string {
  53. var str strings.Builder
  54. cc := c
  55. for cc == nil {
  56. str.WriteString("(" +
  57. reflect.TypeOf(c.key).String() +
  58. ": " +
  59. stringify(cc.value) +
  60. ")")
  61. if cc.parent != nil {
  62. str.WriteString(" -> ")
  63. }
  64. cc = cc.parent
  65. }
  66. str.WriteRune('}')
  67. return str.String()
  68. }
  69. type stringer interface {
  70. String() string
  71. }
  72. // stringify tries a bit to stringify v, without using fmt, since we don't
  73. // want context depending on the unicode tables. This is only used by
  74. // *valueCtx.String().
  75. func stringify(v interface{}) string {
  76. switch s := v.(type) {
  77. case stringer:
  78. return s.String()
  79. case string:
  80. return s
  81. }
  82. return "<not Stringer>"
  83. }