Mainly redefine or reused what Terraform did. * allow to used `variables`, `variable` and `local` blocks * import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package yaml
|
|
|
|
import (
|
|
"github.com/zclconf/go-cty/cty"
|
|
"github.com/zclconf/go-cty/cty/function"
|
|
)
|
|
|
|
// YAMLDecodeFunc is a cty function for decoding arbitrary YAML source code
|
|
// into a cty Value, using the ImpliedType and Unmarshal methods of the
|
|
// Standard pre-defined converter.
|
|
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")
|
|
}
|
|
return Standard.ImpliedType([]byte(args[0].AsString()))
|
|
},
|
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
|
if retType == cty.DynamicPseudoType {
|
|
return cty.DynamicVal, nil
|
|
}
|
|
return Standard.Unmarshal([]byte(args[0].AsString()), retType)
|
|
},
|
|
})
|
|
|
|
// YAMLEncodeFunc is a cty function for encoding an arbitrary cty value
|
|
// into YAML.
|
|
var YAMLEncodeFunc = function.New(&function.Spec{
|
|
Params: []function.Parameter{
|
|
{
|
|
Name: "value",
|
|
Type: cty.DynamicPseudoType,
|
|
AllowNull: true,
|
|
AllowDynamicType: true,
|
|
},
|
|
},
|
|
Type: function.StaticReturnType(cty.String),
|
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
|
if !args[0].IsWhollyKnown() {
|
|
return cty.UnknownVal(retType), nil
|
|
}
|
|
raw, err := Standard.Marshal(args[0])
|
|
if err != nil {
|
|
return cty.NilVal, err
|
|
}
|
|
return cty.StringVal(string(raw)), nil
|
|
},
|
|
})
|