src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

properties.go (1621B)


      1 package smithy
      2 
      3 // PropertiesReader provides an interface for reading metadata from the
      4 // underlying metadata container.
      5 type 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.
     14 type 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.
     22 func (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.
     31 func (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.
     39 func (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.
     47 func (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 
     58 func (m *Properties) lazyInit() {
     59 	if m.values == nil {
     60 		m.values = map[interface{}]interface{}{}
     61 	}
     62 }