update go-cty

This commit is contained in:
James Bardin 2019-05-16 16:43:28 -04:00
parent c391f3a1a3
commit 1d134fea69
5 changed files with 93 additions and 5 deletions

2
go.mod
View File

@ -107,7 +107,7 @@ require (
github.com/xanzy/ssh-agent v0.2.1
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18 // indirect
github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557
github.com/zclconf/go-cty v0.0.0-20190430221426-d36a6f0dbffd
github.com/zclconf/go-cty v0.0.0-20190516203816-4fecf87372ec
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1 // indirect

4
go.sum
View File

@ -403,8 +403,8 @@ github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557/go.mod h1:ce1O1j6Ut
github.com/zclconf/go-cty v0.0.0-20181129180422-88fbe721e0f8/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20190426224007-b18a157db9e2 h1:Ai1LhlYNEqE39zGU07qHDNJ41iZVPZfZr1dSCoXrp1w=
github.com/zclconf/go-cty v0.0.0-20190426224007-b18a157db9e2/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20190430221426-d36a6f0dbffd h1:NZOOU7h+pDtcKo6xlqm8PwnarS8nJ+6+I83jT8ZfLPI=
github.com/zclconf/go-cty v0.0.0-20190430221426-d36a6f0dbffd/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20190516203816-4fecf87372ec h1:MSeYjmyjucsFbecMTxg63ASg23lcSARP/kr9sClTFfk=
github.com/zclconf/go-cty v0.0.0-20190516203816-4fecf87372ec/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
go.opencensus.io v0.18.0 h1:Mk5rgZcggtbvtAun5aJzAtjKKN/t0R3jJPlWILlv938=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=

View File

@ -138,7 +138,7 @@ func conversionTupleToSet(tupleType cty.Type, listEty cty.Type, unsafe bool) con
if len(tupleEtys) == 0 {
// Empty tuple short-circuit
return func(val cty.Value, path cty.Path) (cty.Value, error) {
return cty.ListValEmpty(listEty), nil
return cty.SetValEmpty(listEty), nil
}
}

View File

@ -119,6 +119,75 @@ var ConcatFunc = function.New(&function.Spec{
},
})
var RangeFunc = function.New(&function.Spec{
VarParam: &function.Parameter{
Name: "params",
Type: cty.Number,
},
Type: function.StaticReturnType(cty.List(cty.Number)),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var start, end, step cty.Value
switch len(args) {
case 1:
if args[0].LessThan(cty.Zero).True() {
start, end, step = cty.Zero, args[0], cty.NumberIntVal(-1)
} else {
start, end, step = cty.Zero, args[0], cty.NumberIntVal(1)
}
case 2:
if args[1].LessThan(args[0]).True() {
start, end, step = args[0], args[1], cty.NumberIntVal(-1)
} else {
start, end, step = args[0], args[1], cty.NumberIntVal(1)
}
case 3:
start, end, step = args[0], args[1], args[2]
default:
return cty.NilVal, fmt.Errorf("must have one, two, or three arguments")
}
var vals []cty.Value
if step == cty.Zero {
return cty.NilVal, function.NewArgErrorf(2, "step must not be zero")
}
down := step.LessThan(cty.Zero).True()
if down {
if end.GreaterThan(start).True() {
return cty.NilVal, function.NewArgErrorf(1, "end must be less than start when step is negative")
}
} else {
if end.LessThan(start).True() {
return cty.NilVal, function.NewArgErrorf(1, "end must be greater than start when step is positive")
}
}
num := start
for {
if down {
if num.LessThanOrEqualTo(end).True() {
break
}
} else {
if num.GreaterThanOrEqualTo(end).True() {
break
}
}
if len(vals) >= 1024 {
// Artificial limit to prevent bad arguments from consuming huge amounts of memory
return cty.NilVal, fmt.Errorf("more than 1024 values were generated; either decrease the difference between start and end or use a smaller step")
}
vals = append(vals, num)
num = num.Add(step)
}
if len(vals) == 0 {
return cty.ListValEmpty(cty.Number), nil
}
return cty.ListVal(vals), nil
},
})
// Concat takes one or more sequences (lists or tuples) and returns the single
// sequence that results from concatenating them together in order.
//
@ -128,3 +197,22 @@ var ConcatFunc = function.New(&function.Spec{
func Concat(seqs ...cty.Value) (cty.Value, error) {
return ConcatFunc.Call(seqs)
}
// Range creates a list of numbers by starting from the given starting value,
// then adding the given step value until the result is greater than or
// equal to the given stopping value. Each intermediate result becomes an
// element in the resulting list.
//
// When all three parameters are set, the order is (start, end, step). If
// only two parameters are set, they are the start and end respectively and
// step defaults to 1. If only one argument is set, it gives the end value
// with start defaulting to 0 and step defaulting to 1.
//
// Because the resulting list must be fully buffered in memory, there is an
// artificial cap of 1024 elements, after which this function will return
// an error to avoid consuming unbounded amounts of memory. The Range function
// is primarily intended for creating small lists of indices to iterate over,
// so there should be no reason to generate huge lists with it.
func Range(params ...cty.Value) (cty.Value, error) {
return RangeFunc.Call(params)
}

2
vendor/modules.txt vendored
View File

@ -434,7 +434,7 @@ github.com/vmihailenco/msgpack/codes
github.com/xanzy/ssh-agent
# github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557
github.com/xlab/treeprint
# github.com/zclconf/go-cty v0.0.0-20190430221426-d36a6f0dbffd
# github.com/zclconf/go-cty v0.0.0-20190516203816-4fecf87372ec
github.com/zclconf/go-cty/cty
github.com/zclconf/go-cty/cty/gocty
github.com/zclconf/go-cty/cty/convert