port distinct and chunklist functions

This commit is contained in:
Kristin Laemmert 2018-05-25 16:15:50 -07:00 committed by Martin Atkins
parent 8aac7587f7
commit 0cbcd75ebb
3 changed files with 295 additions and 3 deletions

View File

@ -202,7 +202,6 @@ var ContainsFunc = function.New(&function.Spec{
_, err = Index(args[0], args[1])
if err != nil {
fmt.Println(err)
return cty.False, nil
}
@ -246,6 +245,103 @@ var IndexFunc = function.New(&function.Spec{
},
})
// DistinctFunc contructs a function that takes a list and returns a new list
// with any duplicate elements removed.
var DistinctFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "list",
Type: cty.List(cty.DynamicPseudoType),
},
},
Type: function.StaticReturnType(cty.List(cty.DynamicPseudoType)),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
if len(args) != 1 {
return cty.NilVal, fmt.Errorf("distinct accepts only one argument")
}
var list []cty.Value
for it := args[0].ElementIterator(); it.Next(); {
_, v := it.Element()
list, err = appendIfMissing(list, v)
if err != nil {
return cty.NilVal, err
}
}
return cty.ListVal(list), nil
},
})
// ChunklistFunc contructs a function that splits a single list into fixed-size chunks,
// returning a list of lists.
var ChunklistFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "list",
Type: cty.List(cty.DynamicPseudoType),
},
{
Name: "size",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.List(cty.DynamicPseudoType)),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var size int
err = gocty.FromCtyValue(args[1], &size)
if err != nil {
return cty.NilVal, fmt.Errorf("invalid index: %s", err)
}
if size < 0 {
return cty.NilVal, fmt.Errorf("The size argument must be positive")
}
output := make([]cty.Value, 0)
// if size is 0, returns a list made of the initial list
if size == 0 {
output = append(output, args[0])
return cty.ListVal(output), nil
}
chunk := make([]cty.Value, 0)
l := args[0].LengthInt()
i := 0
for it := args[0].ElementIterator(); it.Next(); {
_, v := it.Element()
chunk = append(chunk, v)
// Chunk when index isn't 0, or when reaching the values's length
if (i+1)%size == 0 || (i+1) == l {
output = append(output, cty.ListVal(chunk))
chunk = make([]cty.Value, 0)
}
i++
}
return cty.ListVal(output), nil
},
})
// helper function to add an element to a list, if it does not already exsit
func appendIfMissing(slice []cty.Value, element cty.Value) ([]cty.Value, error) {
for _, ele := range slice {
eq, err := stdlib.Equal(ele, element)
if err != nil {
return slice, err
}
if eq.True() {
return slice, nil
}
}
return append(slice, element), nil
}
// Element returns a single element from a given list at the given index. If
// index is greater than the length of the list then it is wrapped modulo
// the list length.
@ -280,3 +376,13 @@ func Contains(list, value cty.Value) (cty.Value, error) {
func Index(list, value cty.Value) (cty.Value, error) {
return IndexFunc.Call([]cty.Value{list, value})
}
// Distinct takes a list and returns a new list with any duplicate elements removed.
func Distinct(list cty.Value) (cty.Value, error) {
return DistinctFunc.Call([]cty.Value{list})
}
// Chunklist splits a single list into fixed-size chunks, returning a list of lists.
func Chunklist(list, size cty.Value) (cty.Value, error) {
return ChunklistFunc.Call([]cty.Value{list, size})
}

View File

@ -565,3 +565,189 @@ func TestIndex(t *testing.T) {
})
}
}
func TestDistinct(t *testing.T) {
tests := []struct {
List cty.Value
Want cty.Value
Err bool
}{
{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("a"),
cty.StringVal("b"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
}),
false,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
cty.StringVal("d"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
cty.StringVal("d"),
}),
false,
},
{
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
false,
},
{
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
}),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
}),
false,
},
{
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
cty.ListVal([]cty.Value{
cty.NumberIntVal(3),
cty.NumberIntVal(4),
}),
}),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(2),
}),
cty.ListVal([]cty.Value{
cty.NumberIntVal(3),
cty.NumberIntVal(4),
}),
}),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("distinct(%#v)", test.List), func(t *testing.T) {
got, err := Distinct(test.List)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
func TestChunklist(t *testing.T) {
tests := []struct {
List cty.Value
Size cty.Value
Want cty.Value
Err bool
}{
{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.NumberIntVal(1),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("b"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("c"),
}),
}),
false,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.NumberIntVal(-1),
cty.NilVal,
true,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.NumberIntVal(0),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
}),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("chunklist(%#v, %#v)", test.List, test.Size), func(t *testing.T) {
got, err := Chunklist(test.List, test.Size)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}

View File

@ -49,9 +49,9 @@ func (s *Scope) Functions() map[string]function.Function {
"contains": funcs.ContainsFunc,
"csvdecode": stdlib.CSVDecodeFunc,
"dirname": funcs.DirnameFunc,
"distinct": unimplFunc, // TODO
"distinct": funcs.DistinctFunc,
"element": funcs.ElementFunc,
"chunklist": unimplFunc, // TODO
"chunklist": funcs.ChunklistFunc,
"file": funcs.MakeFileFunc(s.BaseDir, false),
"filebase64": funcs.MakeFileFunc(s.BaseDir, true),
"matchkeys": unimplFunc, // TODO