sliceOfMapsToMapHookFunc merges a slice of maps to a map
()
| 166 | |
| 167 | // sliceOfMapsToMapHookFunc merges a slice of maps to a map |
| 168 | func sliceOfMapsToMapHookFunc() mapstructure.DecodeHookFunc { |
| 169 | return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) { |
| 170 | if from.Kind() == reflect.Slice && from.Elem().Kind() == reflect.Map && (to.Kind() == reflect.Struct || to.Kind() == reflect.Map) { |
| 171 | source, ok := data.([]map[string]interface{}) |
| 172 | if !ok { |
| 173 | return data, nil |
| 174 | } |
| 175 | if len(source) == 0 { |
| 176 | return data, nil |
| 177 | } |
| 178 | if len(source) == 1 { |
| 179 | return source[0], nil |
| 180 | } |
| 181 | // flatten the slice into one map |
| 182 | convert := make(map[string]interface{}) |
| 183 | for _, mapItem := range source { |
| 184 | for key, value := range mapItem { |
| 185 | convert[key] = value |
| 186 | } |
| 187 | } |
| 188 | return convert, nil |
| 189 | } |
| 190 | return data, nil |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func initConfig() { |
| 195 | var defaultConfig = &Config{} |