Skip to content

Commit 54b9780

Browse files
committed
decode Options using DecodeMapstructure
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 4fc0069 commit 54b9780

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

loader/loader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ type Transformer struct {
607607
func createTransformHook(additionalTransformers ...Transformer) mapstructure.DecodeHookFuncType {
608608
transforms := map[reflect.Type]func(interface{}) (interface{}, error){
609609
reflect.TypeOf(types.External{}): transformExternal,
610-
reflect.TypeOf(types.Options{}): transformMapStringString,
610+
reflect.TypeOf(types.Options{}): transformOptions,
611611
reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
612612
reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
613613
reflect.TypeOf(types.ServiceSecretConfig{}): transformFileReferenceConfig,
@@ -1020,7 +1020,7 @@ func loadFileObjectConfig(name string, objType string, obj types.FileObjectConfi
10201020
return obj, nil
10211021
}
10221022

1023-
var transformMapStringString TransformerFunc = func(data interface{}) (interface{}, error) {
1023+
var transformOptions TransformerFunc = func(data interface{}) (interface{}, error) {
10241024
switch value := data.(type) {
10251025
case map[string]interface{}:
10261026
return toMapStringString(value, false), nil

types/options.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// Options is a mapping type for options we pass as-is to container runtime
26+
type Options map[string]string
27+
28+
func (d *Options) DecodeMapstructure(value interface{}) error {
29+
switch v := value.(type) {
30+
case map[string]interface{}:
31+
m := make(map[string]string)
32+
for key, e := range v {
33+
if e == nil {
34+
m[key] = ""
35+
} else {
36+
m[key] = fmt.Sprint(e)
37+
}
38+
}
39+
*d = m
40+
case map[string]string:
41+
*d = v
42+
default:
43+
return errors.Errorf("invalid type %T for options", value)
44+
}
45+
return nil
46+
}

types/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,6 @@ func (m Mapping) Merge(o Mapping) Mapping {
442442
return m
443443
}
444444

445-
// Options is a mapping type for options we pass as-is to container runtime
446-
type Options map[string]string
447-
448445
type SSHKey struct {
449446
ID string
450447
Path string

0 commit comments

Comments
 (0)