Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Throw error when Tuple contains mixed Types #233

Closed
wants to merge 9 commits into from
Closed
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
62 changes: 62 additions & 0 deletions provider/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,68 @@ func (s *RawProviderServer) ValidateResourceTypeConfig(ctx context.Context, req
return resp, nil
}

// This section validates that all elements within a Tuple have the same Type.
// https://github.com/hashicorp/terraform-provider-kubernetes-alpha/issues/231
err = tftypes.Walk(config, func(path *tftypes.AttributePath, val tftypes.Value) (bool, error) {
if val.IsNull() || !val.IsKnown() {
return false, nil
}
if val.Type().Is(tftypes.Tuple{}) {
// Convert the Tuple into a Go value that can be inspected.
var tuple []tftypes.Value
val.As(&tuple)
containsObjects := false

// Inspect each Object in the Tuple.
for _, tupleElem := range tuple {
var objectAsMap map[string]tftypes.Value
if tupleElem.Type().Is(tftypes.Object{}) {
containsObjects = true
tupleElem.As(&objectAsMap)

}

// Collect the element types from the Object.
var elementTypes []tftypes.Value
for _, v := range objectAsMap {
elementTypes = append(elementTypes, v)
}

// Check the list of element types.
_, err := tftypes.TypeFromElements(elementTypes)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Tuple element failed validation:" + tupleElem.String(),
Detail: err.Error(),
})
return false, err
}
}
// If a Tuple contains no Objects, its elements can be checked.
if !containsObjects {
_, err := tftypes.TypeFromElements(tuple)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Tuple failed validation:" + val.String(),
Detail: err.Error(),
})
return false, err
}
}
}
return true, nil
})
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Config validation failed",
Detail: err.Error(),
})
return resp, nil
}

att := tftypes.NewAttributePath()
att = att.WithAttributeName("manifest")

Expand Down