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