aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty-yaml/cty_funcs.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/zclconf/go-cty-yaml/cty_funcs.go')
-rw-r--r--vendor/github.com/zclconf/go-cty-yaml/cty_funcs.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty-yaml/cty_funcs.go b/vendor/github.com/zclconf/go-cty-yaml/cty_funcs.go
new file mode 100644
index 0000000..b91141c
--- /dev/null
+++ b/vendor/github.com/zclconf/go-cty-yaml/cty_funcs.go
@@ -0,0 +1,57 @@
1package yaml
2
3import (
4 "github.com/zclconf/go-cty/cty"
5 "github.com/zclconf/go-cty/cty/function"
6)
7
8// YAMLDecodeFunc is a cty function for decoding arbitrary YAML source code
9// into a cty Value, using the ImpliedType and Unmarshal methods of the
10// Standard pre-defined converter.
11var YAMLDecodeFunc = function.New(&function.Spec{
12 Params: []function.Parameter{
13 {
14 Name: "src",
15 Type: cty.String,
16 },
17 },
18 Type: func(args []cty.Value) (cty.Type, error) {
19 if !args[0].IsKnown() {
20 return cty.DynamicPseudoType, nil
21 }
22 if args[0].IsNull() {
23 return cty.NilType, function.NewArgErrorf(0, "YAML source code cannot be null")
24 }
25 return Standard.ImpliedType([]byte(args[0].AsString()))
26 },
27 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
28 if retType == cty.DynamicPseudoType {
29 return cty.DynamicVal, nil
30 }
31 return Standard.Unmarshal([]byte(args[0].AsString()), retType)
32 },
33})
34
35// YAMLEncodeFunc is a cty function for encoding an arbitrary cty value
36// into YAML.
37var YAMLEncodeFunc = function.New(&function.Spec{
38 Params: []function.Parameter{
39 {
40 Name: "value",
41 Type: cty.DynamicPseudoType,
42 AllowNull: true,
43 AllowDynamicType: true,
44 },
45 },
46 Type: function.StaticReturnType(cty.String),
47 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
48 if !args[0].IsWhollyKnown() {
49 return cty.UnknownVal(retType), nil
50 }
51 raw, err := Standard.Marshal(args[0])
52 if err != nil {
53 return cty.NilVal, err
54 }
55 return cty.StringVal(string(raw)), nil
56 },
57})