2018-04-04 03:11:28 +02:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/function"
|
|
|
|
"github.com/zclconf/go-cty/cty/function/stdlib"
|
2018-05-22 02:39:26 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/lang/funcs"
|
2018-04-04 03:11:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var impureFunctions = []string{
|
|
|
|
"timestamp",
|
|
|
|
"uuid",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions returns the set of functions that should be used to when evaluating
|
|
|
|
// expressions in the receiving scope.
|
|
|
|
func (s *Scope) Functions() map[string]function.Function {
|
|
|
|
s.funcsLock.Lock()
|
|
|
|
if s.funcs == nil {
|
2018-05-22 02:39:26 +02:00
|
|
|
// Some of our functions are just directly the cty stdlib functions.
|
|
|
|
// Others are implemented in the subdirectory "funcs" here in this
|
|
|
|
// repository. New functions should generally start out their lives
|
|
|
|
// in the "funcs" directory and potentially graduate to cty stdlib
|
|
|
|
// later if the functionality seems to be something domain-agnostic
|
|
|
|
// that would be useful to all applications using cty functions.
|
|
|
|
|
2018-04-04 03:11:28 +02:00
|
|
|
s.funcs = map[string]function.Function{
|
|
|
|
"abs": stdlib.AbsoluteFunc,
|
2018-05-23 00:04:49 +02:00
|
|
|
"basename": funcs.BasenameFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"base64decode": unimplFunc, // TODO
|
|
|
|
"base64encode": unimplFunc, // TODO
|
|
|
|
"base64gzip": unimplFunc, // TODO
|
|
|
|
"base64sha256": unimplFunc, // TODO
|
|
|
|
"base64sha512": unimplFunc, // TODO
|
|
|
|
"bcrypt": unimplFunc, // TODO
|
|
|
|
"ceil": unimplFunc, // TODO
|
|
|
|
"chomp": unimplFunc, // TODO
|
|
|
|
"cidrhost": unimplFunc, // TODO
|
|
|
|
"cidrnetmask": unimplFunc, // TODO
|
|
|
|
"cidrsubnet": unimplFunc, // TODO
|
|
|
|
"coalesce": stdlib.CoalesceFunc,
|
|
|
|
"coalescelist": unimplFunc, // TODO
|
|
|
|
"compact": unimplFunc, // TODO
|
|
|
|
"concat": stdlib.ConcatFunc,
|
|
|
|
"contains": unimplFunc, // TODO
|
|
|
|
"csvdecode": stdlib.CSVDecodeFunc,
|
2018-05-23 00:24:50 +02:00
|
|
|
"dirname": funcs.DirnameFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"distinct": unimplFunc, // TODO
|
2018-05-22 02:39:26 +02:00
|
|
|
"element": funcs.ElementFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"chunklist": unimplFunc, // TODO
|
2018-05-22 02:39:26 +02:00
|
|
|
"file": funcs.MakeFileFunc(s.BaseDir, false),
|
|
|
|
"filebase64": funcs.MakeFileFunc(s.BaseDir, true),
|
2018-04-04 03:11:28 +02:00
|
|
|
"matchkeys": unimplFunc, // TODO
|
|
|
|
"flatten": unimplFunc, // TODO
|
|
|
|
"floor": unimplFunc, // TODO
|
|
|
|
"format": stdlib.FormatFunc,
|
|
|
|
"formatlist": stdlib.FormatListFunc,
|
|
|
|
"indent": unimplFunc, // TODO
|
|
|
|
"index": unimplFunc, // TODO
|
2018-05-22 02:39:26 +02:00
|
|
|
"join": funcs.JoinFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"jsondecode": stdlib.JSONDecodeFunc,
|
|
|
|
"jsonencode": stdlib.JSONEncodeFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"length": funcs.LengthFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"list": unimplFunc, // TODO
|
|
|
|
"log": unimplFunc, // TODO
|
2018-05-22 02:39:26 +02:00
|
|
|
"lookup": unimplFunc, // TODO
|
2018-04-04 03:11:28 +02:00
|
|
|
"lower": stdlib.LowerFunc,
|
|
|
|
"map": unimplFunc, // TODO
|
|
|
|
"max": stdlib.MaxFunc,
|
|
|
|
"md5": unimplFunc, // TODO
|
|
|
|
"merge": unimplFunc, // TODO
|
|
|
|
"min": stdlib.MinFunc,
|
2018-05-23 00:43:29 +02:00
|
|
|
"pathexpand": funcs.PathExpandFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"pow": unimplFunc, // TODO
|
|
|
|
"replace": unimplFunc, // TODO
|
|
|
|
"rsadecrypt": unimplFunc, // TODO
|
|
|
|
"sha1": unimplFunc, // TODO
|
|
|
|
"sha256": unimplFunc, // TODO
|
|
|
|
"sha512": unimplFunc, // TODO
|
|
|
|
"signum": unimplFunc, // TODO
|
|
|
|
"slice": unimplFunc, // TODO
|
2018-05-22 02:39:26 +02:00
|
|
|
"sort": funcs.SortFunc,
|
|
|
|
"split": funcs.SplitFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"substr": stdlib.SubstrFunc,
|
|
|
|
"timestamp": unimplFunc, // TODO
|
|
|
|
"timeadd": unimplFunc, // TODO
|
|
|
|
"title": unimplFunc, // TODO
|
|
|
|
"transpose": unimplFunc, // TODO
|
|
|
|
"trimspace": unimplFunc, // TODO
|
|
|
|
"upper": stdlib.UpperFunc,
|
|
|
|
"urlencode": unimplFunc, // TODO
|
2018-05-22 02:39:26 +02:00
|
|
|
"uuid": funcs.UUIDFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"zipmap": unimplFunc, // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.PureOnly {
|
|
|
|
// Force our few impure functions to return unknown so that we
|
|
|
|
// can defer evaluating them until a later pass.
|
|
|
|
for _, name := range impureFunctions {
|
|
|
|
s.funcs[name] = function.Unpredictable(s.funcs[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.funcsLock.Unlock()
|
|
|
|
|
|
|
|
return s.funcs
|
|
|
|
}
|
|
|
|
|
|
|
|
var unimplFunc = function.New(&function.Spec{
|
|
|
|
Type: func([]cty.Value) (cty.Type, error) {
|
|
|
|
return cty.DynamicPseudoType, fmt.Errorf("function not yet implemented")
|
|
|
|
},
|
|
|
|
Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
|
|
|
|
return cty.DynamicVal, fmt.Errorf("function not yet implemented")
|
|
|
|
},
|
|
|
|
})
|