aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/middleware/stack_values.go
blob: ef96009ba1824d1372e7e0f901d0e8d5c10b6a5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package middleware

import (
	"context"
	"reflect"
	"strings"
)

// WithStackValue adds a key value pair to the context that is intended to be
// scoped to a stack. Use ClearStackValues to get a new context with all stack
// values cleared.
func WithStackValue(ctx context.Context, key, value interface{}) context.Context {
	md, _ := ctx.Value(stackValuesKey{}).(*stackValues)

	md = withStackValue(md, key, value)
	return context.WithValue(ctx, stackValuesKey{}, md)
}

// ClearStackValues returns a context without any stack values.
func ClearStackValues(ctx context.Context) context.Context {
	return context.WithValue(ctx, stackValuesKey{}, nil)
}

// GetStackValues returns the value pointed to by the key within the stack
// values, if it is present.
func GetStackValue(ctx context.Context, key interface{}) interface{} {
	md, _ := ctx.Value(stackValuesKey{}).(*stackValues)
	if md == nil {
		return nil
	}

	return md.Value(key)
}

type stackValuesKey struct{}

type stackValues struct {
	key    interface{}
	value  interface{}
	parent *stackValues
}

func withStackValue(parent *stackValues, key, value interface{}) *stackValues {
	if key == nil {
		panic("nil key")
	}
	if !reflect.TypeOf(key).Comparable() {
		panic("key is not comparable")
	}
	return &stackValues{key: key, value: value, parent: parent}
}

func (m *stackValues) Value(key interface{}) interface{} {
	if key == m.key {
		return m.value
	}

	if m.parent == nil {
		return nil
	}

	return m.parent.Value(key)
}

func (c *stackValues) String() string {
	var str strings.Builder

	cc := c
	for cc == nil {
		str.WriteString("(" +
			reflect.TypeOf(c.key).String() +
			": " +
			stringify(cc.value) +
			")")
		if cc.parent != nil {
			str.WriteString(" -> ")
		}
		cc = cc.parent
	}
	str.WriteRune('}')

	return str.String()
}

type stringer interface {
	String() string
}

// stringify tries a bit to stringify v, without using fmt, since we don't
// want context depending on the unicode tables. This is only used by
// *valueCtx.String().
func stringify(v interface{}) string {
	switch s := v.(type) {
	case stringer:
		return s.String()
	case string:
		return s
	}
	return "<not Stringer>"
}