-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_generator.go
119 lines (94 loc) · 3.73 KB
/
value_generator.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
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
119
package tfsig
import (
"fmt"
"strings"
"github.com/zclconf/go-cty/cty"
"github.com/yoanm/go-tfsig/tokens"
)
// ValueGenerator is able to detect "ident" tokens and convert them into a special cty capsule.
// Capsule will then be converted to `hclwrite.tokens`
// It allows to write values like `var.my_var`, `locals.my_local` or `data.res_name.val_name` without any quotes.
type ValueGenerator struct {
matcher IdentTokenMatcherInterface
}
// NewValueGenerator returns a new ValueGenerator with the default 'ident' tokens matcher augmented with provided list
// of token to consider as 'ident' tokens.
func NewValueGenerator(identPrefixList ...string) ValueGenerator {
return NewValueGeneratorWith(NewIdentTokenMatcher(identPrefixList...))
}
// NewValueGeneratorWith returns a new ValueGenerator with the provided matcher.
func NewValueGeneratorWith(matcher IdentTokenMatcherInterface) ValueGenerator {
return ValueGenerator{matcher: matcher}
}
// ToIdent converts a string to a special `cty.Value` capsule holding `hclwrite.tokens`.
func (g *ValueGenerator) ToIdent(s *string) *cty.Value {
if s == nil {
return nil
}
return tokens.NewIdentValue(*s)
}
// ToIdentList converts a list of string to `cty.Value` list containing capsules holding `hclwrite.tokens`.
func (g *ValueGenerator) ToIdentList(list *[]string) *cty.Value {
if list == nil {
return nil
}
return tokens.NewIdentListValue(*list)
}
// ToString convert a string to `cty.Value` string which will be rendered as quoted string by terraform HCL
// If the provided string is actually an 'ident' token, `cty.Value` will be a capsule holding `hclwrite.tokens`.
func (g *ValueGenerator) ToString(s *string) *cty.Value {
return g.FromString(s, cty.String)
}
// ToBool convert a string to `cty.Value` boolean which will be rendered as true or false value by terraform HCL
// If the provided string is actually an 'ident' token, `cty.Value` will be a capsule holding `hclwrite.tokens`.
func (g *ValueGenerator) ToBool(s *string) *cty.Value {
return g.FromString(s, cty.Bool)
}
// ToNumber convert a string to `cty.Value` number which will be rendered as numeric value by terraform HCL
// If the provided string is actually an 'ident' token, `cty.Value` will be a capsule holding `hclwrite.tokens`.
func (g *ValueGenerator) ToNumber(s *string) *cty.Value {
return g.FromString(s, cty.Number)
}
// ToStringList convert a string list to `cty.Value` string list which will be rendered as quoted string list
// by terraform HCL.
// If a provided string item is actually an 'ident' token, `cty.Value` item will be a capsule holding `hclwrite.tokens`.
func (g *ValueGenerator) ToStringList(list *[]string) *cty.Value {
if list == nil {
return nil
}
listLength := len(*list)
val := cty.EmptyTupleVal
if listLength > 0 {
newList := make([]cty.Value, listLength)
for i := range *list {
// Do not use `i, rawValue := range ...` because of "G601: Implicit memory aliasing in for loop."
rawValue := (*list)[i]
newList[i] = *g.FromString(&rawValue, cty.String)
}
val = cty.TupleVal(newList)
}
return &val
}
// FromString convert a string to `cty.Value` of the provided type
// If the provided string is actually an 'ident' token, `cty.Value` will be a capsule holding `hclwrite.tokens`.
func (g *ValueGenerator) FromString(val *string, toType cty.Type) *cty.Value {
if val == nil {
return nil
}
if g.matcher.IsIdentToken(*val) {
return g.ToIdent(val)
}
switch toType {
case cty.String:
val := cty.StringVal(*val)
return &val
case cty.Bool:
val := cty.BoolVal(*val == "1" || strings.ToLower(*val) == "true")
return &val
case cty.Number:
val := cty.MustParseNumberVal(*val)
return &val
default:
panic(fmt.Sprintf("Unable to convert \"%s\" to a %s", *val, toType.FriendlyName()))
}
}