-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define Set type #53
Comments
I originally thought we wanted to use a third-party Go implementation for this, but the more I think about it, the more convinced I am that we actually want to build our own, in-house set implementation using |
This might have been developer error on my part, but in trying to implement an "ordered set" type (list with required ordering, but element-uniqueness constraints), I was having trouble with creating a Panic:
Code: package types
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
var (
_ attr.TypeWithValidate = OrderedSetType{}
)
type OrderedSetType struct {
ListType
}
func (t OrderedSetType) Validate(ctx context.Context, in tftypes.Value) []*tfprotov6.Diagnostic {
var diags []*tfprotov6.Diagnostic
if !in.Type().Is(tftypes.List{}) {
err := fmt.Errorf("expected List value, received %T with value: %v", in, in)
return append(diags, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "OrderedSet Type Validation Error",
Detail: "An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer:\n\n" + err.Error(),
})
}
var vals []tftypes.Value
if err := in.As(&vals); err != nil {
return append(diags, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "OrderedSet Type Validation Error",
Detail: "An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. Please report the following to the provider developer:\n\n" + err.Error(),
})
}
duplicatesMap := make(map[tftypes.Value]struct{})
valsMap := make(map[tftypes.Value]struct{})
for _, val := range vals {
if _, ok := valsMap[val]; ok {
if _, ok := duplicatesMap[val]; ok {
continue
}
duplicatesMap[val] = struct{}{}
continue
}
valsMap[val] = struct{}{}
}
if len(duplicatesMap) > 0 {
var duplicates []string
for duplicate := range duplicatesMap {
duplicates = append(duplicates, duplicate.String())
}
return append(diags, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Duplicate Set Elements",
Detail: fmt.Sprintf("This attribute contains duplicate elements of:\n\n%s", strings.Join(duplicates, "\n")),
})
}
return nil
} |
I think my hot take is that we should start with unordered sets, as that's what the Terraform type expects, and right now there's no way to surface a The string representation of the value should be consistent, but I think what we really want is to use the |
Certainly, opened #99 to track the potential for an "ordered set" type, unordered sets (surfacing |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Backed by an actual Set implementation.
The text was updated successfully, but these errors were encountered: