|
| 1 | +package maps |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/jucardi/infuse/util/reflectx" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + regex = regexp.MustCompile(`.*\[\d*\]$`) |
| 15 | +) |
| 16 | + |
| 17 | +// Contains indicates if the given map contains an entry by the given key |
| 18 | +func Contains(c map[string]interface{}, key string) bool { |
| 19 | + if !strings.Contains(key, ".") { |
| 20 | + if _, ok := c[key]; ok { |
| 21 | + return ok |
| 22 | + } |
| 23 | + |
| 24 | + return false |
| 25 | + } |
| 26 | + |
| 27 | + if _, err := GetValue(c, key); err != nil { |
| 28 | + return false |
| 29 | + } |
| 30 | + |
| 31 | + return true |
| 32 | +} |
| 33 | + |
| 34 | +// GetValue If a map represents a JSON with nested objects. GetValue retrieves the value by the given path. Eg. 'info.database.port' |
| 35 | +func GetValue(data map[string]interface{}, key string) (interface{}, error) { |
| 36 | + split := strings.Split(key, ".") |
| 37 | + v := reflect.ValueOf(data) |
| 38 | + for i, s := range split { |
| 39 | + isArray := regex.MatchString(s) |
| 40 | + index := 0 |
| 41 | + |
| 42 | + if isArray { |
| 43 | + split := strings.Split(s, "[") |
| 44 | + s = split[0] |
| 45 | + index, _ = strconv.Atoi(split[1][:len(split[1])-1]) |
| 46 | + } |
| 47 | + |
| 48 | + current := v.MapIndex(reflect.ValueOf(s)) |
| 49 | + |
| 50 | + if !current.IsValid() { |
| 51 | + return nil, fmt.Errorf("unable to get value by the key '%s'. The value for '%s' is not present", key, s) |
| 52 | + } |
| 53 | + |
| 54 | + if reflectx.IsNil(current) { |
| 55 | + if i < len(split)-1 { |
| 56 | + return nil, fmt.Errorf("unable to get value by the key '%s'. The value for '%s' is null", key, s) |
| 57 | + } else { |
| 58 | + return nil, nil |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if isArray { |
| 63 | + for current = current.Elem(); current.IsValid() && current.Kind() != reflect.Slice && current.Kind() != reflect.Array; { |
| 64 | + } |
| 65 | + if current.Len() <= index { |
| 66 | + return nil, fmt.Errorf("failed to retrieve value at key '%s'. Index out of range for field '%s' (index: %d | length: %d)", key, s, index, current.Len()) |
| 67 | + } |
| 68 | + current = current.Index(index) |
| 69 | + } |
| 70 | + |
| 71 | + if i < len(split)-1 { |
| 72 | + if v.Kind() != reflect.Map { |
| 73 | + return nil, fmt.Errorf("unable to get value by path: '%s' | The piece '%s' does not represent an object", key, s) |
| 74 | + } |
| 75 | + |
| 76 | + // m, err := ConvertMap(current.Interface()) |
| 77 | + m, err := reflectx.GetNonPointerValue(current) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + v = m |
| 83 | + } else { |
| 84 | + v = current |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if !v.IsValid() { |
| 89 | + return nil, fmt.Errorf("value by the key '%s' is not present", key) |
| 90 | + } |
| 91 | + |
| 92 | + if reflectx.IsNil(v) { |
| 93 | + return nil, nil |
| 94 | + } |
| 95 | + |
| 96 | + return v.Interface(), nil |
| 97 | +} |
| 98 | + |
| 99 | +// GetOrDefault gets the value by the given key, if the value is not present, or an error occurs while retrieving the value, returns what was specified as `defaultVal` |
| 100 | +func GetOrDefault(data map[string]interface{}, key string, defaultVal interface{}) interface{} { |
| 101 | + if v, _ := GetValue(data, key); v == nil { |
| 102 | + return defaultVal |
| 103 | + } else { |
| 104 | + return v |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// SetValue if a map represents a JSON with nested objects. SetValue assigns a value to the given path. Eg. 'info.database.port' |
| 109 | +// 'makeEmpty' indicates that if a piece of the path is missing (Eg. 'info.database' is nil) an empty object should be created to continue the assignment. |
| 110 | +func SetValue(data map[string]interface{}, key string, value interface{}, makeEmpty bool) error { |
| 111 | + split := strings.Split(key, ".") |
| 112 | + v := reflect.ValueOf(data) |
| 113 | + for i, s := range split { |
| 114 | + if i == len(split)-1 { |
| 115 | + v.SetMapIndex(reflect.ValueOf(s), reflect.ValueOf(value)) |
| 116 | + } else { |
| 117 | + if v.Kind() != reflect.Map { |
| 118 | + return fmt.Errorf("unable to get value by path: '%s' | The piece '%s' does not represent an object", key, s) |
| 119 | + } |
| 120 | + |
| 121 | + val := v.MapIndex(reflect.ValueOf(s)) |
| 122 | + |
| 123 | + if !val.IsValid() { |
| 124 | + if makeEmpty { |
| 125 | + val = reflect.ValueOf(make(map[string]interface{})) |
| 126 | + v.SetMapIndex(reflect.ValueOf(s), val) |
| 127 | + } else { |
| 128 | + return fmt.Errorf("unable to get value by path: '%s' | The piece '%s' does not represent an object", key, s) |
| 129 | + } |
| 130 | + } |
| 131 | + v = reflect.ValueOf(val.Interface().(map[string]interface{})) |
| 132 | + } |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func ConvertMap(val interface{}) (map[string]interface{}, error) { |
| 138 | + if m, ok := val.(map[string]interface{}); ok { |
| 139 | + for k, v := range m { |
| 140 | + if mapValue, ok := v.(map[interface{}]interface{}); ok { |
| 141 | + if newVal, err := ConvertMap(mapValue); err != nil { |
| 142 | + return nil, fmt.Errorf("failed to convert '%s', %s ", k, err.Error()) |
| 143 | + } else { |
| 144 | + m[k] = newVal |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return m, nil |
| 149 | + } |
| 150 | + |
| 151 | + if m, ok := val.(map[interface{}]interface{}); ok { |
| 152 | + ret := map[string]interface{}{} |
| 153 | + for k, v := range m { |
| 154 | + key, ok := k.(string) |
| 155 | + if !ok { |
| 156 | + return nil, fmt.Errorf("all keys must be strings when mapping to a struct, detected key: '%+v'", k) |
| 157 | + } |
| 158 | + if mapValue, ok := v.(map[interface{}]interface{}); ok { |
| 159 | + if newVal, err := ConvertMap(mapValue); err != nil { |
| 160 | + return nil, fmt.Errorf("failed to convert '%s', %s ", key, err.Error()) |
| 161 | + } else { |
| 162 | + ret[key] = newVal |
| 163 | + } |
| 164 | + } else { |
| 165 | + ret[key] = v |
| 166 | + } |
| 167 | + } |
| 168 | + return ret, nil |
| 169 | + } |
| 170 | + |
| 171 | + return nil, fmt.Errorf("unexpected object type: %+v", val) |
| 172 | +} |
| 173 | + |
| 174 | +func StringMapEqual(m1 map[string]string, m2 map[string]string) bool { |
| 175 | + if len(m1) != len(m2) { |
| 176 | + return false |
| 177 | + } |
| 178 | + for k1, v1 := range m1 { |
| 179 | + if v2, ok := m2[k1]; ok { |
| 180 | + if v1 != v2 { |
| 181 | + return false |
| 182 | + } |
| 183 | + } else { |
| 184 | + return false |
| 185 | + } |
| 186 | + } |
| 187 | + return true |
| 188 | +} |
0 commit comments