Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions types/basetypes/list_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ func (l ListValue) Elements() []attr.Value {
return result
}

// Length returns the number of elements in the List.
func (l ListValue) Length() int {
return len(l.elements)
}

// ElementsAs populates `target` with the elements of the ListValue, throwing an
// error if the elements cannot be stored in `target`.
func (l ListValue) ElementsAs(ctx context.Context, target interface{}, allowUnhandled bool) diag.Diagnostics {
Expand Down
45 changes: 45 additions & 0 deletions types/basetypes/list_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,51 @@ func TestListValueType(t *testing.T) {
}
}

func TestListValueLength(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input ListValue
expected int
}{
"known-empty": {
input: NewListValueMust(StringType{}, []attr.Value{}),
expected: 0,
},
"known-single": {
input: NewListValueMust(StringType{}, []attr.Value{NewStringValue("test")}),
expected: 1,
},
"known-multiple": {
input: NewListValueMust(StringType{}, []attr.Value{
NewStringValue("hello"),
NewStringValue("world"),
}),
expected: 2,
},
"null": {
input: NewListNull(StringType{}),
expected: 0,
},
"unknown": {
input: NewListUnknown(StringType{}),
expected: 0,
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.Length()

if got != testCase.expected {
t.Errorf("Expected %d, got %d", testCase.expected, got)
}
})
}
}

func TestListTypeValidate(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 5 additions & 0 deletions types/basetypes/map_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func (m MapValue) Elements() map[string]attr.Value {
return result
}

// Length returns the number of elements in the Map.
func (m MapValue) Length() int {
return len(m.elements)
}

// ElementsAs populates `target` with the elements of the MapValue, throwing an
// error if the elements cannot be stored in `target`.
func (m MapValue) ElementsAs(ctx context.Context, target interface{}, allowUnhandled bool) diag.Diagnostics {
Expand Down
45 changes: 45 additions & 0 deletions types/basetypes/map_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,51 @@ func TestMapValueType(t *testing.T) {
}
}

func TestMapValueLength(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input MapValue
expected int
}{
"known-empty": {
input: NewMapValueMust(StringType{}, map[string]attr.Value{}),
expected: 0,
},
"known-single": {
input: NewMapValueMust(StringType{}, map[string]attr.Value{"key": NewStringValue("test")}),
expected: 1,
},
"known-multiple": {
input: NewMapValueMust(StringType{}, map[string]attr.Value{
"key1": NewStringValue("hello"),
"key2": NewStringValue("world"),
}),
expected: 2,
},
"null": {
input: NewMapNull(StringType{}),
expected: 0,
},
"unknown": {
input: NewMapUnknown(StringType{}),
expected: 0,
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.Length()

if got != testCase.expected {
t.Errorf("Expected %d, got %d", testCase.expected, got)
}
})
}
}

func TestMapTypeValidate(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 5 additions & 0 deletions types/basetypes/set_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ func (s SetValue) Elements() []attr.Value {
return result
}

// Length returns the number of elements in the Set.
func (s SetValue) Length() int {
return len(s.elements)
}

// ElementsAs populates `target` with the elements of the SetValue, throwing an
// error if the elements cannot be stored in `target`.
func (s SetValue) ElementsAs(ctx context.Context, target interface{}, allowUnhandled bool) diag.Diagnostics {
Expand Down
47 changes: 47 additions & 0 deletions types/basetypes/set_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,50 @@ func TestSetValueType(t *testing.T) {
})
}
}

func TestSetValueLength(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input SetValue
expected int
}{
"known-empty": {
input: NewSetValueMust(StringType{}, []attr.Value{}),
expected: 0,
},
"known-single": {
input: NewSetValueMust(StringType{}, []attr.Value{NewStringValue("test")}),
expected: 1,
},
"known-multiple": {
input: NewSetValueMust(StringType{}, []attr.Value{
NewStringValue("hello"),
NewStringValue("world"),
}),
expected: 2,
},
"null": {
input: NewSetNull(StringType{}),
expected: 0,
},
"unknown": {
input: NewSetUnknown(StringType{}),
expected: 0,
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.Length()

if got != testCase.expected {
t.Errorf("Expected %d, got %d", testCase.expected, got)
}
})
}
}


5 changes: 5 additions & 0 deletions types/basetypes/tuple_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func (v TupleValue) Elements() []attr.Value {
return result
}

// Length returns the number of elements in the Tuple.
func (v TupleValue) Length() int {
return len(v.elements)
}

// ElementTypes returns the ordered list of element types for the Tuple.
func (v TupleValue) ElementTypes(ctx context.Context) []attr.Type {
return v.elementTypes
Expand Down
45 changes: 45 additions & 0 deletions types/basetypes/tuple_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,48 @@ func TestTupleValueToTerraformValue(t *testing.T) {
})
}
}

func TestTupleValueLength(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input TupleValue
expected int
}{
"known-empty": {
input: NewTupleValueMust([]attr.Type{}, []attr.Value{}),
expected: 0,
},
"known-single": {
input: NewTupleValueMust([]attr.Type{StringType{}}, []attr.Value{NewStringValue("test")}),
expected: 1,
},
"known-multiple": {
input: NewTupleValueMust(
[]attr.Type{StringType{}, BoolType{}},
[]attr.Value{NewStringValue("hello"), NewBoolValue(true)},
),
expected: 2,
},
"null": {
input: NewTupleNull([]attr.Type{StringType{}, BoolType{}}),
expected: 0,
},
"unknown": {
input: NewTupleUnknown([]attr.Type{StringType{}, BoolType{}}),
expected: 0,
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.Length()

if got != testCase.expected {
t.Errorf("Expected %d, got %d", testCase.expected, got)
}
})
}
}