github.com/pulumi/terraform@v1.4.0/pkg/addrs/map.go (about) 1 package addrs 2 3 // Map represents a mapping whose keys are address types that implement 4 // UniqueKeyer. 5 // 6 // Since not all address types are comparable in the Go language sense, this 7 // type cannot work with the typical Go map access syntax, and so instead has 8 // a method-based syntax. Use this type only for situations where the key 9 // type isn't guaranteed to always be a valid key for a standard Go map. 10 type Map[K UniqueKeyer, V any] struct { 11 // Elems is the internal data structure of the map. 12 // 13 // This is exported to allow for comparisons during tests and other similar 14 // careful read operations, but callers MUST NOT modify this map directly. 15 // Use only the methods of Map to modify the contents of this structure, 16 // to ensure that it remains correct and consistent. 17 Elems map[UniqueKey]MapElem[K, V] 18 } 19 20 type MapElem[K UniqueKeyer, V any] struct { 21 Key K 22 Value V 23 } 24 25 func MakeMap[K UniqueKeyer, V any](initialElems ...MapElem[K, V]) Map[K, V] { 26 inner := make(map[UniqueKey]MapElem[K, V], len(initialElems)) 27 ret := Map[K, V]{inner} 28 for _, elem := range initialElems { 29 ret.Put(elem.Key, elem.Value) 30 } 31 return ret 32 } 33 34 func MakeMapElem[K UniqueKeyer, V any](key K, value V) MapElem[K, V] { 35 return MapElem[K, V]{key, value} 36 } 37 38 // Put inserts a new element into the map, or replaces an existing element 39 // which has an equivalent key. 40 func (m Map[K, V]) Put(key K, value V) { 41 realKey := key.UniqueKey() 42 m.Elems[realKey] = MapElem[K, V]{key, value} 43 } 44 45 // PutElement is like Put but takes the key and value from the given MapElement 46 // structure instead of as individual arguments. 47 func (m Map[K, V]) PutElement(elem MapElem[K, V]) { 48 m.Put(elem.Key, elem.Value) 49 } 50 51 // Remove deletes the element with the given key from the map, or does nothing 52 // if there is no such element. 53 func (m Map[K, V]) Remove(key K) { 54 realKey := key.UniqueKey() 55 delete(m.Elems, realKey) 56 } 57 58 // Get returns the value of the element with the given key, or the zero value 59 // of V if there is no such element. 60 func (m Map[K, V]) Get(key K) V { 61 realKey := key.UniqueKey() 62 return m.Elems[realKey].Value 63 } 64 65 // GetOk is like Get but additionally returns a flag for whether there was an 66 // element with the given key present in the map. 67 func (m Map[K, V]) GetOk(key K) (V, bool) { 68 realKey := key.UniqueKey() 69 elem, ok := m.Elems[realKey] 70 return elem.Value, ok 71 } 72 73 // Has returns true if and only if there is an element in the map which has the 74 // given key. 75 func (m Map[K, V]) Has(key K) bool { 76 realKey := key.UniqueKey() 77 _, ok := m.Elems[realKey] 78 return ok 79 } 80 81 // Len returns the number of elements in the map. 82 func (m Map[K, V]) Len() int { 83 return len(m.Elems) 84 } 85 86 // Elements returns a slice containing a snapshot of the current elements of 87 // the map, in an unpredictable order. 88 func (m Map[K, V]) Elements() []MapElem[K, V] { 89 if len(m.Elems) == 0 { 90 return nil 91 } 92 ret := make([]MapElem[K, V], 0, len(m.Elems)) 93 for _, elem := range m.Elems { 94 ret = append(ret, elem) 95 } 96 return ret 97 } 98 99 // Keys returns a Set[K] containing a snapshot of the current keys of elements 100 // of the map. 101 func (m Map[K, V]) Keys() Set[K] { 102 if len(m.Elems) == 0 { 103 return nil 104 } 105 ret := make(Set[K], len(m.Elems)) 106 107 // We mess with the internals of Set here, rather than going through its 108 // public interface, because that means we can avoid re-calling UniqueKey 109 // on all of the elements when we know that our own Put method would have 110 // already done the same thing. 111 for realKey, elem := range m.Elems { 112 ret[realKey] = elem.Key 113 } 114 return ret 115 } 116 117 // Values returns a slice containing a snapshot of the current values of 118 // elements of the map, in an unpredictable order. 119 func (m Map[K, V]) Values() []V { 120 if len(m.Elems) == 0 { 121 return nil 122 } 123 ret := make([]V, 0, len(m.Elems)) 124 for _, elem := range m.Elems { 125 ret = append(ret, elem.Value) 126 } 127 return ret 128 }