Skip to content

Commit 74c59b5

Browse files
committed
decode stringOrList using DecodeMapstructure
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 49eefaf commit 74c59b5

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

loader/loader.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,11 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
608608
transforms := map[reflect.Type]func(interface{}) (interface{}, error){
609609
reflect.TypeOf(types.External{}): transformExternal,
610610
reflect.TypeOf(types.HealthCheckTest{}): transformHealthCheckTest,
611-
reflect.TypeOf(types.StringList{}): transformStringList,
612-
reflect.TypeOf(types.Options{}): transformMapStringString,
611+
reflect.TypeOf(types.Options{}): transformMapStringString,
613612
reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
614613
reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
615614
reflect.TypeOf(types.ServiceSecretConfig{}): transformFileReferenceConfig,
616615
reflect.TypeOf(types.ServiceConfigObjConfig{}): transformFileReferenceConfig,
617-
reflect.TypeOf(types.StringOrNumberList{}): transformStringOrNumberList,
618616
reflect.TypeOf(map[string]*types.ServiceNetworkConfig{}): transformServiceNetworkMap,
619617
reflect.TypeOf(types.Mapping{}): transformMappingOrListFunc("=", false),
620618
reflect.TypeOf(types.MappingWithEquals{}): transformMappingOrListFunc("=", true),
@@ -1221,26 +1219,6 @@ func ParseShortSSHSyntax(value string) ([]types.SSHKey, error) {
12211219
return result, nil
12221220
}
12231221

1224-
var transformStringOrNumberList TransformerFunc = func(value interface{}) (interface{}, error) {
1225-
list := value.([]interface{})
1226-
result := make([]string, len(list))
1227-
for i, item := range list {
1228-
result[i] = fmt.Sprint(item)
1229-
}
1230-
return result, nil
1231-
}
1232-
1233-
var transformStringList TransformerFunc = func(data interface{}) (interface{}, error) {
1234-
switch value := data.(type) {
1235-
case string:
1236-
return []string{value}, nil
1237-
case []interface{}:
1238-
return value, nil
1239-
default:
1240-
return data, errors.Errorf("invalid type %T for string list", value)
1241-
}
1242-
}
1243-
12441222
func transformMappingOrListFunc(sep string, allowNil bool) TransformerFunc {
12451223
return func(data interface{}) (interface{}, error) {
12461224
return transformMappingOrList(data, sep, allowNil)

types/stringOrList.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/pkg/errors"
23+
)
24+
25+
// StringList is a type for fields that can be a string or list of strings
26+
type StringList []string
27+
28+
func (l *StringList) DecodeMapstructure(value interface{}) error {
29+
switch v := value.(type) {
30+
case string:
31+
*l = []string{v}
32+
case []interface{}:
33+
list := make([]string, len(v))
34+
for i, e := range v {
35+
list[i] = e.(string)
36+
}
37+
*l = list
38+
default:
39+
return errors.Errorf("invalid type %T for string list", value)
40+
}
41+
return nil
42+
}
43+
44+
// StringOrNumberList is a type for fields that can be a list of strings or numbers
45+
type StringOrNumberList []string
46+
47+
func (l *StringOrNumberList) DecodeMapstructure(value interface{}) error {
48+
switch v := value.(type) {
49+
case string:
50+
*l = []string{v}
51+
case []interface{}:
52+
list := make([]string, len(v))
53+
for i, e := range v {
54+
list[i] = fmt.Sprint(e)
55+
}
56+
*l = list
57+
default:
58+
return errors.Errorf("invalid type %T for string list", value)
59+
}
60+
return nil
61+
}

types/types.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,6 @@ type ThrottleDevice struct {
327327
Extensions Extensions `yaml:"#extensions,inline" json:"-"`
328328
}
329329

330-
// StringList is a type for fields that can be a string or list of strings
331-
type StringList []string
332-
333-
// StringOrNumberList is a type for fields that can be a list of strings or
334-
// numbers
335-
type StringOrNumberList []string
336-
337330
// MappingWithEquals is a mapping type that can be converted from a list of
338331
// key[=value] strings.
339332
// For the key with an empty value (`key=`), the mapped value is set to a pointer to `""`.

0 commit comments

Comments
 (0)