properties.go

 1package smithy
 2
 3// PropertiesReader provides an interface for reading metadata from the
 4// underlying metadata container.
 5type PropertiesReader interface {
 6	Get(key interface{}) interface{}
 7}
 8
 9// Properties provides storing and reading metadata values. Keys may be any
10// comparable value type. Get and Set will panic if a key is not comparable.
11//
12// The zero value for a Properties instance is ready for reads/writes without
13// any additional initialization.
14type Properties struct {
15	values map[interface{}]interface{}
16}
17
18// Get attempts to retrieve the value the key points to. Returns nil if the
19// key was not found.
20//
21// Panics if key type is not comparable.
22func (m *Properties) Get(key interface{}) interface{} {
23	m.lazyInit()
24	return m.values[key]
25}
26
27// Set stores the value pointed to by the key. If a value already exists at
28// that key it will be replaced with the new value.
29//
30// Panics if the key type is not comparable.
31func (m *Properties) Set(key, value interface{}) {
32	m.lazyInit()
33	m.values[key] = value
34}
35
36// Has returns whether the key exists in the metadata.
37//
38// Panics if the key type is not comparable.
39func (m *Properties) Has(key interface{}) bool {
40	m.lazyInit()
41	_, ok := m.values[key]
42	return ok
43}
44
45// SetAll accepts all of the given Properties into the receiver, overwriting
46// any existing keys in the case of conflicts.
47func (m *Properties) SetAll(other *Properties) {
48	if other.values == nil {
49		return
50	}
51
52	m.lazyInit()
53	for k, v := range other.values {
54		m.values[k] = v
55	}
56}
57
58func (m *Properties) lazyInit() {
59	if m.values == nil {
60		m.values = map[interface{}]interface{}{}
61	}
62}