Skip to content

Commit e32f0b4

Browse files
authored
Merge pull request #172 from ndeloof/interpolate_yaml_nodes
2 parents 8ad38fd + ba39ede commit e32f0b4

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

loader/interpolate.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,50 @@ import (
2121
"strings"
2222

2323
interp "github.com/compose-spec/compose-go/interpolation"
24+
"github.com/compose-spec/compose-go/types"
2425
"github.com/pkg/errors"
2526
)
2627

2728
var interpolateTypeCastMapping = map[interp.Path]interp.Cast{
2829
servicePath("configs", interp.PathMatchList, "mode"): toInt,
29-
servicePath("secrets", interp.PathMatchList, "mode"): toInt,
30-
servicePath("healthcheck", "retries"): toInt,
31-
servicePath("healthcheck", "disable"): toBoolean,
30+
servicePath("cpu_count"): toInt64,
31+
servicePath("cpu_percent"): toFloat,
32+
servicePath("cpu_period"): toInt64,
33+
servicePath("cpu_quota"): toInt64,
34+
servicePath("cpu_rt_period"): toInt64,
35+
servicePath("cpu_rt_runtime"): toInt64,
36+
servicePath("cpus"): toFloat32,
37+
servicePath("cpu_shares"): toInt64,
38+
servicePath("init"): toBoolean,
3239
servicePath("deploy", "replicas"): toInt,
3340
servicePath("deploy", "update_config", "parallelism"): toInt,
3441
servicePath("deploy", "update_config", "max_failure_ratio"): toFloat,
3542
servicePath("deploy", "rollback_config", "parallelism"): toInt,
3643
servicePath("deploy", "rollback_config", "max_failure_ratio"): toFloat,
3744
servicePath("deploy", "restart_policy", "max_attempts"): toInt,
3845
servicePath("deploy", "placement", "max_replicas_per_node"): toInt,
46+
servicePath("healthcheck", "retries"): toInt,
47+
servicePath("healthcheck", "disable"): toBoolean,
48+
servicePath("mem_limit"): toUnitBytes,
49+
servicePath("mem_reservation"): toUnitBytes,
50+
servicePath("memswap_limit"): toUnitBytes,
51+
servicePath("mem_swappiness"): toUnitBytes,
52+
servicePath("oom_kill_disable"): toBoolean,
53+
servicePath("oom_score_adj"): toInt64,
54+
servicePath("pids_limit"): toInt64,
3955
servicePath("ports", interp.PathMatchList, "target"): toInt,
4056
servicePath("ports", interp.PathMatchList, "published"): toInt,
41-
servicePath("ulimits", interp.PathMatchAll): toInt,
42-
servicePath("ulimits", interp.PathMatchAll, "hard"): toInt,
43-
servicePath("ulimits", interp.PathMatchAll, "soft"): toInt,
4457
servicePath("privileged"): toBoolean,
4558
servicePath("read_only"): toBoolean,
59+
servicePath("scale"): toInt,
60+
servicePath("secrets", interp.PathMatchList, "mode"): toInt,
61+
servicePath("shm_size"): toUnitBytes,
4662
servicePath("stdin_open"): toBoolean,
63+
servicePath("stop_grace_period"): toDuration,
4764
servicePath("tty"): toBoolean,
65+
servicePath("ulimits", interp.PathMatchAll): toInt,
66+
servicePath("ulimits", interp.PathMatchAll, "hard"): toInt,
67+
servicePath("ulimits", interp.PathMatchAll, "soft"): toInt,
4868
servicePath("volumes", interp.PathMatchList, "read_only"): toBoolean,
4969
servicePath("volumes", interp.PathMatchList, "volume", "nocopy"): toBoolean,
5070
iPath("networks", interp.PathMatchAll, "external"): toBoolean,
@@ -67,10 +87,38 @@ func toInt(value string) (interface{}, error) {
6787
return strconv.Atoi(value)
6888
}
6989

90+
func toInt64(value string) (interface{}, error) {
91+
return strconv.ParseInt(value, 10, 64)
92+
}
93+
94+
func toUnitBytes(value string) (interface{}, error) {
95+
i, err := strconv.ParseInt(value, 10, 64)
96+
if err != nil {
97+
return nil, err
98+
}
99+
return types.UnitBytes(i), nil
100+
}
101+
102+
func toDuration(value string) (interface{}, error) {
103+
i, err := strconv.ParseInt(value, 10, 64)
104+
if err != nil {
105+
return nil, err
106+
}
107+
return types.Duration(i), nil
108+
}
109+
70110
func toFloat(value string) (interface{}, error) {
71111
return strconv.ParseFloat(value, 64)
72112
}
73113

114+
func toFloat32(value string) (interface{}, error) {
115+
f, err := strconv.ParseFloat(value, 32)
116+
if err != nil {
117+
return nil, err
118+
}
119+
return float32(f), nil
120+
}
121+
74122
// should match http://yaml.org/type/bool.html
75123
func toBoolean(value string) (interface{}, error) {
76124
switch strings.ToLower(value) {

loader/loader.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"path"
2424
"path/filepath"
2525
"reflect"
26-
"regexp"
2726
"sort"
2827
"strings"
2928
"time"
@@ -221,21 +220,14 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
221220
}
222221

223222
func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
223+
yaml, err := ParseYAML(b)
224+
if err != nil {
225+
return nil, err
226+
}
224227
if !opts.SkipInterpolation {
225-
withoutFullLineComments := removeYamlComments(string(b))
226-
substituted, err := opts.Interpolate.Substitute(withoutFullLineComments, template.Mapping(opts.Interpolate.LookupValue))
227-
if err != nil {
228-
return nil, err
229-
}
230-
b = []byte(substituted)
228+
return interp.Interpolate(yaml, *opts.Interpolate)
231229
}
232-
233-
return ParseYAML(b)
234-
}
235-
236-
// removeYamlComments drop all full line comments from the yaml file, so we don't try to apply string substitutions on most of the irrelevant places
237-
func removeYamlComments(content string) string {
238-
return regexp.MustCompile(`(?m)^[ \t]*#.*\n*`).ReplaceAllString(content, "")
230+
return yaml, err
239231
}
240232

241233
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {

0 commit comments

Comments
 (0)