Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions interpolation/interpolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
)

var defaults = map[string]string{
"USER": "jenny",
"FOO": "bar",
"count": "5",
"USER": "jenny",
"FOO": "bar",
"count": "5",
"REQUIRED": "true",
}

func defaultMapping(name string) (string, bool) {
Expand Down Expand Up @@ -196,19 +197,27 @@ func TestInterpolateWithCast(t *testing.T) {
config := map[string]interface{}{
"foo": map[string]interface{}{
"replicas": "$count",
"required": "$REQUIRED",
},
}
toInt := func(value string) (interface{}, error) {
return strconv.Atoi(value)
}
toBoolean := func(value string) (interface{}, error) {
return strconv.ParseBool(value)
}
result, err := Interpolate(config, Options{
LookupValue: defaultMapping,
TypeCastMapping: map[tree.Path]Cast{tree.NewPath(tree.PathMatchAll, "replicas"): toInt},
LookupValue: defaultMapping,
TypeCastMapping: map[tree.Path]Cast{
tree.NewPath(tree.PathMatchAll, "replicas"): toInt,
tree.NewPath(tree.PathMatchAll, "required"): toBoolean,
},
})
assert.NilError(t, err)
expected := map[string]interface{}{
"foo": map[string]interface{}{
"replicas": 5,
"required": true,
},
}
assert.Check(t, is.DeepEqual(expected, result))
Expand Down
2 changes: 2 additions & 0 deletions loader/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var interpolateTypeCastMapping = map[tree.Path]interp.Cast{
servicePath("cpus"): toFloat32,
servicePath("cpu_shares"): toInt64,
servicePath("init"): toBoolean,
servicePath("depends_on", tree.PathMatchAll, "required"): toBoolean,
servicePath("depends_on", tree.PathMatchAll, "restart"): toBoolean,
servicePath("deploy", "replicas"): toInt,
servicePath("deploy", "update_config", "parallelism"): toInt,
servicePath("deploy", "update_config", "max_failure_ratio"): toFloat,
Expand Down