github.com/prebid/prebid-server/v2@v2.18.0/util/maputil/maputil.go (about) 1 package maputil 2 3 // ReadEmbeddedMap reads element k from the map m as a map[string]interface{}. 4 func ReadEmbeddedMap(m map[string]interface{}, k string) (map[string]interface{}, bool) { 5 if v, ok := m[k]; ok { 6 vCasted, ok := v.(map[string]interface{}) 7 return vCasted, ok 8 } 9 10 return nil, false 11 } 12 13 // ReadEmbeddedSlice reads element k from the map m as a []interface{}. 14 func ReadEmbeddedSlice(m map[string]interface{}, k string) ([]interface{}, bool) { 15 if v, ok := m[k]; ok { 16 vCasted, ok := v.([]interface{}) 17 return vCasted, ok 18 } 19 20 return nil, false 21 } 22 23 // ReadEmbeddedString reads element k from the map m as a string. 24 func ReadEmbeddedString(m map[string]interface{}, k string) (string, bool) { 25 if v, ok := m[k]; ok { 26 vCasted, ok := v.(string) 27 return vCasted, ok 28 } 29 return "", false 30 } 31 32 // HasElement returns true if nested element k exists. 33 func HasElement(m map[string]interface{}, k ...string) bool { 34 exists := false 35 kLastIndex := len(k) - 1 36 37 for i, k := range k { 38 isLastKey := i == kLastIndex 39 40 if isLastKey { 41 _, exists = m[k] 42 } else { 43 if m, exists = ReadEmbeddedMap(m, k); !exists { 44 break 45 } 46 } 47 } 48 49 return exists 50 } 51 52 // Clone creates an independent copy of a map, 53 func Clone[K comparable, V any](m map[K]V) map[K]V { 54 if m == nil { 55 return nil 56 } 57 58 clone := make(map[K]V, len(m)) 59 for key, value := range m { 60 clone[key] = value 61 } 62 63 return clone 64 }