File: properties.go

package info (click to toggle)
golang-github-aws-smithy-go 1.19.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 2,680 kB
  • sloc: java: 15,917; xml: 166; sh: 131; makefile: 66
file content (62 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (2)
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
package smithy

// PropertiesReader provides an interface for reading metadata from the
// underlying metadata container.
type PropertiesReader interface {
	Get(key interface{}) interface{}
}

// Properties provides storing and reading metadata values. Keys may be any
// comparable value type. Get and Set will panic if a key is not comparable.
//
// The zero value for a Properties instance is ready for reads/writes without
// any additional initialization.
type Properties struct {
	values map[interface{}]interface{}
}

// Get attempts to retrieve the value the key points to. Returns nil if the
// key was not found.
//
// Panics if key type is not comparable.
func (m *Properties) Get(key interface{}) interface{} {
	m.lazyInit()
	return m.values[key]
}

// Set stores the value pointed to by the key. If a value already exists at
// that key it will be replaced with the new value.
//
// Panics if the key type is not comparable.
func (m *Properties) Set(key, value interface{}) {
	m.lazyInit()
	m.values[key] = value
}

// Has returns whether the key exists in the metadata.
//
// Panics if the key type is not comparable.
func (m *Properties) Has(key interface{}) bool {
	m.lazyInit()
	_, ok := m.values[key]
	return ok
}

// SetAll accepts all of the given Properties into the receiver, overwriting
// any existing keys in the case of conflicts.
func (m *Properties) SetAll(other *Properties) {
	if other.values == nil {
		return
	}

	m.lazyInit()
	for k, v := range other.values {
		m.values[k] = v
	}
}

func (m *Properties) lazyInit() {
	if m.values == nil {
		m.values = map[interface{}]interface{}{}
	}
}