Use a map instad of a switch/case for Compose transform.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-06-01 15:22:09 -04:00
parent 729d07a371
commit 97ebc19438
1 changed files with 34 additions and 41 deletions

View File

@ -196,7 +196,7 @@ func transform(source map[string]interface{}, target interface{}) error {
data := mapstructure.Metadata{} data := mapstructure.Metadata{}
config := &mapstructure.DecoderConfig{ config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc( DecodeHook: mapstructure.ComposeDecodeHookFunc(
transformHook, createTransformHook(),
mapstructure.StringToTimeDurationHookFunc()), mapstructure.StringToTimeDurationHookFunc()),
Result: target, Result: target,
Metadata: &data, Metadata: &data,
@ -208,46 +208,33 @@ func transform(source map[string]interface{}, target interface{}) error {
return decoder.Decode(source) return decoder.Decode(source)
} }
func transformHook( func createTransformHook() mapstructure.DecodeHookFuncType {
source reflect.Type, transforms := map[reflect.Type]func(interface{}) (interface{}, error){
target reflect.Type, reflect.TypeOf(types.External{}): transformExternal,
data interface{}, reflect.TypeOf(types.HealthCheckTest{}): transformHealthCheckTest,
) (interface{}, error) { reflect.TypeOf(types.ShellCommand{}): transformShellCommand,
switch target { reflect.TypeOf(types.StringList{}): transformStringList,
case reflect.TypeOf(types.External{}): reflect.TypeOf(map[string]string{}): transformMapStringString,
return transformExternal(data) reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
case reflect.TypeOf(types.HealthCheckTest{}): reflect.TypeOf(types.UnitBytes(0)): transformSize,
return transformHealthCheckTest(data) reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
case reflect.TypeOf(types.ShellCommand{}): reflect.TypeOf(types.ServiceSecretConfig{}): transformStringSourceMap,
return transformShellCommand(data) reflect.TypeOf(types.ServiceConfigObjConfig{}): transformStringSourceMap,
case reflect.TypeOf(types.StringList{}): reflect.TypeOf(types.StringOrNumberList{}): transformStringOrNumberList,
return transformStringList(data) reflect.TypeOf(map[string]*types.ServiceNetworkConfig{}): transformServiceNetworkMap,
case reflect.TypeOf(map[string]string{}): reflect.TypeOf(types.MappingWithEquals{}): transformMappingOrListFunc("=", true),
return transformMapStringString(data) reflect.TypeOf(types.Labels{}): transformMappingOrListFunc("=", false),
case reflect.TypeOf(types.UlimitsConfig{}): reflect.TypeOf(types.MappingWithColon{}): transformMappingOrListFunc(":", false),
return transformUlimits(data) reflect.TypeOf(types.ServiceVolumeConfig{}): transformServiceVolumeConfig,
case reflect.TypeOf(types.UnitBytes(0)): }
return transformSize(data)
case reflect.TypeOf([]types.ServicePortConfig{}): return func(_ reflect.Type, target reflect.Type, data interface{}) (interface{}, error) {
return transformServicePort(data) transform, ok := transforms[target]
case reflect.TypeOf(types.ServiceSecretConfig{}): if !ok {
return transformStringSourceMap(data) return data, nil
case reflect.TypeOf(types.ServiceConfigObjConfig{}): }
return transformStringSourceMap(data) return transform(data)
case reflect.TypeOf(types.StringOrNumberList{}):
return transformStringOrNumberList(data)
case reflect.TypeOf(map[string]*types.ServiceNetworkConfig{}):
return transformServiceNetworkMap(data)
case reflect.TypeOf(types.MappingWithEquals{}):
return transformMappingOrList(data, "=", true), nil
case reflect.TypeOf(types.Labels{}):
return transformMappingOrList(data, "=", false), nil
case reflect.TypeOf(types.MappingWithColon{}):
return transformMappingOrList(data, ":", false), nil
case reflect.TypeOf(types.ServiceVolumeConfig{}):
return transformServiceVolumeConfig(data)
} }
return data, nil
} }
// keys needs to be converted to strings for jsonschema // keys needs to be converted to strings for jsonschema
@ -618,6 +605,12 @@ func transformStringList(data interface{}) (interface{}, error) {
} }
} }
func transformMappingOrListFunc(sep string, allowNil bool) func(interface{}) (interface{}, error) {
return func(data interface{}) (interface{}, error) {
return transformMappingOrList(data, sep, allowNil), nil
}
}
func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) interface{} { func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) interface{} {
switch value := mappingOrList.(type) { switch value := mappingOrList.(type) {
case map[string]interface{}: case map[string]interface{}:
@ -659,7 +652,7 @@ func transformHealthCheckTest(data interface{}) (interface{}, error) {
} }
} }
func transformSize(value interface{}) (int64, error) { func transformSize(value interface{}) (interface{}, error) {
switch value := value.(type) { switch value := value.(type) {
case int: case int:
return int64(value), nil return int64(value), nil