File: interpolate.go

package info (click to toggle)
golang-github-compose-spec-compose-go 2.4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,628 kB
  • sloc: makefile: 36; sh: 8
file content (118 lines) | stat: -rw-r--r-- 5,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
   Copyright 2020 The Compose Specification Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package loader

import (
	"fmt"
	"strconv"
	"strings"

	interp "github.com/compose-spec/compose-go/v2/interpolation"
	"github.com/compose-spec/compose-go/v2/tree"
	"github.com/sirupsen/logrus"
)

var interpolateTypeCastMapping = map[tree.Path]interp.Cast{
	servicePath("configs", tree.PathMatchList, "mode"):             toInt,
	servicePath("cpu_count"):                                       toInt64,
	servicePath("cpu_percent"):                                     toFloat,
	servicePath("cpu_period"):                                      toInt64,
	servicePath("cpu_quota"):                                       toInt64,
	servicePath("cpu_rt_period"):                                   toInt64,
	servicePath("cpu_rt_runtime"):                                  toInt64,
	servicePath("cpus"):                                            toFloat32,
	servicePath("cpu_shares"):                                      toInt64,
	servicePath("init"):                                            toBoolean,
	servicePath("deploy", "replicas"):                              toInt,
	servicePath("deploy", "update_config", "parallelism"):          toInt,
	servicePath("deploy", "update_config", "max_failure_ratio"):    toFloat,
	servicePath("deploy", "rollback_config", "parallelism"):        toInt,
	servicePath("deploy", "rollback_config", "max_failure_ratio"):  toFloat,
	servicePath("deploy", "restart_policy", "max_attempts"):        toInt,
	servicePath("deploy", "placement", "max_replicas_per_node"):    toInt,
	servicePath("healthcheck", "retries"):                          toInt,
	servicePath("healthcheck", "disable"):                          toBoolean,
	servicePath("oom_kill_disable"):                                toBoolean,
	servicePath("oom_score_adj"):                                   toInt64,
	servicePath("pids_limit"):                                      toInt64,
	servicePath("ports", tree.PathMatchList, "target"):             toInt,
	servicePath("privileged"):                                      toBoolean,
	servicePath("read_only"):                                       toBoolean,
	servicePath("scale"):                                           toInt,
	servicePath("secrets", tree.PathMatchList, "mode"):             toInt,
	servicePath("stdin_open"):                                      toBoolean,
	servicePath("tty"):                                             toBoolean,
	servicePath("ulimits", tree.PathMatchAll):                      toInt,
	servicePath("ulimits", tree.PathMatchAll, "hard"):              toInt,
	servicePath("ulimits", tree.PathMatchAll, "soft"):              toInt,
	servicePath("volumes", tree.PathMatchList, "read_only"):        toBoolean,
	servicePath("volumes", tree.PathMatchList, "volume", "nocopy"): toBoolean,
	iPath("networks", tree.PathMatchAll, "external"):               toBoolean,
	iPath("networks", tree.PathMatchAll, "internal"):               toBoolean,
	iPath("networks", tree.PathMatchAll, "attachable"):             toBoolean,
	iPath("networks", tree.PathMatchAll, "enable_ipv4"):            toBoolean,
	iPath("networks", tree.PathMatchAll, "enable_ipv6"):            toBoolean,
	iPath("volumes", tree.PathMatchAll, "external"):                toBoolean,
	iPath("secrets", tree.PathMatchAll, "external"):                toBoolean,
	iPath("configs", tree.PathMatchAll, "external"):                toBoolean,
}

func iPath(parts ...string) tree.Path {
	return tree.NewPath(parts...)
}

func servicePath(parts ...string) tree.Path {
	return iPath(append([]string{"services", tree.PathMatchAll}, parts...)...)
}

func toInt(value string) (interface{}, error) {
	return strconv.Atoi(value)
}

func toInt64(value string) (interface{}, error) {
	return strconv.ParseInt(value, 10, 64)
}

func toFloat(value string) (interface{}, error) {
	return strconv.ParseFloat(value, 64)
}

func toFloat32(value string) (interface{}, error) {
	f, err := strconv.ParseFloat(value, 32)
	if err != nil {
		return nil, err
	}
	return float32(f), nil
}

// should match http://yaml.org/type/bool.html
func toBoolean(value string) (interface{}, error) {
	switch strings.ToLower(value) {
	case "true":
		return true, nil
	case "false":
		return false, nil
	case "y", "yes", "on":
		logrus.Warnf("%q for boolean is not supported by YAML 1.2, please use `true`", value)
		return true, nil
	case "n", "no", "off":
		logrus.Warnf("%q for boolean is not supported by YAML 1.2, please use `false`", value)
		return false, nil
	default:
		return nil, fmt.Errorf("invalid boolean: %s", value)
	}
}