MCPcopy Create free account
hub / github.com/devops-rob/target-cli / sliceOfMapsToMapHookFunc

Function sliceOfMapsToMapHookFunc

cmd/root.go:168–192  ·  view source on GitHub ↗

sliceOfMapsToMapHookFunc merges a slice of maps to a map

()

Source from the content-addressed store, hash-verified

166
167// sliceOfMapsToMapHookFunc merges a slice of maps to a map
168func 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
194func initConfig() {
195 var defaultConfig = &Config{}

Callers 1

initConfigFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected