Skip to content

Commit

Permalink
compose/loader: define type for transformer-functions
Browse files Browse the repository at this point in the history
Also explicitly type transformer-functions

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Oct 31, 2019
1 parent 6eb0c9c commit 9118b2b
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,13 @@ func Transform(source interface{}, target interface{}, additionalTransformers ..
return decoder.Decode(source)
}

// TransformerFunc defines a function to perform the actual transformation
type TransformerFunc func(interface{}) (interface{}, error)

// Transformer defines a map to type transformer
type Transformer struct {
TypeOf reflect.Type
Func func(interface{}) (interface{}, error)
Func TransformerFunc
}

func createTransformHook(additionalTransformers ...Transformer) mapstructure.DecodeHookFuncType {
Expand Down Expand Up @@ -684,7 +687,7 @@ func absPath(workingDir string, filePath string) string {
return filepath.Join(workingDir, filePath)
}

func transformMapStringString(data interface{}) (interface{}, error) {
var transformMapStringString TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case map[string]interface{}:
return toMapStringString(value, false), nil
Expand All @@ -695,7 +698,7 @@ func transformMapStringString(data interface{}) (interface{}, error) {
}
}

func transformExternal(data interface{}) (interface{}, error) {
var transformExternal TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case bool:
return map[string]interface{}{"external": value}, nil
Expand All @@ -706,7 +709,7 @@ func transformExternal(data interface{}) (interface{}, error) {
}
}

func transformServicePort(data interface{}) (interface{}, error) {
var transformServicePort TransformerFunc = func(data interface{}) (interface{}, error) {
switch entries := data.(type) {
case []interface{}:
// We process the list instead of individual items here.
Expand Down Expand Up @@ -739,7 +742,7 @@ func transformServicePort(data interface{}) (interface{}, error) {
}
}

func transformStringSourceMap(data interface{}) (interface{}, error) {
var transformStringSourceMap TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case string:
return map[string]interface{}{"source": value}, nil
Expand All @@ -750,7 +753,7 @@ func transformStringSourceMap(data interface{}) (interface{}, error) {
}
}

func transformBuildConfig(data interface{}) (interface{}, error) {
var transformBuildConfig TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case string:
return map[string]interface{}{"context": value}, nil
Expand All @@ -761,7 +764,7 @@ func transformBuildConfig(data interface{}) (interface{}, error) {
}
}

func transformServiceVolumeConfig(data interface{}) (interface{}, error) {
var transformServiceVolumeConfig TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case string:
return ParseVolume(value)
Expand All @@ -772,8 +775,7 @@ func transformServiceVolumeConfig(data interface{}) (interface{}, error) {
}
}

// nolint: unparam
func transformServiceNetworkMap(value interface{}) (interface{}, error) {
var transformServiceNetworkMap TransformerFunc = func(value interface{}) (interface{}, error) {
if list, ok := value.([]interface{}); ok {
mapValue := map[interface{}]interface{}{}
for _, name := range list {
Expand All @@ -784,8 +786,7 @@ func transformServiceNetworkMap(value interface{}) (interface{}, error) {
return value, nil
}

// nolint: unparam
func transformStringOrNumberList(value interface{}) (interface{}, error) {
var transformStringOrNumberList TransformerFunc = func(value interface{}) (interface{}, error) {
list := value.([]interface{})
result := make([]string, len(list))
for i, item := range list {
Expand All @@ -794,7 +795,7 @@ func transformStringOrNumberList(value interface{}) (interface{}, error) {
return result, nil
}

func transformStringList(data interface{}) (interface{}, error) {
var transformStringList TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case string:
return []string{value}, nil
Expand All @@ -805,13 +806,13 @@ func transformStringList(data interface{}) (interface{}, error) {
}
}

func transformMappingOrListFunc(sep string, allowNil bool) func(interface{}) (interface{}, error) {
func transformMappingOrListFunc(sep string, allowNil bool) TransformerFunc {
return func(data interface{}) (interface{}, error) {
return transformMappingOrList(data, sep, allowNil), nil
}
}

func transformListOrMappingFunc(sep string, allowNil bool) func(interface{}) (interface{}, error) {
func transformListOrMappingFunc(sep string, allowNil bool) TransformerFunc {
return func(data interface{}) (interface{}, error) {
return transformListOrMapping(data, sep, allowNil), nil
}
Expand Down Expand Up @@ -850,14 +851,14 @@ func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool
panic(errors.Errorf("expected a map or a list, got %T: %#v", mappingOrList, mappingOrList))
}

func transformShellCommand(value interface{}) (interface{}, error) {
var transformShellCommand TransformerFunc = func(value interface{}) (interface{}, error) {
if str, ok := value.(string); ok {
return shellwords.Parse(str)
}
return value, nil
}

func transformHealthCheckTest(data interface{}) (interface{}, error) {
var transformHealthCheckTest TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case string:
return append([]string{"CMD-SHELL"}, value), nil
Expand All @@ -868,7 +869,7 @@ func transformHealthCheckTest(data interface{}) (interface{}, error) {
}
}

func transformSize(value interface{}) (interface{}, error) {
var transformSize TransformerFunc = func(value interface{}) (interface{}, error) {
switch value := value.(type) {
case int:
return int64(value), nil
Expand All @@ -878,7 +879,7 @@ func transformSize(value interface{}) (interface{}, error) {
panic(errors.Errorf("invalid type for size %T", value))
}

func transformStringToDuration(value interface{}) (interface{}, error) {
var transformStringToDuration TransformerFunc = func(value interface{}) (interface{}, error) {
switch value := value.(type) {
case string:
d, err := time.ParseDuration(value)
Expand Down

0 comments on commit 9118b2b

Please sign in to comment.