mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
Update go-cty to pull in more stdlib funcs.
I needed "split" specifically so I can do something like: ```hcl variable PLATFORMS { default = "linux/amd64" } target foo { platforms = split(",", "${PLATFORMS}") # other stuff } ``` Where the existing "csvdecode" does not work for this because it parses the string into a list of objects instead of a list of strings. I went ahead and just added all the available new functions. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
1179
vendor/github.com/zclconf/go-cty/cty/function/stdlib/collection.go
generated
vendored
1179
vendor/github.com/zclconf/go-cty/cty/function/stdlib/collection.go
generated
vendored
File diff suppressed because it is too large
Load Diff
87
vendor/github.com/zclconf/go-cty/cty/function/stdlib/conversion.go
generated
vendored
Normal file
87
vendor/github.com/zclconf/go-cty/cty/function/stdlib/conversion.go
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/convert"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
)
|
||||
|
||||
// MakeToFunc constructs a "to..." function, like "tostring", which converts
|
||||
// its argument to a specific type or type kind.
|
||||
//
|
||||
// The given type wantTy can be any type constraint that cty's "convert" package
|
||||
// would accept. In particular, this means that you can pass
|
||||
// cty.List(cty.DynamicPseudoType) to mean "list of any single type", which
|
||||
// will then cause cty to attempt to unify all of the element types when given
|
||||
// a tuple.
|
||||
func MakeToFunc(wantTy cty.Type) function.Function {
|
||||
return function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "v",
|
||||
// We use DynamicPseudoType rather than wantTy here so that
|
||||
// all values will pass through the function API verbatim and
|
||||
// we can handle the conversion logic within the Type and
|
||||
// Impl functions. This allows us to customize the error
|
||||
// messages to be more appropriate for an explicit type
|
||||
// conversion, whereas the cty function system produces
|
||||
// messages aimed at _implicit_ type conversions.
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowNull: true,
|
||||
},
|
||||
},
|
||||
Type: func(args []cty.Value) (cty.Type, error) {
|
||||
gotTy := args[0].Type()
|
||||
if gotTy.Equals(wantTy) {
|
||||
return wantTy, nil
|
||||
}
|
||||
conv := convert.GetConversionUnsafe(args[0].Type(), wantTy)
|
||||
if conv == nil {
|
||||
// We'll use some specialized errors for some trickier cases,
|
||||
// but most we can handle in a simple way.
|
||||
switch {
|
||||
case gotTy.IsTupleType() && wantTy.IsTupleType():
|
||||
return cty.NilType, function.NewArgErrorf(0, "incompatible tuple type for conversion: %s", convert.MismatchMessage(gotTy, wantTy))
|
||||
case gotTy.IsObjectType() && wantTy.IsObjectType():
|
||||
return cty.NilType, function.NewArgErrorf(0, "incompatible object type for conversion: %s", convert.MismatchMessage(gotTy, wantTy))
|
||||
default:
|
||||
return cty.NilType, function.NewArgErrorf(0, "cannot convert %s to %s", gotTy.FriendlyName(), wantTy.FriendlyNameForConstraint())
|
||||
}
|
||||
}
|
||||
// If a conversion is available then everything is fine.
|
||||
return wantTy, nil
|
||||
},
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
// We didn't set "AllowUnknown" on our argument, so it is guaranteed
|
||||
// to be known here but may still be null.
|
||||
ret, err := convert.Convert(args[0], retType)
|
||||
if err != nil {
|
||||
// Because we used GetConversionUnsafe above, conversion can
|
||||
// still potentially fail in here. For example, if the user
|
||||
// asks to convert the string "a" to bool then we'll
|
||||
// optimistically permit it during type checking but fail here
|
||||
// once we note that the value isn't either "true" or "false".
|
||||
gotTy := args[0].Type()
|
||||
switch {
|
||||
case gotTy == cty.String && wantTy == cty.Bool:
|
||||
what := "string"
|
||||
if !args[0].IsNull() {
|
||||
what = strconv.Quote(args[0].AsString())
|
||||
}
|
||||
return cty.NilVal, function.NewArgErrorf(0, `cannot convert %s to bool; only the strings "true" or "false" are allowed`, what)
|
||||
case gotTy == cty.String && wantTy == cty.Number:
|
||||
what := "string"
|
||||
if !args[0].IsNull() {
|
||||
what = strconv.Quote(args[0].AsString())
|
||||
}
|
||||
return cty.NilVal, function.NewArgErrorf(0, `cannot convert %s to number; given string must be a decimal representation of a number`, what)
|
||||
default:
|
||||
return cty.NilVal, function.NewArgErrorf(0, "cannot convert %s to %s", gotTy.FriendlyName(), wantTy.FriendlyNameForConstraint())
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
},
|
||||
})
|
||||
}
|
44
vendor/github.com/zclconf/go-cty/cty/function/stdlib/datetime.go
generated
vendored
44
vendor/github.com/zclconf/go-cty/cty/function/stdlib/datetime.go
generated
vendored
@ -203,6 +203,33 @@ var FormatDateFunc = function.New(&function.Spec{
|
||||
},
|
||||
})
|
||||
|
||||
// TimeAddFunc is a function that adds a duration to a timestamp, returning a new timestamp.
|
||||
var TimeAddFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "timestamp",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "duration",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
ts, err := parseTimestamp(args[0].AsString())
|
||||
if err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
duration, err := time.ParseDuration(args[1].AsString())
|
||||
if err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
|
||||
return cty.StringVal(ts.Add(duration).Format(time.RFC3339)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// FormatDate reformats a timestamp given in RFC3339 syntax into another time
|
||||
// syntax defined by a given format string.
|
||||
//
|
||||
@ -383,3 +410,20 @@ func splitDateFormat(data []byte, atEOF bool) (advance int, token []byte, err er
|
||||
func startsDateFormatVerb(b byte) bool {
|
||||
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
|
||||
}
|
||||
|
||||
// TimeAdd adds a duration to a timestamp, returning a new timestamp.
|
||||
//
|
||||
// In the HCL language, timestamps are conventionally represented as
|
||||
// strings using RFC 3339 "Date and Time format" syntax. Timeadd requires
|
||||
// the timestamp argument to be a string conforming to this syntax.
|
||||
//
|
||||
// `duration` is a string representation of a time difference, consisting of
|
||||
// sequences of number and unit pairs, like `"1.5h"` or `1h30m`. The accepted
|
||||
// units are `ns`, `us` (or `µs`), `"ms"`, `"s"`, `"m"`, and `"h"`. The first
|
||||
// number may be negative to indicate a negative duration, like `"-2h5m"`.
|
||||
//
|
||||
// The result is a string, also in RFC 3339 format, representing the result
|
||||
// of adding the given direction to the given timestamp.
|
||||
func TimeAdd(timestamp cty.Value, duration cty.Value) (cty.Value, error) {
|
||||
return TimeAddFunc.Call([]cty.Value{timestamp, duration})
|
||||
}
|
||||
|
2
vendor/github.com/zclconf/go-cty/cty/function/stdlib/format.go
generated
vendored
2
vendor/github.com/zclconf/go-cty/cty/function/stdlib/format.go
generated
vendored
@ -6,7 +6,7 @@ import (
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/apparentlymart/go-textseg/textseg"
|
||||
"github.com/apparentlymart/go-textseg/v12/textseg"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/convert"
|
||||
|
215
vendor/github.com/zclconf/go-cty/cty/function/stdlib/number.go
generated
vendored
215
vendor/github.com/zclconf/go-cty/cty/function/stdlib/number.go
generated
vendored
@ -2,10 +2,12 @@ package stdlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
"github.com/zclconf/go-cty/cty/gocty"
|
||||
)
|
||||
|
||||
var AbsoluteFunc = function.New(&function.Spec{
|
||||
@ -358,6 +360,188 @@ var IntFunc = function.New(&function.Spec{
|
||||
},
|
||||
})
|
||||
|
||||
// CeilFunc is a function that returns the closest whole number greater
|
||||
// than or equal to the given value.
|
||||
var CeilFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var val float64
|
||||
if err := gocty.FromCtyValue(args[0], &val); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
if math.IsInf(val, 0) {
|
||||
return cty.NumberFloatVal(val), nil
|
||||
}
|
||||
return cty.NumberIntVal(int64(math.Ceil(val))), nil
|
||||
},
|
||||
})
|
||||
|
||||
// FloorFunc is a function that returns the closest whole number lesser
|
||||
// than or equal to the given value.
|
||||
var FloorFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var val float64
|
||||
if err := gocty.FromCtyValue(args[0], &val); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
if math.IsInf(val, 0) {
|
||||
return cty.NumberFloatVal(val), nil
|
||||
}
|
||||
return cty.NumberIntVal(int64(math.Floor(val))), nil
|
||||
},
|
||||
})
|
||||
|
||||
// LogFunc is a function that returns the logarithm of a given number in a given base.
|
||||
var LogFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
{
|
||||
Name: "base",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var num float64
|
||||
if err := gocty.FromCtyValue(args[0], &num); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
|
||||
var base float64
|
||||
if err := gocty.FromCtyValue(args[1], &base); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
|
||||
return cty.NumberFloatVal(math.Log(num) / math.Log(base)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// PowFunc is a function that returns the logarithm of a given number in a given base.
|
||||
var PowFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
{
|
||||
Name: "power",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var num float64
|
||||
if err := gocty.FromCtyValue(args[0], &num); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
|
||||
var power float64
|
||||
if err := gocty.FromCtyValue(args[1], &power); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
|
||||
return cty.NumberFloatVal(math.Pow(num, power)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// SignumFunc is a function that determines the sign of a number, returning a
|
||||
// number between -1 and 1 to represent the sign..
|
||||
var SignumFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var num int
|
||||
if err := gocty.FromCtyValue(args[0], &num); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
switch {
|
||||
case num < 0:
|
||||
return cty.NumberIntVal(-1), nil
|
||||
case num > 0:
|
||||
return cty.NumberIntVal(+1), nil
|
||||
default:
|
||||
return cty.NumberIntVal(0), nil
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
// ParseIntFunc is a function that parses a string argument and returns an integer of the specified base.
|
||||
var ParseIntFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "number",
|
||||
Type: cty.DynamicPseudoType,
|
||||
},
|
||||
{
|
||||
Name: "base",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
|
||||
Type: func(args []cty.Value) (cty.Type, error) {
|
||||
if !args[0].Type().Equals(cty.String) {
|
||||
return cty.Number, function.NewArgErrorf(0, "first argument must be a string, not %s", args[0].Type().FriendlyName())
|
||||
}
|
||||
return cty.Number, nil
|
||||
},
|
||||
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
var numstr string
|
||||
var base int
|
||||
var err error
|
||||
|
||||
if err = gocty.FromCtyValue(args[0], &numstr); err != nil {
|
||||
return cty.UnknownVal(cty.String), function.NewArgError(0, err)
|
||||
}
|
||||
|
||||
if err = gocty.FromCtyValue(args[1], &base); err != nil {
|
||||
return cty.UnknownVal(cty.Number), function.NewArgError(1, err)
|
||||
}
|
||||
|
||||
if base < 2 || base > 62 {
|
||||
return cty.UnknownVal(cty.Number), function.NewArgErrorf(
|
||||
1,
|
||||
"base must be a whole number between 2 and 62 inclusive",
|
||||
)
|
||||
}
|
||||
|
||||
num, ok := (&big.Int{}).SetString(numstr, base)
|
||||
if !ok {
|
||||
return cty.UnknownVal(cty.Number), function.NewArgErrorf(
|
||||
0,
|
||||
"cannot parse %q as a base %d integer",
|
||||
numstr,
|
||||
base,
|
||||
)
|
||||
}
|
||||
|
||||
parsedNum := cty.NumberVal((&big.Float{}).SetInt(num))
|
||||
|
||||
return parsedNum, nil
|
||||
},
|
||||
})
|
||||
|
||||
// Absolute returns the magnitude of the given number, without its sign.
|
||||
// That is, it turns negative values into positive values.
|
||||
func Absolute(num cty.Value) (cty.Value, error) {
|
||||
@ -436,3 +620,34 @@ func Int(num cty.Value) (cty.Value, error) {
|
||||
}
|
||||
return IntFunc.Call([]cty.Value{num})
|
||||
}
|
||||
|
||||
// Ceil returns the closest whole number greater than or equal to the given value.
|
||||
func Ceil(num cty.Value) (cty.Value, error) {
|
||||
return CeilFunc.Call([]cty.Value{num})
|
||||
}
|
||||
|
||||
// Floor returns the closest whole number lesser than or equal to the given value.
|
||||
func Floor(num cty.Value) (cty.Value, error) {
|
||||
return FloorFunc.Call([]cty.Value{num})
|
||||
}
|
||||
|
||||
// Log returns returns the logarithm of a given number in a given base.
|
||||
func Log(num, base cty.Value) (cty.Value, error) {
|
||||
return LogFunc.Call([]cty.Value{num, base})
|
||||
}
|
||||
|
||||
// Pow returns the logarithm of a given number in a given base.
|
||||
func Pow(num, power cty.Value) (cty.Value, error) {
|
||||
return PowFunc.Call([]cty.Value{num, power})
|
||||
}
|
||||
|
||||
// Signum determines the sign of a number, returning a number between -1 and
|
||||
// 1 to represent the sign.
|
||||
func Signum(num cty.Value) (cty.Value, error) {
|
||||
return SignumFunc.Call([]cty.Value{num})
|
||||
}
|
||||
|
||||
// ParseInt parses a string argument and returns an integer of the specified base.
|
||||
func ParseInt(num cty.Value, base cty.Value) (cty.Value, error) {
|
||||
return ParseIntFunc.Call([]cty.Value{num, base})
|
||||
}
|
||||
|
315
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string.go
generated
vendored
315
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string.go
generated
vendored
@ -1,9 +1,13 @@
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/apparentlymart/go-textseg/textseg"
|
||||
"github.com/apparentlymart/go-textseg/v12/textseg"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
"github.com/zclconf/go-cty/cty/gocty"
|
||||
@ -140,8 +144,14 @@ var SubstrFunc = function.New(&function.Spec{
|
||||
}
|
||||
|
||||
offset += totalLen
|
||||
} else if length == 0 {
|
||||
// Short circuit here, after error checks, because if a
|
||||
// string of length 0 has been requested it will always
|
||||
// be the empty string
|
||||
return cty.StringVal(""), nil
|
||||
}
|
||||
|
||||
|
||||
sub := in
|
||||
pos := 0
|
||||
var i int
|
||||
@ -187,6 +197,252 @@ var SubstrFunc = function.New(&function.Spec{
|
||||
},
|
||||
})
|
||||
|
||||
var JoinFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "separator",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "lists",
|
||||
Type: cty.List(cty.String),
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
sep := args[0].AsString()
|
||||
listVals := args[1:]
|
||||
if len(listVals) < 1 {
|
||||
return cty.UnknownVal(cty.String), fmt.Errorf("at least one list is required")
|
||||
}
|
||||
|
||||
l := 0
|
||||
for _, list := range listVals {
|
||||
if !list.IsWhollyKnown() {
|
||||
return cty.UnknownVal(cty.String), nil
|
||||
}
|
||||
l += list.LengthInt()
|
||||
}
|
||||
|
||||
items := make([]string, 0, l)
|
||||
for ai, list := range listVals {
|
||||
ei := 0
|
||||
for it := list.ElementIterator(); it.Next(); {
|
||||
_, val := it.Element()
|
||||
if val.IsNull() {
|
||||
if len(listVals) > 1 {
|
||||
return cty.UnknownVal(cty.String), function.NewArgErrorf(ai+1, "element %d of list %d is null; cannot concatenate null values", ei, ai+1)
|
||||
}
|
||||
return cty.UnknownVal(cty.String), function.NewArgErrorf(ai+1, "element %d is null; cannot concatenate null values", ei)
|
||||
}
|
||||
items = append(items, val.AsString())
|
||||
ei++
|
||||
}
|
||||
}
|
||||
|
||||
return cty.StringVal(strings.Join(items, sep)), nil
|
||||
},
|
||||
})
|
||||
|
||||
var SortFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
Type: cty.List(cty.String),
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
listVal := args[0]
|
||||
|
||||
if !listVal.IsWhollyKnown() {
|
||||
// If some of the element values aren't known yet then we
|
||||
// can't yet predict the order of the result.
|
||||
return cty.UnknownVal(retType), nil
|
||||
}
|
||||
if listVal.LengthInt() == 0 { // Easy path
|
||||
return listVal, nil
|
||||
}
|
||||
|
||||
list := make([]string, 0, listVal.LengthInt())
|
||||
for it := listVal.ElementIterator(); it.Next(); {
|
||||
iv, v := it.Element()
|
||||
if v.IsNull() {
|
||||
return cty.UnknownVal(retType), fmt.Errorf("given list element %s is null; a null string cannot be sorted", iv.AsBigFloat().String())
|
||||
}
|
||||
list = append(list, v.AsString())
|
||||
}
|
||||
|
||||
sort.Strings(list)
|
||||
retVals := make([]cty.Value, len(list))
|
||||
for i, s := range list {
|
||||
retVals[i] = cty.StringVal(s)
|
||||
}
|
||||
return cty.ListVal(retVals), nil
|
||||
},
|
||||
})
|
||||
|
||||
var SplitFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "separator",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
sep := args[0].AsString()
|
||||
str := args[1].AsString()
|
||||
elems := strings.Split(str, sep)
|
||||
elemVals := make([]cty.Value, len(elems))
|
||||
for i, s := range elems {
|
||||
elemVals[i] = cty.StringVal(s)
|
||||
}
|
||||
if len(elemVals) == 0 {
|
||||
return cty.ListValEmpty(cty.String), nil
|
||||
}
|
||||
return cty.ListVal(elemVals), nil
|
||||
},
|
||||
})
|
||||
|
||||
// ChompFunc is a function that removes newline characters at the end of a
|
||||
// string.
|
||||
var ChompFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
newlines := regexp.MustCompile(`(?:\r\n?|\n)*\z`)
|
||||
return cty.StringVal(newlines.ReplaceAllString(args[0].AsString(), "")), nil
|
||||
},
|
||||
})
|
||||
|
||||
// IndentFunc is a function that adds a given number of spaces to the
|
||||
// beginnings of all but the first line in a given multi-line string.
|
||||
var IndentFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "spaces",
|
||||
Type: cty.Number,
|
||||
},
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var spaces int
|
||||
if err := gocty.FromCtyValue(args[0], &spaces); err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
data := args[1].AsString()
|
||||
pad := strings.Repeat(" ", spaces)
|
||||
return cty.StringVal(strings.Replace(data, "\n", "\n"+pad, -1)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TitleFunc is a function that converts the first letter of each word in the
|
||||
// given string to uppercase.
|
||||
var TitleFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return cty.StringVal(strings.Title(args[0].AsString())), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimSpaceFunc is a function that removes any space characters from the start
|
||||
// and end of the given string.
|
||||
var TrimSpaceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return cty.StringVal(strings.TrimSpace(args[0].AsString())), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimFunc is a function that removes the specified characters from the start
|
||||
// and end of the given string.
|
||||
var TrimFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "cutset",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
return cty.StringVal(strings.Trim(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimPrefixFunc is a function that removes the specified characters from the
|
||||
// start the given string.
|
||||
var TrimPrefixFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "prefix",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
prefix := args[1].AsString()
|
||||
return cty.StringVal(strings.TrimPrefix(str, prefix)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimSuffixFunc is a function that removes the specified characters from the
|
||||
// end of the given string.
|
||||
var TrimSuffixFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "suffix",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
return cty.StringVal(strings.TrimSuffix(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// Upper is a Function that converts a given string to uppercase.
|
||||
func Upper(str cty.Value) (cty.Value, error) {
|
||||
return UpperFunc.Call([]cty.Value{str})
|
||||
@ -232,3 +488,60 @@ func Strlen(str cty.Value) (cty.Value, error) {
|
||||
func Substr(str cty.Value, offset cty.Value, length cty.Value) (cty.Value, error) {
|
||||
return SubstrFunc.Call([]cty.Value{str, offset, length})
|
||||
}
|
||||
|
||||
// Join concatenates together the string elements of one or more lists with a
|
||||
// given separator.
|
||||
func Join(sep cty.Value, lists ...cty.Value) (cty.Value, error) {
|
||||
args := make([]cty.Value, len(lists)+1)
|
||||
args[0] = sep
|
||||
copy(args[1:], lists)
|
||||
return JoinFunc.Call(args)
|
||||
}
|
||||
|
||||
// Sort re-orders the elements of a given list of strings so that they are
|
||||
// in ascending lexicographical order.
|
||||
func Sort(list cty.Value) (cty.Value, error) {
|
||||
return SortFunc.Call([]cty.Value{list})
|
||||
}
|
||||
|
||||
// Split divides a given string by a given separator, returning a list of
|
||||
// strings containing the characters between the separator sequences.
|
||||
func Split(sep, str cty.Value) (cty.Value, error) {
|
||||
return SplitFunc.Call([]cty.Value{sep, str})
|
||||
}
|
||||
|
||||
// Chomp removes newline characters at the end of a string.
|
||||
func Chomp(str cty.Value) (cty.Value, error) {
|
||||
return ChompFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// Indent adds a given number of spaces to the beginnings of all but the first
|
||||
// line in a given multi-line string.
|
||||
func Indent(spaces, str cty.Value) (cty.Value, error) {
|
||||
return IndentFunc.Call([]cty.Value{spaces, str})
|
||||
}
|
||||
|
||||
// Title converts the first letter of each word in the given string to uppercase.
|
||||
func Title(str cty.Value) (cty.Value, error) {
|
||||
return TitleFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// TrimSpace removes any space characters from the start and end of the given string.
|
||||
func TrimSpace(str cty.Value) (cty.Value, error) {
|
||||
return TrimSpaceFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// Trim removes the specified characters from the start and end of the given string.
|
||||
func Trim(str, cutset cty.Value) (cty.Value, error) {
|
||||
return TrimFunc.Call([]cty.Value{str, cutset})
|
||||
}
|
||||
|
||||
// TrimPrefix removes the specified prefix from the start of the given string.
|
||||
func TrimPrefix(str, prefix cty.Value) (cty.Value, error) {
|
||||
return TrimPrefixFunc.Call([]cty.Value{str, prefix})
|
||||
}
|
||||
|
||||
// TrimSuffix removes the specified suffix from the end of the given string.
|
||||
func TrimSuffix(str, suffix cty.Value) (cty.Value, error) {
|
||||
return TrimSuffixFunc.Call([]cty.Value{str, suffix})
|
||||
}
|
||||
|
80
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string_replace.go
generated
vendored
Normal file
80
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string_replace.go
generated
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
)
|
||||
|
||||
// ReplaceFunc is a function that searches a given string for another given
|
||||
// substring, and replaces each occurence with a given replacement string.
|
||||
// The substr argument is a simple string.
|
||||
var ReplaceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "substr",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "replace",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
substr := args[1].AsString()
|
||||
replace := args[2].AsString()
|
||||
|
||||
return cty.StringVal(strings.Replace(str, substr, replace, -1)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// RegexReplaceFunc is a function that searches a given string for another
|
||||
// given substring, and replaces each occurence with a given replacement
|
||||
// string. The substr argument must be a valid regular expression.
|
||||
var RegexReplaceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "substr",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "replace",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
str := args[0].AsString()
|
||||
substr := args[1].AsString()
|
||||
replace := args[2].AsString()
|
||||
|
||||
re, err := regexp.Compile(substr)
|
||||
if err != nil {
|
||||
return cty.UnknownVal(cty.String), err
|
||||
}
|
||||
|
||||
return cty.StringVal(re.ReplaceAllString(str, replace)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// Replace searches a given string for another given substring,
|
||||
// and replaces all occurrences with a given replacement string.
|
||||
func Replace(str, substr, replace cty.Value) (cty.Value, error) {
|
||||
return ReplaceFunc.Call([]cty.Value{str, substr, replace})
|
||||
}
|
||||
|
||||
func RegexReplace(str, substr, replace cty.Value) (cty.Value, error) {
|
||||
return RegexReplaceFunc.Call([]cty.Value{str, substr, replace})
|
||||
}
|
Reference in New Issue
Block a user