-
Notifications
You must be signed in to change notification settings - Fork 523
/
yaml.go
50 lines (45 loc) 路 1.47 KB
/
yaml.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
package funcs
import (
"strings"
yaml "github.com/zclconf/go-cty-yaml"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
// YAMLDecodeFunc is an Infracost specific version of the yaml.YAMLDecodeFunc
// which handles Infracost mocked return values. If the argument passed to YAMLDecodeFunc
// is an Infracost mock (e.g. a string with value mock-value) then we return a mocked object
// that can be used in the HCL evaluation loop. This means we get less unwanted nil values when
// evaluating HCL files. This is especially important when evaluating Terragrunt HCL files
// as unexpected nils cause program termination.
var YAMLDecodeFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "src",
Type: cty.String,
},
},
Type: func(args []cty.Value) (cty.Type, error) {
if !args[0].IsKnown() {
return cty.DynamicPseudoType, nil
}
if args[0].IsNull() {
return cty.NilType, function.NewArgErrorf(0, "YAML source code cannot be null")
}
val := args[0].AsString()
if strings.HasSuffix(val, "-mock") {
return cty.Object(map[string]cty.Type{
"foo": cty.String,
}), nil
}
return yaml.Standard.ImpliedType([]byte(val))
},
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
val := args[0].AsString()
if strings.HasSuffix(val, "-mock") {
return cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
}), nil
}
return yaml.Standard.Unmarshal([]byte(val), retType)
},
})