Skip to content

Commit 08c529e

Browse files
committed
Fix type casting for duration and size interpolated variables
Signed-off-by: Laura Brehm <[email protected]>
1 parent 0621f17 commit 08c529e

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
### IDEs ###
22
.idea/*
3+
.vscode/*

loader/interpolate.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222

2323
interp "github.com/compose-spec/compose-go/interpolation"
24-
"github.com/compose-spec/compose-go/types"
2524
"github.com/pkg/errors"
2625
)
2726

@@ -94,19 +93,11 @@ func toInt64(value string) (interface{}, error) {
9493
}
9594

9695
func toUnitBytes(value string) (interface{}, error) {
97-
i, err := strconv.ParseInt(value, 10, 64)
98-
if err != nil {
99-
return nil, err
100-
}
101-
return types.UnitBytes(i), nil
96+
return transformSize(value)
10297
}
10398

10499
func toDuration(value string) (interface{}, error) {
105-
i, err := strconv.ParseInt(value, 10, 64)
106-
if err != nil {
107-
return nil, err
108-
}
109-
return types.Duration(i), nil
100+
return transformStringToDuration(value)
110101
}
111102

112103
func toFloat(value string) (interface{}, error) {

loader/loader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,8 @@ var transformStringToDuration TransformerFunc = func(value interface{}) (interfa
10451045
return value, err
10461046
}
10471047
return types.Duration(d), nil
1048+
case types.Duration:
1049+
return value, nil
10481050
default:
10491051
return value, errors.Errorf("invalid type %T for duration", value)
10501052
}

loader/loader_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ services:
601601
soft: $theint
602602
privileged: $thebool
603603
read_only: $thebool
604-
shm_size: 2gb
604+
shm_size: ${thesize}
605+
stop_grace_period: ${theduration}
605606
stdin_open: ${thebool}
606607
tty: $thebool
607608
volumes:
@@ -628,9 +629,11 @@ networks:
628629
back:
629630
`
630631
env := map[string]string{
631-
"theint": "555",
632-
"thefloat": "3.14",
633-
"thebool": "true",
632+
"theint": "555",
633+
"thefloat": "3.14",
634+
"thebool": "true",
635+
"theduration": "60s",
636+
"thesize": "2gb",
634637
}
635638

636639
config, err := Load(buildConfigDetails(dict, env), func(options *Options) {
@@ -641,9 +644,12 @@ networks:
641644

642645
workingDir, err := os.Getwd()
643646
assert.NilError(t, err)
647+
duration, err := time.ParseDuration("60s")
648+
assert.NilError(t, err)
649+
typesDuration := types.Duration(duration)
644650
expected := &types.Project{
645651
Name: "",
646-
Environment: map[string]string{"thebool": "true", "thefloat": "3.14", "theint": "555"},
652+
Environment: env,
647653
WorkingDir: workingDir,
648654
Services: []types.ServiceConfig{
649655
{
@@ -690,12 +696,13 @@ networks:
690696
"nproc": {Single: 555},
691697
"nofile": {Hard: 555, Soft: 555},
692698
},
693-
Privileged: true,
694-
ReadOnly: true,
695-
Scale: 1,
696-
ShmSize: types.UnitBytes(2 * 1024 * 1024 * 1024),
697-
StdinOpen: true,
698-
Tty: true,
699+
Privileged: true,
700+
ReadOnly: true,
701+
Scale: 1,
702+
ShmSize: types.UnitBytes(2 * 1024 * 1024 * 1024),
703+
StopGracePeriod: &typesDuration,
704+
StdinOpen: true,
705+
Tty: true,
699706
Volumes: []types.ServiceVolumeConfig{
700707
{
701708
Source: "data",

0 commit comments

Comments
 (0)