mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
vendor: update github.com/hashicorp/hcl/v2 to v2.19.1
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
3
vendor/github.com/zclconf/go-cty/cty/function/argument.go
generated
vendored
3
vendor/github.com/zclconf/go-cty/cty/function/argument.go
generated
vendored
@ -10,6 +10,9 @@ type Parameter struct {
|
||||
// value, but callers may use it for documentation, etc.
|
||||
Name string
|
||||
|
||||
// Description is an optional description for the argument.
|
||||
Description string
|
||||
|
||||
// A type that any argument for this parameter must conform to.
|
||||
// cty.DynamicPseudoType can be used, either at top-level or nested
|
||||
// in a parameterized type, to indicate that any type should be
|
||||
|
91
vendor/github.com/zclconf/go-cty/cty/function/function.go
generated
vendored
91
vendor/github.com/zclconf/go-cty/cty/function/function.go
generated
vendored
@ -14,6 +14,9 @@ type Function struct {
|
||||
// Spec is the specification of a function, used to instantiate
|
||||
// a new Function.
|
||||
type Spec struct {
|
||||
// Description is an optional description for the function specification.
|
||||
Description string
|
||||
|
||||
// Params is a description of the positional parameters for the function.
|
||||
// The standard checking logic rejects any calls that do not provide
|
||||
// arguments conforming to this definition, freeing the function
|
||||
@ -36,6 +39,19 @@ type Spec struct {
|
||||
// depending on its arguments.
|
||||
Type TypeFunc
|
||||
|
||||
// RefineResult is an optional callback for describing additional
|
||||
// refinements for the result value beyond what can be described using
|
||||
// a type constraint.
|
||||
//
|
||||
// A refinement callback should always return the same builder it was
|
||||
// given, typically after modifying it using the methods of
|
||||
// [cty.RefinementBuilder].
|
||||
//
|
||||
// Any refinements described by this callback must hold for the entire
|
||||
// range of results from the function. For refinements that only apply
|
||||
// to certain results, use direct refinement within [Impl] instead.
|
||||
RefineResult func(*cty.RefinementBuilder) *cty.RefinementBuilder
|
||||
|
||||
// Impl is the ImplFunc that implements the function's behavior.
|
||||
//
|
||||
// Functions are expected to behave as pure functions, and not create
|
||||
@ -230,6 +246,22 @@ func (f Function) Call(args []cty.Value) (val cty.Value, err error) {
|
||||
return cty.NilVal, err
|
||||
}
|
||||
|
||||
if refineResult := f.spec.RefineResult; refineResult != nil {
|
||||
// If this function has a refinement callback then we'll refine
|
||||
// our result value in the same way regardless of how we return.
|
||||
// It's the function author's responsibility to ensure that the
|
||||
// refinements they specify are valid for the full range of possible
|
||||
// return values from the function. If not, this will panic when
|
||||
// detecting an inconsistency.
|
||||
defer func() {
|
||||
if val != cty.NilVal {
|
||||
if val.IsKnown() || val.Type() != cty.DynamicPseudoType {
|
||||
val = val.RefineWith(refineResult)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Type checking already dealt with most situations relating to our
|
||||
// parameter specification, but we still need to deal with unknown
|
||||
// values and marked values.
|
||||
@ -344,3 +376,62 @@ func (f Function) VarParam() *Parameter {
|
||||
ret := *f.spec.VarParam
|
||||
return &ret
|
||||
}
|
||||
|
||||
// Description returns a human-readable description of the function.
|
||||
func (f Function) Description() string {
|
||||
return f.spec.Description
|
||||
}
|
||||
|
||||
// WithNewDescriptions returns a new function that has the same signature
|
||||
// and implementation as the receiver but has the function description and
|
||||
// the parameter descriptions replaced with those given in the arguments.
|
||||
//
|
||||
// All descriptions may be given as an empty string to specify that there
|
||||
// should be no description at all.
|
||||
//
|
||||
// The paramDescs argument must match the number of parameters
|
||||
// the reciever expects, or this function will panic. If the function has a
|
||||
// VarParam then that counts as one parameter for the sake of this rule. The
|
||||
// given descriptions will be assigned in order starting with the positional
|
||||
// arguments in their declared order, followed by the variadic parameter if
|
||||
// any.
|
||||
//
|
||||
// As a special case, WithNewDescriptions will accept a paramDescs which
|
||||
// does not cover the reciever's variadic parameter (if any), so that it's
|
||||
// possible to add a variadic parameter to a function which didn't previously
|
||||
// have one without that being a breaking change for an existing caller using
|
||||
// WithNewDescriptions against that function. In this case the base description
|
||||
// of the variadic parameter will be preserved.
|
||||
func (f Function) WithNewDescriptions(funcDesc string, paramDescs []string) Function {
|
||||
retSpec := *f.spec // shallow copy of the reciever
|
||||
retSpec.Description = funcDesc
|
||||
|
||||
retSpec.Params = make([]Parameter, len(f.spec.Params))
|
||||
copy(retSpec.Params, f.spec.Params) // shallow copy of positional parameters
|
||||
if f.spec.VarParam != nil {
|
||||
retVarParam := *f.spec.VarParam // shallow copy of variadic parameter
|
||||
retSpec.VarParam = &retVarParam
|
||||
}
|
||||
|
||||
if retSpec.VarParam != nil {
|
||||
if with, without := len(retSpec.Params)+1, len(retSpec.Params); len(paramDescs) != with && len(paramDescs) != without {
|
||||
panic(fmt.Sprintf("paramDescs must have length of either %d or %d", with, without))
|
||||
}
|
||||
} else {
|
||||
if want := len(retSpec.Params); len(paramDescs) != want {
|
||||
panic(fmt.Sprintf("paramDescs must have length %d", want))
|
||||
}
|
||||
}
|
||||
|
||||
posParamDescs := paramDescs[:len(retSpec.Params)]
|
||||
varParamDescs := paramDescs[len(retSpec.Params):] // guaranteed to be zero or one elements because of the rules above
|
||||
|
||||
for i, desc := range posParamDescs {
|
||||
retSpec.Params[i].Description = desc
|
||||
}
|
||||
for _, desc := range varParamDescs {
|
||||
retSpec.VarParam.Description = desc
|
||||
}
|
||||
|
||||
return New(&retSpec)
|
||||
}
|
||||
|
12
vendor/github.com/zclconf/go-cty/cty/function/stdlib/bool.go
generated
vendored
12
vendor/github.com/zclconf/go-cty/cty/function/stdlib/bool.go
generated
vendored
@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
var NotFunc = function.New(&function.Spec{
|
||||
Description: `Applies the logical NOT operation to the given boolean value.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "val",
|
||||
@ -14,13 +15,15 @@ var NotFunc = function.New(&function.Spec{
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
return args[0].Not(), nil
|
||||
},
|
||||
})
|
||||
|
||||
var AndFunc = function.New(&function.Spec{
|
||||
Description: `Applies the logical AND operation to the given boolean values.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -35,13 +38,15 @@ var AndFunc = function.New(&function.Spec{
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
return args[0].And(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var OrFunc = function.New(&function.Spec{
|
||||
Description: `Applies the logical OR operation to the given boolean values.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -56,7 +61,8 @@ var OrFunc = function.New(&function.Spec{
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
return args[0].Or(args[1]), nil
|
||||
},
|
||||
|
8
vendor/github.com/zclconf/go-cty/cty/function/stdlib/bytes.go
generated
vendored
8
vendor/github.com/zclconf/go-cty/cty/function/stdlib/bytes.go
generated
vendored
@ -30,6 +30,7 @@ func BytesVal(buf []byte) cty.Value {
|
||||
// BytesLen is a Function that returns the length of the buffer encapsulated
|
||||
// in a Bytes value.
|
||||
var BytesLenFunc = function.New(&function.Spec{
|
||||
Description: `Returns the total number of bytes in the given buffer.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "buf",
|
||||
@ -37,7 +38,8 @@ var BytesLenFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
bufPtr := args[0].EncapsulatedValue().(*[]byte)
|
||||
return cty.NumberIntVal(int64(len(*bufPtr))), nil
|
||||
@ -46,6 +48,7 @@ var BytesLenFunc = function.New(&function.Spec{
|
||||
|
||||
// BytesSlice is a Function that returns a slice of the given Bytes value.
|
||||
var BytesSliceFunc = function.New(&function.Spec{
|
||||
Description: `Extracts a subslice from the given buffer.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "buf",
|
||||
@ -63,7 +66,8 @@ var BytesSliceFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(Bytes),
|
||||
Type: function.StaticReturnType(Bytes),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
bufPtr := args[0].EncapsulatedValue().(*[]byte)
|
||||
|
||||
|
120
vendor/github.com/zclconf/go-cty/cty/function/stdlib/collection.go
generated
vendored
120
vendor/github.com/zclconf/go-cty/cty/function/stdlib/collection.go
generated
vendored
@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
var HasIndexFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if if the given collection can be indexed with the given key without producing an error, or false otherwise.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "collection",
|
||||
@ -31,12 +32,14 @@ var HasIndexFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Bool, nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].HasIndex(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var IndexFunc = function.New(&function.Spec{
|
||||
Description: `Returns the element with the given key from the given collection, or raises an error if there is no such element.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "collection",
|
||||
@ -106,11 +109,13 @@ var IndexFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var LengthFunc = function.New(&function.Spec{
|
||||
Description: `Returns the number of elements in the given collection.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "collection",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowDynamicType: true,
|
||||
AllowUnknown: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
@ -121,12 +126,14 @@ var LengthFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Number, nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].Length(), nil
|
||||
},
|
||||
})
|
||||
|
||||
var ElementFunc = function.New(&function.Spec{
|
||||
Description: `Returns the element with the given index from the given list or tuple, applying the modulo operation to the given index if it's greater than the number of elements.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
@ -206,9 +213,11 @@ var ElementFunc = function.New(&function.Spec{
|
||||
// CoalesceListFunc is a function that takes any number of list arguments
|
||||
// and returns the first one that isn't empty.
|
||||
var CoalesceListFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Returns the first of the given sequences that has a length greater than zero.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "vals",
|
||||
Description: `List or tuple values to test in the given order.`,
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
@ -245,6 +254,7 @@ var CoalesceListFunc = function.New(&function.Spec{
|
||||
|
||||
return last, nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
for _, arg := range args {
|
||||
if !arg.IsKnown() {
|
||||
@ -270,13 +280,15 @@ var CoalesceListFunc = function.New(&function.Spec{
|
||||
// CompactFunc is a function that takes a list of strings and returns a new list
|
||||
// with any empty string elements removed.
|
||||
var CompactFunc = function.New(&function.Spec{
|
||||
Description: `Removes all empty string elements from the given list of strings.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
Type: cty.List(cty.String),
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
listVal := args[0]
|
||||
if !listVal.IsWhollyKnown() {
|
||||
@ -306,6 +318,7 @@ var CompactFunc = function.New(&function.Spec{
|
||||
// ContainsFunc is a function that determines whether a given list or
|
||||
// set contains a given single value as one of its elements.
|
||||
var ContainsFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if the given value is a value in the given list, tuple, or set, or false otherwise.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
@ -316,7 +329,8 @@ var ContainsFunc = function.New(&function.Spec{
|
||||
Type: cty.DynamicPseudoType,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
arg := args[0]
|
||||
ty := arg.Type()
|
||||
@ -364,6 +378,7 @@ var ContainsFunc = function.New(&function.Spec{
|
||||
// DistinctFunc is a function that takes a list and returns a new list
|
||||
// with any duplicate elements removed.
|
||||
var DistinctFunc = function.New(&function.Spec{
|
||||
Description: `Removes any duplicate values from the given list, preserving the order of remaining elements.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
@ -373,6 +388,7 @@ var DistinctFunc = function.New(&function.Spec{
|
||||
Type: func(args []cty.Value) (cty.Type, error) {
|
||||
return args[0].Type(), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
listVal := args[0]
|
||||
|
||||
@ -399,14 +415,17 @@ var DistinctFunc = function.New(&function.Spec{
|
||||
// ChunklistFunc is a function that splits a single list into fixed-size chunks,
|
||||
// returning a list of lists.
|
||||
var ChunklistFunc = function.New(&function.Spec{
|
||||
Description: `Splits a single list into multiple lists where each has at most the given number of elements.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
Description: `The list to split into chunks.`,
|
||||
Type: cty.List(cty.DynamicPseudoType),
|
||||
AllowMarked: true,
|
||||
},
|
||||
{
|
||||
Name: "size",
|
||||
Description: `The maximum length of each chunk. All but the last element of the result is guaranteed to be of exactly this size.`,
|
||||
Type: cty.Number,
|
||||
AllowMarked: true,
|
||||
},
|
||||
@ -414,6 +433,7 @@ var ChunklistFunc = function.New(&function.Spec{
|
||||
Type: func(args []cty.Value) (cty.Type, error) {
|
||||
return cty.List(args[0].Type()), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
listVal := args[0]
|
||||
sizeVal := args[1]
|
||||
@ -471,6 +491,7 @@ var ChunklistFunc = function.New(&function.Spec{
|
||||
// FlattenFunc is a function that takes a list and replaces any elements
|
||||
// that are lists with a flattened sequence of the list contents.
|
||||
var FlattenFunc = function.New(&function.Spec{
|
||||
Description: `Transforms a list, set, or tuple value into a tuple by replacing any given elements that are themselves sequences with a flattened tuple of all of the nested elements concatenated together.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
@ -500,6 +521,7 @@ var FlattenFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Tuple(tys), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
inputList := args[0]
|
||||
|
||||
@ -525,6 +547,7 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
|
||||
if len(flattenListMarks) > 0 {
|
||||
markses = append(markses, flattenListMarks)
|
||||
}
|
||||
|
||||
if !flattenList.Length().IsKnown() {
|
||||
// If we don't know the length of what we're flattening then we can't
|
||||
// predict the length of our result yet either.
|
||||
@ -542,7 +565,7 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
|
||||
isKnown = false
|
||||
}
|
||||
|
||||
if val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType() {
|
||||
if !val.IsNull() && (val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType()) {
|
||||
if !val.IsKnown() {
|
||||
isKnown = false
|
||||
_, unknownMarks := val.Unmark()
|
||||
@ -566,9 +589,11 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
|
||||
|
||||
// KeysFunc is a function that takes a map and returns a sorted list of the map keys.
|
||||
var KeysFunc = function.New(&function.Spec{
|
||||
Description: `Returns a list of the keys of the given map in lexicographical order.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "inputMap",
|
||||
Description: `The map to extract keys from. May instead be an object-typed value, in which case the result is a tuple of the object attributes.`,
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowUnknown: true,
|
||||
AllowMarked: true,
|
||||
@ -595,6 +620,7 @@ var KeysFunc = function.New(&function.Spec{
|
||||
return cty.DynamicPseudoType, function.NewArgErrorf(0, "must have map or object type")
|
||||
}
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
// We must unmark the value before we can use ElementIterator on it, and
|
||||
// then re-apply the same marks (possibly none) when we return. Since we
|
||||
@ -641,6 +667,7 @@ var KeysFunc = function.New(&function.Spec{
|
||||
|
||||
// LookupFunc is a function that performs dynamic lookups of map types.
|
||||
var LookupFunc = function.New(&function.Spec{
|
||||
Description: `Returns the value of the element with the given key from the given map, or returns the default value if there is no such element.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "inputMap",
|
||||
@ -733,7 +760,8 @@ var LookupFunc = function.New(&function.Spec{
|
||||
// If more than one given map or object defines the same key then the one that
|
||||
// is later in the argument sequence takes precedence.
|
||||
var MergeFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Merges all of the elements from the given maps into a single map, or the attributes from given objects into a single object.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "maps",
|
||||
Type: cty.DynamicPseudoType,
|
||||
@ -814,6 +842,7 @@ var MergeFunc = function.New(&function.Spec{
|
||||
|
||||
return cty.Object(attrs), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
outputMap := make(map[string]cty.Value)
|
||||
var markses []cty.ValueMarks // remember any marked maps/objects we find
|
||||
@ -849,6 +878,7 @@ var MergeFunc = function.New(&function.Spec{
|
||||
// ReverseListFunc takes a sequence and produces a new sequence of the same length
|
||||
// with all of the same elements as the given sequence but in reverse order.
|
||||
var ReverseListFunc = function.New(&function.Spec{
|
||||
Description: `Returns the given list with its elements in reverse order.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
@ -872,6 +902,7 @@ var ReverseListFunc = function.New(&function.Spec{
|
||||
return cty.NilType, function.NewArgErrorf(0, "can only reverse list or tuple values, not %s", argTy.FriendlyName())
|
||||
}
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
in, marks := args[0].Unmark()
|
||||
inVals := in.AsValueSlice()
|
||||
@ -897,11 +928,14 @@ var ReverseListFunc = function.New(&function.Spec{
|
||||
// preserving the ordering of all of the input lists. Otherwise the result is a
|
||||
// set of tuples.
|
||||
var SetProductFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Calculates the cartesian product of two or more sets.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "sets",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowMarked: true,
|
||||
Name: "sets",
|
||||
Description: "The sets to consider. Also accepts lists and tuples, and if all arguments are of list or tuple type then the result will preserve the input ordering",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowMarked: true,
|
||||
AllowUnknown: true,
|
||||
},
|
||||
Type: func(args []cty.Value) (retType cty.Type, err error) {
|
||||
if len(args) < 2 {
|
||||
@ -943,6 +977,7 @@ var SetProductFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Set(cty.Tuple(elemTys)), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
ety := retType.ElementType()
|
||||
var retMarks cty.ValueMarks
|
||||
@ -955,7 +990,7 @@ var SetProductFunc = function.New(&function.Spec{
|
||||
|
||||
// Continue processing after we find an argument with unknown
|
||||
// length to ensure that we cover all the marks
|
||||
if !arg.Length().IsKnown() {
|
||||
if !(arg.IsKnown() && arg.Length().IsKnown()) {
|
||||
hasUnknownLength = true
|
||||
continue
|
||||
}
|
||||
@ -967,7 +1002,62 @@ var SetProductFunc = function.New(&function.Spec{
|
||||
}
|
||||
|
||||
if hasUnknownLength {
|
||||
return cty.UnknownVal(retType).WithMarks(retMarks), nil
|
||||
defer func() {
|
||||
// We're definitely going to return from somewhere in this
|
||||
// branch and however we do it we must reapply the marks
|
||||
// on the way out.
|
||||
ret = ret.WithMarks(retMarks)
|
||||
}()
|
||||
ret := cty.UnknownVal(retType)
|
||||
|
||||
// Even if we don't know the exact length we may be able to
|
||||
// constrain the upper and lower bounds of the resulting length.
|
||||
maxLength := 1
|
||||
for _, arg := range args {
|
||||
arg, _ := arg.Unmark() // safe to discard marks because "retMarks" already contains them all
|
||||
argRng := arg.Range()
|
||||
ty := argRng.TypeConstraint()
|
||||
var argMaxLen int
|
||||
if ty.IsCollectionType() {
|
||||
argMaxLen = argRng.LengthUpperBound()
|
||||
} else if ty.IsTupleType() {
|
||||
argMaxLen = ty.Length()
|
||||
} else {
|
||||
// Should not get here but if we do then we'll just
|
||||
// bail out with an unrefined unknown value.
|
||||
return ret, nil
|
||||
}
|
||||
// The upper bound of a totally-unrefined collection is
|
||||
// math.MaxInt, which will quickly get us to integer overflow
|
||||
// here, and so out of pragmatism we'll just impose a reasonable
|
||||
// upper limit on what is a useful bound to track and return
|
||||
// unrefined for unusually-large input.
|
||||
if argMaxLen > 1024 { // arbitrarily-decided threshold
|
||||
return ret, nil
|
||||
}
|
||||
maxLength *= argMaxLen
|
||||
if maxLength > 2048 { // arbitrarily-decided threshold
|
||||
return ret, nil
|
||||
}
|
||||
if maxLength < 0 { // Seems like we already overflowed, then.
|
||||
return ret, nil
|
||||
}
|
||||
}
|
||||
|
||||
if maxLength == 0 {
|
||||
// This refinement will typically allow the unknown value to
|
||||
// collapse into a known empty collection.
|
||||
ret = ret.Refine().CollectionLength(0).NewValue()
|
||||
} else {
|
||||
// If we know there's a nonzero maximum number of elements then
|
||||
// set element coalescing cannot reduce to fewer than one
|
||||
// element.
|
||||
ret = ret.Refine().
|
||||
CollectionLengthLowerBound(1).
|
||||
CollectionLengthUpperBound(maxLength).
|
||||
NewValue()
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
if total == 0 {
|
||||
@ -1037,6 +1127,7 @@ var SetProductFunc = function.New(&function.Spec{
|
||||
// SliceFunc is a function that extracts some consecutive elements
|
||||
// from within a list.
|
||||
var SliceFunc = function.New(&function.Spec{
|
||||
Description: `Extracts a subslice of the given list or tuple value.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
@ -1079,6 +1170,7 @@ var SliceFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Tuple(argTy.TupleElementTypes()[startIndex:endIndex]), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
inputList, marks := args[0].Unmark()
|
||||
|
||||
@ -1158,9 +1250,10 @@ func sliceIndexes(args []cty.Value) (int, int, bool, error) {
|
||||
// ValuesFunc is a function that returns a list of the map values,
|
||||
// in the order of the sorted keys.
|
||||
var ValuesFunc = function.New(&function.Spec{
|
||||
Description: `Returns the values of elements of a given map, or the values of attributes of a given object, in lexicographic order by key or attribute name.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "values",
|
||||
Name: "mapping",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowMarked: true,
|
||||
},
|
||||
@ -1192,6 +1285,7 @@ var ValuesFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.NilType, errors.New("values() requires a map as the first argument")
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
mapVar := args[0]
|
||||
|
||||
@ -1225,6 +1319,7 @@ var ValuesFunc = function.New(&function.Spec{
|
||||
// ZipmapFunc is a function that constructs a map from a list of keys
|
||||
// and a corresponding list of values.
|
||||
var ZipmapFunc = function.New(&function.Spec{
|
||||
Description: `Constructs a map from a list of keys and a corresponding list of values, which must both be of the same length.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "keys",
|
||||
@ -1279,6 +1374,7 @@ var ZipmapFunc = function.New(&function.Spec{
|
||||
return cty.NilType, errors.New("values argument must be a list or tuple value")
|
||||
}
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
keys := args[0]
|
||||
values := args[1]
|
||||
|
35
vendor/github.com/zclconf/go-cty/cty/function/stdlib/conversion.go
generated
vendored
35
vendor/github.com/zclconf/go-cty/cty/function/stdlib/conversion.go
generated
vendored
@ -1,6 +1,7 @@
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
@ -18,6 +19,7 @@ import (
|
||||
// a tuple.
|
||||
func MakeToFunc(wantTy cty.Type) function.Function {
|
||||
return function.New(&function.Spec{
|
||||
Description: fmt.Sprintf("Converts the given value to %s, or raises an error if that conversion is impossible.", wantTy.FriendlyName()),
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "v",
|
||||
@ -85,3 +87,36 @@ func MakeToFunc(wantTy cty.Type) function.Function {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// AssertNotNullFunc is a function which does nothing except return an error
|
||||
// if the argument given to it is null.
|
||||
//
|
||||
// This could be useful in some cases where the automatic refinment of
|
||||
// nullability isn't precise enough, because the result is guaranteed to not
|
||||
// be null and can therefore allow downstream comparisons to null to return
|
||||
// a known value even if the value is otherwise unknown.
|
||||
var AssertNotNullFunc = function.New(&function.Spec{
|
||||
Description: "Returns the given value varbatim if it is non-null, or raises an error if it's null.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "v",
|
||||
Type: cty.DynamicPseudoType,
|
||||
// NOTE: We intentionally don't set AllowNull here, and so
|
||||
// the function system will automatically reject a null argument
|
||||
// for us before calling Impl.
|
||||
},
|
||||
},
|
||||
Type: func(args []cty.Value) (cty.Type, error) {
|
||||
return args[0].Type(), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
// Our argument doesn't set AllowNull: true, so we're guaranteed to
|
||||
// have a non-null value in args[0].
|
||||
return args[0], nil
|
||||
},
|
||||
})
|
||||
|
||||
func AssertNotNull(v cty.Value) (cty.Value, error) {
|
||||
return AssertNotNullFunc.Call([]cty.Value{v})
|
||||
}
|
||||
|
2
vendor/github.com/zclconf/go-cty/cty/function/stdlib/csv.go
generated
vendored
2
vendor/github.com/zclconf/go-cty/cty/function/stdlib/csv.go
generated
vendored
@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var CSVDecodeFunc = function.New(&function.Spec{
|
||||
Description: `Parses the given string as Comma Separated Values (as defined by RFC 4180) and returns a map of objects representing the table of data, using the first row as a header row to define the object attributes.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
@ -42,6 +43,7 @@ var CSVDecodeFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.List(cty.Object(atys)), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
ety := retType.ElementType()
|
||||
atys := ety.AttributeTypes()
|
||||
|
135
vendor/github.com/zclconf/go-cty/cty/function/stdlib/datetime.go
generated
vendored
135
vendor/github.com/zclconf/go-cty/cty/function/stdlib/datetime.go
generated
vendored
@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
var FormatDateFunc = function.New(&function.Spec{
|
||||
Description: `Formats a timestamp given in RFC 3339 syntax into another timestamp in some other machine-oriented time syntax, as described in the format string.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "format",
|
||||
@ -22,7 +23,8 @@ var FormatDateFunc = function.New(&function.Spec{
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
formatStr := args[0].AsString()
|
||||
timeStr := args[1].AsString()
|
||||
@ -205,6 +207,7 @@ 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{
|
||||
Description: `Adds the duration represented by the given duration string to the given RFC 3339 timestamp string, returning another RFC 3339 timestamp.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "timestamp",
|
||||
@ -279,67 +282,6 @@ func FormatDate(format cty.Value, timestamp cty.Value) (cty.Value, error) {
|
||||
return FormatDateFunc.Call([]cty.Value{format, timestamp})
|
||||
}
|
||||
|
||||
func parseTimestamp(ts string) (time.Time, error) {
|
||||
t, err := time.Parse(time.RFC3339, ts)
|
||||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case *time.ParseError:
|
||||
// If err is s time.ParseError then its string representation is not
|
||||
// appropriate since it relies on details of Go's strange date format
|
||||
// representation, which a caller of our functions is not expected
|
||||
// to be familiar with.
|
||||
//
|
||||
// Therefore we do some light transformation to get a more suitable
|
||||
// error that should make more sense to our callers. These are
|
||||
// still not awesome error messages, but at least they refer to
|
||||
// the timestamp portions by name rather than by Go's example
|
||||
// values.
|
||||
if err.LayoutElem == "" && err.ValueElem == "" && err.Message != "" {
|
||||
// For some reason err.Message is populated with a ": " prefix
|
||||
// by the time package.
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp%s", err.Message)
|
||||
}
|
||||
var what string
|
||||
switch err.LayoutElem {
|
||||
case "2006":
|
||||
what = "year"
|
||||
case "01":
|
||||
what = "month"
|
||||
case "02":
|
||||
what = "day of month"
|
||||
case "15":
|
||||
what = "hour"
|
||||
case "04":
|
||||
what = "minute"
|
||||
case "05":
|
||||
what = "second"
|
||||
case "Z07:00":
|
||||
what = "UTC offset"
|
||||
case "T":
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: missing required time introducer 'T'")
|
||||
case ":", "-":
|
||||
if err.ValueElem == "" {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string where %q is expected", err.LayoutElem)
|
||||
} else {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: found %q where %q is expected", err.ValueElem, err.LayoutElem)
|
||||
}
|
||||
default:
|
||||
// Should never get here, because time.RFC3339 includes only the
|
||||
// above portions, but since that might change in future we'll
|
||||
// be robust here.
|
||||
what = "timestamp segment"
|
||||
}
|
||||
if err.ValueElem == "" {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string before %s", what)
|
||||
} else {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: cannot use %q as %s", err.ValueElem, what)
|
||||
}
|
||||
}
|
||||
return time.Time{}, err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// splitDataFormat is a bufio.SplitFunc used to tokenize a date format.
|
||||
func splitDateFormat(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
if len(data) == 0 {
|
||||
@ -416,6 +358,75 @@ func startsDateFormatVerb(b byte) bool {
|
||||
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
|
||||
}
|
||||
|
||||
func parseTimestamp(ts string) (time.Time, error) {
|
||||
t, err := parseStrictRFC3339(ts)
|
||||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case *time.ParseError:
|
||||
// If err is s time.ParseError then its string representation is not
|
||||
// appropriate since it relies on details of Go's strange date format
|
||||
// representation, which a caller of our functions is not expected
|
||||
// to be familiar with.
|
||||
//
|
||||
// Therefore we do some light transformation to get a more suitable
|
||||
// error that should make more sense to our callers. These are
|
||||
// still not awesome error messages, but at least they refer to
|
||||
// the timestamp portions by name rather than by Go's example
|
||||
// values.
|
||||
if err.LayoutElem == "" && err.ValueElem == "" && err.Message != "" {
|
||||
// For some reason err.Message is populated with a ": " prefix
|
||||
// by the time package.
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp%s", err.Message)
|
||||
}
|
||||
var what string
|
||||
switch err.LayoutElem {
|
||||
case "2006":
|
||||
what = "year"
|
||||
case "01":
|
||||
what = "month"
|
||||
case "02":
|
||||
what = "day of month"
|
||||
case "15":
|
||||
what = "hour"
|
||||
case "04":
|
||||
what = "minute"
|
||||
case "05":
|
||||
what = "second"
|
||||
case "Z07:00":
|
||||
what = "UTC offset"
|
||||
case "T":
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: missing required time introducer 'T'")
|
||||
case ":", "-":
|
||||
if err.ValueElem == "" {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string where %q is expected", err.LayoutElem)
|
||||
} else {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: found %q where %q is expected", err.ValueElem, err.LayoutElem)
|
||||
}
|
||||
default:
|
||||
// Should never get here, because RFC3339 includes only the
|
||||
// above portions.
|
||||
what = "timestamp segment"
|
||||
}
|
||||
if err.ValueElem == "" {
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string before %s", what)
|
||||
} else {
|
||||
switch {
|
||||
case what == "hour" && strings.Contains(err.ValueElem, ":"):
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: hour must be between 0 and 23 inclusive")
|
||||
case what == "hour" && len(err.ValueElem) != 2:
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: hour must have exactly two digits")
|
||||
case what == "minute" && len(err.ValueElem) != 2:
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: minute must have exactly two digits")
|
||||
default:
|
||||
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: cannot use %q as %s", err.ValueElem, what)
|
||||
}
|
||||
}
|
||||
}
|
||||
return time.Time{}, err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// TimeAdd adds a duration to a timestamp, returning a new timestamp.
|
||||
//
|
||||
// In the HCL language, timestamps are conventionally represented as
|
||||
|
219
vendor/github.com/zclconf/go-cty/cty/function/stdlib/datetime_rfc3339.go
generated
vendored
Normal file
219
vendor/github.com/zclconf/go-cty/cty/function/stdlib/datetime_rfc3339.go
generated
vendored
Normal file
@ -0,0 +1,219 @@
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// This file inlines some RFC3339 parsing code that was added to the Go standard
|
||||
// library's "time" package during the Go 1.20 development period but then
|
||||
// reverted prior to release to follow the Go proposals process first.
|
||||
//
|
||||
// Our goal is to support only valid RFC3339 strings regardless of what version
|
||||
// of Go is being used, because the Go stdlib is just an implementation detail
|
||||
// of the cty stdlib and so these functions should not very their behavior
|
||||
// significantly due to being compiled against a different Go version.
|
||||
//
|
||||
// These inline copies of the code from upstream should likely stay here
|
||||
// indefinitely even if functionality like this _is_ accepted in a later version
|
||||
// of Go, because this now defines cty's definition of RFC3339 parsing as
|
||||
// intentionally independent of Go's.
|
||||
|
||||
func parseStrictRFC3339(str string) (time.Time, error) {
|
||||
t, ok := parseRFC3339(str)
|
||||
if !ok {
|
||||
// If parsing failed then we'll try to use time.Parse to gather up a
|
||||
// helpful error object.
|
||||
_, err := time.Parse(time.RFC3339, str)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
// The parse template syntax cannot correctly validate RFC 3339.
|
||||
// Explicitly check for cases that Parse is unable to validate for.
|
||||
// See https://go.dev/issue/54580.
|
||||
num2 := func(str string) byte { return 10*(str[0]-'0') + (str[1] - '0') }
|
||||
switch {
|
||||
case str[len("2006-01-02T")+1] == ':': // hour must be two digits
|
||||
return time.Time{}, &time.ParseError{
|
||||
Layout: time.RFC3339,
|
||||
Value: str,
|
||||
LayoutElem: "15",
|
||||
ValueElem: str[len("2006-01-02T"):][:1],
|
||||
Message: ": hour must have two digits",
|
||||
}
|
||||
case str[len("2006-01-02T15:04:05")] == ',': // sub-second separator must be a period
|
||||
return time.Time{}, &time.ParseError{
|
||||
Layout: time.RFC3339,
|
||||
Value: str,
|
||||
LayoutElem: ".",
|
||||
ValueElem: ",",
|
||||
Message: ": sub-second separator must be a period",
|
||||
}
|
||||
case str[len(str)-1] != 'Z':
|
||||
switch {
|
||||
case num2(str[len(str)-len("07:00"):]) >= 24: // timezone hour must be in range
|
||||
return time.Time{}, &time.ParseError{
|
||||
Layout: time.RFC3339,
|
||||
Value: str,
|
||||
LayoutElem: "Z07:00",
|
||||
ValueElem: str[len(str)-len("Z07:00"):],
|
||||
Message: ": timezone hour out of range",
|
||||
}
|
||||
case num2(str[len(str)-len("00"):]) >= 60: // timezone minute must be in range
|
||||
return time.Time{}, &time.ParseError{
|
||||
Layout: time.RFC3339,
|
||||
Value: str,
|
||||
LayoutElem: "Z07:00",
|
||||
ValueElem: str[len(str)-len("Z07:00"):],
|
||||
Message: ": timezone minute out of range",
|
||||
}
|
||||
}
|
||||
default: // unknown error; should not occur
|
||||
return time.Time{}, &time.ParseError{
|
||||
Layout: time.RFC3339,
|
||||
Value: str,
|
||||
LayoutElem: time.RFC3339,
|
||||
ValueElem: str,
|
||||
Message: "",
|
||||
}
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func parseRFC3339(s string) (time.Time, bool) {
|
||||
// parseUint parses s as an unsigned decimal integer and
|
||||
// verifies that it is within some range.
|
||||
// If it is invalid or out-of-range,
|
||||
// it sets ok to false and returns the min value.
|
||||
ok := true
|
||||
parseUint := func(s string, min, max int) (x int) {
|
||||
for _, c := range []byte(s) {
|
||||
if c < '0' || '9' < c {
|
||||
ok = false
|
||||
return min
|
||||
}
|
||||
x = x*10 + int(c) - '0'
|
||||
}
|
||||
if x < min || max < x {
|
||||
ok = false
|
||||
return min
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// Parse the date and time.
|
||||
if len(s) < len("2006-01-02T15:04:05") {
|
||||
return time.Time{}, false
|
||||
}
|
||||
year := parseUint(s[0:4], 0, 9999) // e.g., 2006
|
||||
month := parseUint(s[5:7], 1, 12) // e.g., 01
|
||||
day := parseUint(s[8:10], 1, daysIn(time.Month(month), year)) // e.g., 02
|
||||
hour := parseUint(s[11:13], 0, 23) // e.g., 15
|
||||
min := parseUint(s[14:16], 0, 59) // e.g., 04
|
||||
sec := parseUint(s[17:19], 0, 59) // e.g., 05
|
||||
if !ok || !(s[4] == '-' && s[7] == '-' && s[10] == 'T' && s[13] == ':' && s[16] == ':') {
|
||||
return time.Time{}, false
|
||||
}
|
||||
s = s[19:]
|
||||
|
||||
// Parse the fractional second.
|
||||
var nsec int
|
||||
if len(s) >= 2 && s[0] == '.' && isDigit(s, 1) {
|
||||
n := 2
|
||||
for ; n < len(s) && isDigit(s, n); n++ {
|
||||
}
|
||||
nsec, _, _ = parseNanoseconds(s, n)
|
||||
s = s[n:]
|
||||
}
|
||||
|
||||
// Parse the time zone.
|
||||
loc := time.UTC
|
||||
if len(s) != 1 || s[0] != 'Z' {
|
||||
if len(s) != len("-07:00") {
|
||||
return time.Time{}, false
|
||||
}
|
||||
hr := parseUint(s[1:3], 0, 23) // e.g., 07
|
||||
mm := parseUint(s[4:6], 0, 59) // e.g., 00
|
||||
if !ok || !((s[0] == '-' || s[0] == '+') && s[3] == ':') {
|
||||
return time.Time{}, false
|
||||
}
|
||||
zoneOffsetSecs := (hr*60 + mm) * 60
|
||||
if s[0] == '-' {
|
||||
zoneOffsetSecs = -zoneOffsetSecs
|
||||
}
|
||||
loc = time.FixedZone("", zoneOffsetSecs)
|
||||
}
|
||||
t := time.Date(year, time.Month(month), day, hour, min, sec, nsec, loc)
|
||||
|
||||
return t, true
|
||||
}
|
||||
|
||||
func isDigit(s string, i int) bool {
|
||||
if len(s) <= i {
|
||||
return false
|
||||
}
|
||||
c := s[i]
|
||||
return '0' <= c && c <= '9'
|
||||
}
|
||||
|
||||
func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string, err error) {
|
||||
if value[0] != '.' && value[0] != ',' {
|
||||
err = errBadTimestamp
|
||||
return
|
||||
}
|
||||
if nbytes > 10 {
|
||||
value = value[:10]
|
||||
nbytes = 10
|
||||
}
|
||||
if ns, err = strconv.Atoi(value[1:nbytes]); err != nil {
|
||||
return
|
||||
}
|
||||
if ns < 0 {
|
||||
rangeErrString = "fractional second"
|
||||
return
|
||||
}
|
||||
// We need nanoseconds, which means scaling by the number
|
||||
// of missing digits in the format, maximum length 10.
|
||||
scaleDigits := 10 - nbytes
|
||||
for i := 0; i < scaleDigits; i++ {
|
||||
ns *= 10
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// These are internal errors used by the date parsing code and are not ever
|
||||
// returned by public functions.
|
||||
var errBadTimestamp = errors.New("bad value for field")
|
||||
|
||||
// daysBefore[m] counts the number of days in a non-leap year
|
||||
// before month m begins. There is an entry for m=12, counting
|
||||
// the number of days before January of next year (365).
|
||||
var daysBefore = [...]int32{
|
||||
0,
|
||||
31,
|
||||
31 + 28,
|
||||
31 + 28 + 31,
|
||||
31 + 28 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
|
||||
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
|
||||
}
|
||||
|
||||
func daysIn(m time.Month, year int) int {
|
||||
if m == time.February && isLeap(year) {
|
||||
return 29
|
||||
}
|
||||
return int(daysBefore[m] - daysBefore[m-1])
|
||||
}
|
||||
|
||||
func isLeap(year int) bool {
|
||||
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||
}
|
71
vendor/github.com/zclconf/go-cty/cty/function/stdlib/format.go
generated
vendored
71
vendor/github.com/zclconf/go-cty/cty/function/stdlib/format.go
generated
vendored
@ -18,33 +18,7 @@ import (
|
||||
//go:generate gofmt -w format_fsm.go
|
||||
|
||||
var FormatFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "format",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "args",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowNull: true,
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
for _, arg := range args[1:] {
|
||||
if !arg.IsWhollyKnown() {
|
||||
// We require all nested values to be known because the only
|
||||
// thing we can do for a collection/structural type is print
|
||||
// it as JSON and that requires it to be wholly known.
|
||||
return cty.UnknownVal(cty.String), nil
|
||||
}
|
||||
}
|
||||
str, err := formatFSM(args[0].AsString(), args[1:])
|
||||
return cty.StringVal(str), err
|
||||
},
|
||||
})
|
||||
|
||||
var FormatListFunc = function.New(&function.Spec{
|
||||
Description: `Constructs a string by applying formatting verbs to a series of arguments, using a similar syntax to the C function \"printf\".`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "format",
|
||||
@ -57,7 +31,46 @@ var FormatListFunc = function.New(&function.Spec{
|
||||
AllowNull: true,
|
||||
AllowUnknown: true,
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
for _, arg := range args[1:] {
|
||||
if !arg.IsWhollyKnown() {
|
||||
// We require all nested values to be known because the only
|
||||
// thing we can do for a collection/structural type is print
|
||||
// it as JSON and that requires it to be wholly known.
|
||||
// However, we might be able to refine the result with a
|
||||
// known prefix, if there are literal characters before the
|
||||
// first formatting verb.
|
||||
f := args[0].AsString()
|
||||
if idx := strings.IndexByte(f, '%'); idx > 0 {
|
||||
prefix := f[:idx]
|
||||
return cty.UnknownVal(cty.String).Refine().StringPrefix(prefix).NewValue(), nil
|
||||
}
|
||||
return cty.UnknownVal(cty.String), nil
|
||||
}
|
||||
}
|
||||
str, err := formatFSM(args[0].AsString(), args[1:])
|
||||
return cty.StringVal(str), err
|
||||
},
|
||||
})
|
||||
|
||||
var FormatListFunc = function.New(&function.Spec{
|
||||
Description: `Constructs a list of strings by applying formatting verbs to a series of arguments, using a similar syntax to the C function \"printf\".`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "format",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "args",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowNull: true,
|
||||
AllowUnknown: true,
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
fmtVal := args[0]
|
||||
args = args[1:]
|
||||
@ -162,7 +175,7 @@ var FormatListFunc = function.New(&function.Spec{
|
||||
// We require all nested values to be known because the only
|
||||
// thing we can do for a collection/structural type is print
|
||||
// it as JSON and that requires it to be wholly known.
|
||||
ret = append(ret, cty.UnknownVal(cty.String))
|
||||
ret = append(ret, cty.UnknownVal(cty.String).RefineNotNull())
|
||||
continue Results
|
||||
}
|
||||
}
|
||||
|
16
vendor/github.com/zclconf/go-cty/cty/function/stdlib/general.go
generated
vendored
16
vendor/github.com/zclconf/go-cty/cty/function/stdlib/general.go
generated
vendored
@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
var EqualFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if the two given values are equal, or false otherwise.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -25,13 +26,15 @@ var EqualFunc = function.New(&function.Spec{
|
||||
AllowNull: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].Equals(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var NotEqualFunc = function.New(&function.Spec{
|
||||
Description: `Returns false if the two given values are equal, or true otherwise.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -48,14 +51,16 @@ var NotEqualFunc = function.New(&function.Spec{
|
||||
AllowNull: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].Equals(args[1]).Not(), nil
|
||||
},
|
||||
})
|
||||
|
||||
var CoalesceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Returns the first of the given arguments that isn't null, or raises an error if there are no non-null arguments.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "vals",
|
||||
Type: cty.DynamicPseudoType,
|
||||
@ -74,6 +79,7 @@ var CoalesceFunc = function.New(&function.Spec{
|
||||
}
|
||||
return retType, nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
for _, argVal := range args {
|
||||
if !argVal.IsKnown() {
|
||||
@ -89,6 +95,10 @@ var CoalesceFunc = function.New(&function.Spec{
|
||||
},
|
||||
})
|
||||
|
||||
func refineNonNull(b *cty.RefinementBuilder) *cty.RefinementBuilder {
|
||||
return b.NotNull()
|
||||
}
|
||||
|
||||
// Equal determines whether the two given values are equal, returning a
|
||||
// bool value.
|
||||
func Equal(a cty.Value, b cty.Value) (cty.Value, error) {
|
||||
|
75
vendor/github.com/zclconf/go-cty/cty/function/stdlib/json.go
generated
vendored
75
vendor/github.com/zclconf/go-cty/cty/function/stdlib/json.go
generated
vendored
@ -1,28 +1,55 @@
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
"github.com/zclconf/go-cty/cty/json"
|
||||
)
|
||||
|
||||
var JSONEncodeFunc = function.New(&function.Spec{
|
||||
Description: `Returns a string containing a JSON representation of the given value.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "val",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowNull: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
val := args[0]
|
||||
if !val.IsWhollyKnown() {
|
||||
// We can't serialize unknowns, so if the value is unknown or
|
||||
// contains any _nested_ unknowns then our result must be
|
||||
// unknown.
|
||||
return cty.UnknownVal(retType), nil
|
||||
// unknown. However, we might still be able to at least constrain
|
||||
// the prefix of our string so that downstreams can sniff for
|
||||
// whether it's valid JSON and what result types it could have.
|
||||
|
||||
valRng := val.Range()
|
||||
if valRng.CouldBeNull() {
|
||||
// If null is possible then we can't constrain the result
|
||||
// beyond the type constraint, because the very first character
|
||||
// of the string is what distinguishes a null.
|
||||
return cty.UnknownVal(retType), nil
|
||||
}
|
||||
b := cty.UnknownVal(retType).Refine()
|
||||
ty := valRng.TypeConstraint()
|
||||
switch {
|
||||
case ty == cty.String:
|
||||
b = b.StringPrefixFull(`"`)
|
||||
case ty.IsObjectType() || ty.IsMapType():
|
||||
b = b.StringPrefixFull("{")
|
||||
case ty.IsTupleType() || ty.IsListType() || ty.IsSetType():
|
||||
b = b.StringPrefixFull("[")
|
||||
}
|
||||
return b.NewValue(), nil
|
||||
}
|
||||
|
||||
if val.IsNull() {
|
||||
@ -34,11 +61,17 @@ var JSONEncodeFunc = function.New(&function.Spec{
|
||||
return cty.NilVal, err
|
||||
}
|
||||
|
||||
// json.Marshal should already produce a trimmed string, but we'll
|
||||
// make sure it always is because our unknown value refinements above
|
||||
// assume there will be no leading whitespace before the value.
|
||||
buf = bytes.TrimSpace(buf)
|
||||
|
||||
return cty.StringVal(string(buf)), nil
|
||||
},
|
||||
})
|
||||
|
||||
var JSONDecodeFunc = function.New(&function.Spec{
|
||||
Description: `Parses the given string as JSON and returns a value corresponding to what the JSON document describes.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
@ -48,6 +81,42 @@ var JSONDecodeFunc = function.New(&function.Spec{
|
||||
Type: func(args []cty.Value) (cty.Type, error) {
|
||||
str := args[0]
|
||||
if !str.IsKnown() {
|
||||
// If the string isn't known then we can't fully parse it, but
|
||||
// if the value has been refined with a prefix then we may at
|
||||
// least be able to reject obviously-invalid syntax and maybe
|
||||
// even predict the result type. It's safe to return a specific
|
||||
// result type only if parsing a full document with this prefix
|
||||
// would return exactly that type or fail with a syntax error.
|
||||
rng := str.Range()
|
||||
if prefix := strings.TrimSpace(rng.StringPrefix()); prefix != "" {
|
||||
// If we know at least one character then it should be one
|
||||
// of the few characters that can introduce a JSON value.
|
||||
switch r, _ := utf8.DecodeRuneInString(prefix); r {
|
||||
case '{', '[':
|
||||
// These can start object values and array values
|
||||
// respectively, but we can't actually form a full
|
||||
// object type constraint or tuple type constraint
|
||||
// without knowing all of the attributes, so we
|
||||
// will still return DynamicPseudoType in this case.
|
||||
case '"':
|
||||
// This means that the result will either be a string
|
||||
// or parsing will fail.
|
||||
return cty.String, nil
|
||||
case 't', 'f':
|
||||
// Must either be a boolean value or a syntax error.
|
||||
return cty.Bool, nil
|
||||
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
|
||||
// These characters would all start the "number" production.
|
||||
return cty.Number, nil
|
||||
case 'n':
|
||||
// n is valid to begin the keyword "null" but that doesn't
|
||||
// give us any extra type information.
|
||||
default:
|
||||
// No other characters are valid as the beginning of a
|
||||
// JSON value, so we can safely return an early error.
|
||||
return cty.NilType, function.NewArgErrorf(0, "a JSON document cannot begin with the character %q", r)
|
||||
}
|
||||
}
|
||||
return cty.DynamicPseudoType, nil
|
||||
}
|
||||
|
||||
|
90
vendor/github.com/zclconf/go-cty/cty/function/stdlib/number.go
generated
vendored
90
vendor/github.com/zclconf/go-cty/cty/function/stdlib/number.go
generated
vendored
@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var AbsoluteFunc = function.New(&function.Spec{
|
||||
Description: `If the given number is negative then returns its positive equivalent, or otherwise returns the given number unchanged.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
@ -19,13 +20,15 @@ var AbsoluteFunc = function.New(&function.Spec{
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
return args[0].Absolute(), nil
|
||||
},
|
||||
})
|
||||
|
||||
var AddFunc = function.New(&function.Spec{
|
||||
Description: `Returns the sum of the two given numbers.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -38,7 +41,8 @@ var AddFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
// big.Float.Add can panic if the input values are opposing infinities,
|
||||
// so we must catch that here in order to remain within
|
||||
@ -59,6 +63,7 @@ var AddFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var SubtractFunc = function.New(&function.Spec{
|
||||
Description: `Returns the difference between the two given numbers.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -71,7 +76,8 @@ var SubtractFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
// big.Float.Sub can panic if the input values are infinities,
|
||||
// so we must catch that here in order to remain within
|
||||
@ -92,6 +98,7 @@ var SubtractFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var MultiplyFunc = function.New(&function.Spec{
|
||||
Description: `Returns the product of the two given numbers.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -104,7 +111,8 @@ var MultiplyFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
// big.Float.Mul can panic if the input values are both zero or both
|
||||
// infinity, so we must catch that here in order to remain within
|
||||
@ -126,6 +134,7 @@ var MultiplyFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var DivideFunc = function.New(&function.Spec{
|
||||
Description: `Divides the first given number by the second.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -138,7 +147,8 @@ var DivideFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
// big.Float.Quo can panic if the input values are both zero or both
|
||||
// infinity, so we must catch that here in order to remain within
|
||||
@ -160,6 +170,7 @@ var DivideFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var ModuloFunc = function.New(&function.Spec{
|
||||
Description: `Divides the first given number by the second and then returns the remainder.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -172,7 +183,8 @@ var ModuloFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
// big.Float.Mul can panic if the input values are both zero or both
|
||||
// infinity, so we must catch that here in order to remain within
|
||||
@ -194,90 +206,107 @@ var ModuloFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var GreaterThanFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if and only if the second number is greater than the first.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
{
|
||||
Name: "b",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].GreaterThan(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var GreaterThanOrEqualToFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if and only if the second number is greater than or equal to the first.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
{
|
||||
Name: "b",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].GreaterThanOrEqualTo(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var LessThanFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if and only if the second number is less than the first.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
{
|
||||
Name: "b",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].LessThan(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var LessThanOrEqualToFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if and only if the second number is less than or equal to the first.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
{
|
||||
Name: "b",
|
||||
Type: cty.Number,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].LessThanOrEqualTo(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var NegateFunc = function.New(&function.Spec{
|
||||
Description: `Multiplies the given number by -1.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
@ -286,20 +315,23 @@ var NegateFunc = function.New(&function.Spec{
|
||||
AllowMarked: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
return args[0].Negate(), nil
|
||||
},
|
||||
})
|
||||
|
||||
var MinFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Returns the numerically smallest of all of the given numbers.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "numbers",
|
||||
Type: cty.Number,
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
if len(args) == 0 {
|
||||
return cty.NilVal, fmt.Errorf("must pass at least one number")
|
||||
@ -317,13 +349,15 @@ var MinFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var MaxFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Returns the numerically greatest of all of the given numbers.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "numbers",
|
||||
Type: cty.Number,
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
if len(args) == 0 {
|
||||
return cty.NilVal, fmt.Errorf("must pass at least one number")
|
||||
@ -341,6 +375,7 @@ var MaxFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var IntFunc = function.New(&function.Spec{
|
||||
Description: `Discards any fractional portion of the given number.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
@ -348,7 +383,8 @@ var IntFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
bf := args[0].AsBigFloat()
|
||||
if bf.IsInt() {
|
||||
@ -363,13 +399,15 @@ 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{
|
||||
Description: `Returns the smallest whole number that is greater than or equal to the given value.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
f := args[0].AsBigFloat()
|
||||
|
||||
@ -392,13 +430,15 @@ var CeilFunc = function.New(&function.Spec{
|
||||
// FloorFunc is a function that returns the closest whole number lesser
|
||||
// than or equal to the given value.
|
||||
var FloorFunc = function.New(&function.Spec{
|
||||
Description: `Returns the greatest whole number that is less than or equal to the given value.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
f := args[0].AsBigFloat()
|
||||
|
||||
@ -420,6 +460,7 @@ var FloorFunc = function.New(&function.Spec{
|
||||
|
||||
// LogFunc is a function that returns the logarithm of a given number in a given base.
|
||||
var LogFunc = function.New(&function.Spec{
|
||||
Description: `Returns the logarithm of the given number in the given base.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
@ -430,7 +471,8 @@ var LogFunc = function.New(&function.Spec{
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
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 {
|
||||
@ -448,6 +490,7 @@ var LogFunc = function.New(&function.Spec{
|
||||
|
||||
// PowFunc is a function that returns the logarithm of a given number in a given base.
|
||||
var PowFunc = function.New(&function.Spec{
|
||||
Description: `Returns the given number raised to the given power (exponentiation).`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
@ -458,7 +501,8 @@ var PowFunc = function.New(&function.Spec{
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
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 {
|
||||
@ -477,13 +521,15 @@ var PowFunc = function.New(&function.Spec{
|
||||
// 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{
|
||||
Description: `Returns 0 if the given number is zero, 1 if the given number is positive, or -1 if the given number is negative.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "num",
|
||||
Type: cty.Number,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: refineNonNull,
|
||||
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 {
|
||||
@ -502,6 +548,7 @@ var SignumFunc = function.New(&function.Spec{
|
||||
|
||||
// ParseIntFunc is a function that parses a string argument and returns an integer of the specified base.
|
||||
var ParseIntFunc = function.New(&function.Spec{
|
||||
Description: `Parses the given string as a number of the given base, or raises an error if the string contains invalid characters.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "number",
|
||||
@ -519,6 +566,7 @@ var ParseIntFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Number, nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
var numstr string
|
||||
|
4
vendor/github.com/zclconf/go-cty/cty/function/stdlib/regexp.go
generated
vendored
4
vendor/github.com/zclconf/go-cty/cty/function/stdlib/regexp.go
generated
vendored
@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
var RegexFunc = function.New(&function.Spec{
|
||||
Description: `Applies the given regular expression pattern to the given string and returns information about a single match, or raises an error if there is no match.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "pattern",
|
||||
@ -32,6 +33,7 @@ var RegexFunc = function.New(&function.Spec{
|
||||
}
|
||||
return retTy, err
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
if retType == cty.DynamicPseudoType {
|
||||
return cty.DynamicVal, nil
|
||||
@ -54,6 +56,7 @@ var RegexFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var RegexAllFunc = function.New(&function.Spec{
|
||||
Description: `Applies the given regular expression pattern to the given string and returns a list of information about all non-overlapping matches, or an empty list if there are no matches.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "pattern",
|
||||
@ -77,6 +80,7 @@ var RegexAllFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.List(retTy), err
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
ety := retType.ElementType()
|
||||
if ety == cty.DynamicPseudoType {
|
||||
|
8
vendor/github.com/zclconf/go-cty/cty/function/stdlib/sequence.go
generated
vendored
8
vendor/github.com/zclconf/go-cty/cty/function/stdlib/sequence.go
generated
vendored
@ -9,7 +9,8 @@ import (
|
||||
)
|
||||
|
||||
var ConcatFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
Description: `Concatenates together all of the given lists or tuples into a single sequence, preserving the input order.`,
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "seqs",
|
||||
Type: cty.DynamicPseudoType,
|
||||
@ -73,6 +74,7 @@ var ConcatFunc = function.New(&function.Spec{
|
||||
}
|
||||
return cty.Tuple(etys), nil
|
||||
},
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
switch {
|
||||
case retType.IsListType():
|
||||
@ -137,11 +139,13 @@ var ConcatFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var RangeFunc = function.New(&function.Spec{
|
||||
Description: `Returns a list of numbers spread evenly over a particular range.`,
|
||||
VarParam: &function.Parameter{
|
||||
Name: "params",
|
||||
Type: cty.Number,
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.Number)),
|
||||
Type: function.StaticReturnType(cty.List(cty.Number)),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
var start, end, step cty.Value
|
||||
switch len(args) {
|
||||
|
20
vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go
generated
vendored
20
vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go
generated
vendored
@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
var SetHasElementFunc = function.New(&function.Spec{
|
||||
Description: `Returns true if the given set contains the given element, or false otherwise.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "set",
|
||||
@ -22,13 +23,15 @@ var SetHasElementFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return args[0].HasElement(args[1]), nil
|
||||
},
|
||||
})
|
||||
|
||||
var SetUnionFunc = function.New(&function.Spec{
|
||||
Description: `Returns the union of all given sets.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "first_set",
|
||||
@ -41,13 +44,15 @@ var SetUnionFunc = function.New(&function.Spec{
|
||||
Type: cty.Set(cty.DynamicPseudoType),
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
Type: setOperationReturnType,
|
||||
Type: setOperationReturnType,
|
||||
RefineResult: refineNonNull,
|
||||
Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
|
||||
return s1.Union(s2)
|
||||
}, true),
|
||||
})
|
||||
|
||||
var SetIntersectionFunc = function.New(&function.Spec{
|
||||
Description: `Returns the intersection of all given sets.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "first_set",
|
||||
@ -60,13 +65,15 @@ var SetIntersectionFunc = function.New(&function.Spec{
|
||||
Type: cty.Set(cty.DynamicPseudoType),
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
Type: setOperationReturnType,
|
||||
Type: setOperationReturnType,
|
||||
RefineResult: refineNonNull,
|
||||
Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
|
||||
return s1.Intersection(s2)
|
||||
}, false),
|
||||
})
|
||||
|
||||
var SetSubtractFunc = function.New(&function.Spec{
|
||||
Description: `Returns the relative complement of the two given sets.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "a",
|
||||
@ -79,13 +86,15 @@ var SetSubtractFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: setOperationReturnType,
|
||||
Type: setOperationReturnType,
|
||||
RefineResult: refineNonNull,
|
||||
Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
|
||||
return s1.Subtract(s2)
|
||||
}, false),
|
||||
})
|
||||
|
||||
var SetSymmetricDifferenceFunc = function.New(&function.Spec{
|
||||
Description: `Returns the symmetric difference of the two given sets.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "first_set",
|
||||
@ -98,7 +107,8 @@ var SetSymmetricDifferenceFunc = function.New(&function.Spec{
|
||||
Type: cty.Set(cty.DynamicPseudoType),
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
Type: setOperationReturnType,
|
||||
Type: setOperationReturnType,
|
||||
RefineResult: refineNonNull,
|
||||
Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
|
||||
return s1.SymmetricDifference(s2)
|
||||
}, false),
|
||||
|
178
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string.go
generated
vendored
178
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string.go
generated
vendored
@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
var UpperFunc = function.New(&function.Spec{
|
||||
Description: "Returns the given string with all Unicode letters translated to their uppercase equivalents.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
@ -21,7 +22,8 @@ var UpperFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
in := args[0].AsString()
|
||||
out := strings.ToUpper(in)
|
||||
@ -30,6 +32,7 @@ var UpperFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var LowerFunc = function.New(&function.Spec{
|
||||
Description: "Returns the given string with all Unicode letters translated to their lowercase equivalents.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
@ -37,7 +40,8 @@ var LowerFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
in := args[0].AsString()
|
||||
out := strings.ToLower(in)
|
||||
@ -46,6 +50,7 @@ var LowerFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var ReverseFunc = function.New(&function.Spec{
|
||||
Description: "Returns the given string with all of its Unicode characters in reverse order.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
@ -53,7 +58,8 @@ var ReverseFunc = function.New(&function.Spec{
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
in := []byte(args[0].AsString())
|
||||
out := make([]byte, len(in))
|
||||
@ -73,48 +79,75 @@ var ReverseFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var StrlenFunc = function.New(&function.Spec{
|
||||
Description: "Returns the number of Unicode characters (technically: grapheme clusters) in the given string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Number),
|
||||
RefineResult: func(b *cty.RefinementBuilder) *cty.RefinementBuilder {
|
||||
// String length is never null and never negative.
|
||||
// (We might refine the lower bound even more inside Impl.)
|
||||
return b.NotNull().NumberRangeLowerBound(cty.NumberIntVal(0), true)
|
||||
},
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
in := args[0].AsString()
|
||||
l := 0
|
||||
|
||||
inB := []byte(in)
|
||||
for i := 0; i < len(in); {
|
||||
d, _, _ := textseg.ScanGraphemeClusters(inB[i:], true)
|
||||
l++
|
||||
i += d
|
||||
if !args[0].IsKnown() {
|
||||
ret := cty.UnknownVal(cty.Number)
|
||||
// We may be able to still return a constrained result based on the
|
||||
// refined range of the unknown value.
|
||||
inRng := args[0].Range()
|
||||
if inRng.TypeConstraint() == cty.String {
|
||||
prefixLen := int64(graphemeClusterCount(inRng.StringPrefix()))
|
||||
ret = ret.Refine().NumberRangeLowerBound(cty.NumberIntVal(prefixLen), true).NewValue()
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
in := args[0].AsString()
|
||||
l := graphemeClusterCount(in)
|
||||
return cty.NumberIntVal(int64(l)), nil
|
||||
},
|
||||
})
|
||||
|
||||
func graphemeClusterCount(in string) int {
|
||||
l := 0
|
||||
inB := []byte(in)
|
||||
for i := 0; i < len(in); {
|
||||
d, _, _ := textseg.ScanGraphemeClusters(inB[i:], true)
|
||||
l++
|
||||
i += d
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
var SubstrFunc = function.New(&function.Spec{
|
||||
Description: "Extracts a substring from the given string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Description: "The input string.",
|
||||
Type: cty.String,
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
{
|
||||
Name: "offset",
|
||||
Description: "The starting offset in Unicode characters.",
|
||||
Type: cty.Number,
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
{
|
||||
Name: "length",
|
||||
Description: "The maximum length of the result in Unicode characters.",
|
||||
Type: cty.Number,
|
||||
AllowDynamicType: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
in := []byte(args[0].AsString())
|
||||
var offset, length int
|
||||
@ -197,17 +230,21 @@ var SubstrFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var JoinFunc = function.New(&function.Spec{
|
||||
Description: "Concatenates together the elements of all given lists with a delimiter, producing a single string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "separator",
|
||||
Type: cty.String,
|
||||
Name: "separator",
|
||||
Description: "Delimiter to insert between the given strings.",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "lists",
|
||||
Type: cty.List(cty.String),
|
||||
Name: "lists",
|
||||
Description: "One or more lists of strings to join.",
|
||||
Type: cty.List(cty.String),
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
sep := args[0].AsString()
|
||||
listVals := args[1:]
|
||||
@ -244,20 +281,32 @@ var JoinFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var SortFunc = function.New(&function.Spec{
|
||||
Description: "Applies a lexicographic sort to the elements of the given list.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
Type: cty.List(cty.String),
|
||||
Name: "list",
|
||||
Type: cty.List(cty.String),
|
||||
AllowUnknown: true,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
RefineResult: refineNonNull,
|
||||
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
|
||||
// can't yet predict the order of the result, but we can be
|
||||
// sure that the length won't change.
|
||||
ret := cty.UnknownVal(retType)
|
||||
if listVal.Type().IsListType() {
|
||||
rng := listVal.Range()
|
||||
ret = ret.Refine().
|
||||
CollectionLengthLowerBound(rng.LengthLowerBound()).
|
||||
CollectionLengthUpperBound(rng.LengthUpperBound()).
|
||||
NewValue()
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
if listVal.LengthInt() == 0 { // Easy path
|
||||
return listVal, nil
|
||||
@ -282,17 +331,21 @@ var SortFunc = function.New(&function.Spec{
|
||||
})
|
||||
|
||||
var SplitFunc = function.New(&function.Spec{
|
||||
Description: "Produces a list of one or more strings by splitting the given string at all instances of a given separator substring.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "separator",
|
||||
Type: cty.String,
|
||||
Name: "separator",
|
||||
Description: "The substring that delimits the result strings.",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
Name: "str",
|
||||
Description: "The string to split.",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
Type: function.StaticReturnType(cty.List(cty.String)),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
sep := args[0].AsString()
|
||||
str := args[1].AsString()
|
||||
@ -311,13 +364,15 @@ var SplitFunc = function.New(&function.Spec{
|
||||
// ChompFunc is a function that removes newline characters at the end of a
|
||||
// string.
|
||||
var ChompFunc = function.New(&function.Spec{
|
||||
Description: "Removes one or more newline characters from the end of the given string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
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
|
||||
@ -327,17 +382,21 @@ var ChompFunc = function.New(&function.Spec{
|
||||
// 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{
|
||||
Description: "Adds a given number of spaces after each newline character in the given string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "spaces",
|
||||
Type: cty.Number,
|
||||
Name: "spaces",
|
||||
Description: "Number of spaces to add after each newline character.",
|
||||
Type: cty.Number,
|
||||
},
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
Name: "str",
|
||||
Description: "The string to transform.",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
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 {
|
||||
@ -352,13 +411,15 @@ var IndentFunc = function.New(&function.Spec{
|
||||
// TitleFunc is a function that converts the first letter of each word in the
|
||||
// given string to uppercase.
|
||||
var TitleFunc = function.New(&function.Spec{
|
||||
Description: "Replaces one letter after each non-letter and non-digit character with its uppercase equivalent.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return cty.StringVal(strings.Title(args[0].AsString())), nil
|
||||
},
|
||||
@ -367,13 +428,15 @@ var TitleFunc = function.New(&function.Spec{
|
||||
// TrimSpaceFunc is a function that removes any space characters from the start
|
||||
// and end of the given string.
|
||||
var TrimSpaceFunc = function.New(&function.Spec{
|
||||
Description: "Removes any consecutive space characters (as defined by Unicode) from the start and end of the given string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
return cty.StringVal(strings.TrimSpace(args[0].AsString())), nil
|
||||
},
|
||||
@ -382,20 +445,27 @@ var TrimSpaceFunc = function.New(&function.Spec{
|
||||
// TrimFunc is a function that removes the specified characters from the start
|
||||
// and end of the given string.
|
||||
var TrimFunc = function.New(&function.Spec{
|
||||
Description: "Removes consecutive sequences of characters in \"cutset\" from the start and end of the given string.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
Name: "str",
|
||||
Description: "The string to trim.",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "cutset",
|
||||
Type: cty.String,
|
||||
Name: "cutset",
|
||||
Description: "A string containing all of the characters to trim. Each character is taken separately, so the order of characters is insignificant.",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
// NOTE: This doesn't properly handle any character that is encoded
|
||||
// with multiple sequential code units, such as letters with
|
||||
// combining diacritics and emoji modifier sequences.
|
||||
return cty.StringVal(strings.Trim(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
@ -403,17 +473,21 @@ var TrimFunc = function.New(&function.Spec{
|
||||
// TrimPrefixFunc is a function that removes the specified characters from the
|
||||
// start the given string.
|
||||
var TrimPrefixFunc = function.New(&function.Spec{
|
||||
Description: "Removes the given prefix from the start of the given string, if present.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
Name: "str",
|
||||
Description: "The string to trim.",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "prefix",
|
||||
Type: cty.String,
|
||||
Name: "prefix",
|
||||
Description: "The prefix to remove, if present.",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
prefix := args[1].AsString()
|
||||
@ -424,17 +498,21 @@ var TrimPrefixFunc = function.New(&function.Spec{
|
||||
// TrimSuffixFunc is a function that removes the specified characters from the
|
||||
// end of the given string.
|
||||
var TrimSuffixFunc = function.New(&function.Spec{
|
||||
Description: "Removes the given suffix from the start of the given string, if present.",
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
Name: "str",
|
||||
Description: "The string to trim.",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "suffix",
|
||||
Type: cty.String,
|
||||
Name: "suffix",
|
||||
Description: "The suffix to remove, if present.",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
|
25
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string_replace.go
generated
vendored
25
vendor/github.com/zclconf/go-cty/cty/function/stdlib/string_replace.go
generated
vendored
@ -12,21 +12,26 @@ import (
|
||||
// substring, and replaces each occurence with a given replacement string.
|
||||
// The substr argument is a simple string.
|
||||
var ReplaceFunc = function.New(&function.Spec{
|
||||
Description: `Replaces all instances of the given substring in the given string with the given replacement string.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
Name: "str",
|
||||
Description: `The string to search within.`,
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "substr",
|
||||
Type: cty.String,
|
||||
Name: "substr",
|
||||
Description: `The substring to search for.`,
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "replace",
|
||||
Type: cty.String,
|
||||
Name: "replace",
|
||||
Description: `The new substring to replace substr with.`,
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
substr := args[1].AsString()
|
||||
@ -40,13 +45,14 @@ var ReplaceFunc = function.New(&function.Spec{
|
||||
// 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{
|
||||
Description: `Applies the given regular expression pattern to the given string and replaces all matches with the given replacement string.`,
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "substr",
|
||||
Name: "pattern",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
@ -54,7 +60,8 @@ var RegexReplaceFunc = function.New(&function.Spec{
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
RefineResult: refineNonNull,
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
str := args[0].AsString()
|
||||
substr := args[1].AsString()
|
||||
|
Reference in New Issue
Block a user