/
config.go
80 lines (69 loc) · 1.59 KB
/
config.go
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
package convert
import (
"fmt"
)
// Config stores the configuration associated with a YAML to starlark conversion.
type Config struct {
Entrypoint string
Args []Arg
varSubs map[string]string
}
// Arg includes the details of an argument for the starlark entrypoint.
type Arg struct {
Name string
DefaultValue interface{}
Required bool
}
// SubVariable determines which variable (if any) a raw string should
// be replaced with.
func (c Config) SubVariable(value string) string {
if c.varSubs == nil {
c.varSubs = map[string]string{}
for _, arg := range c.Args {
switch v := arg.DefaultValue.(type) {
case string:
if v != "" {
c.varSubs[v] = arg.Name
}
default:
}
}
}
return c.varSubs[value]
}
// DefaultValueStr gets the default value for this argument.
func (a Arg) DefaultValueStr() string {
switch v := a.DefaultValue.(type) {
case string:
return fmt.Sprintf(`"%s"`, v)
case int, int8, int16, int32, int64:
return fmt.Sprintf("%d", v)
case float32, float64:
return fmt.Sprintf("%f", v)
case bool:
if v {
return "True"
} else {
return "False"
}
default:
return "None"
}
}
// RequiredStatement returns a statement that ensures that this argument
// is set.
func (a Arg) RequiredStatement() string {
var emptyValue string
switch a.DefaultValue.(type) {
case string:
emptyValue = `""`
case int, int8, int16, int32, int64:
emptyValue = "0"
case float32, float64:
emptyValue = "0.0"
default:
emptyValue = "None"
}
return fmt.Sprintf(`if %s == %s:
fail("%s must be set to non-empty value")`, a.Name, emptyValue, a.Name)
}