Skip to content

Commit

Permalink
fix: check type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
henryde committed May 21, 2024
1 parent a4d006f commit b583ab0
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions internal/provider/buildingblock_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -149,24 +150,42 @@ func (d *buildingBlockDataSource) Read(ctx context.Context, req datasource.ReadR
ValueType types.String `tfsdk:"value_type"`
}

mkIoList := func(ios *[]MeshBuildingBlockIO) *[]io {
mkIoList := func(ios *[]MeshBuildingBlockIO) (*[]io, error) {
result := make([]io, 0)
var err error
for _, input := range *ios {
var valueString *string
var valueInt *int64
var valueBool *bool

if input.ValueType == "STRING" {
val := input.Value.(string)
val, ok := input.Value.(string)
if !ok {
err = errors.Join(err, fmt.Errorf("Unexpected type '%s' for key '%s'.", input.ValueType, input.Key))
continue
}
valueString = &val
} else if input.ValueType == "INTEGER" {
val := int64(input.Value.(float64))
valueInt = &val
val, ok := input.Value.(float64)
if !ok {
err = errors.Join(err, fmt.Errorf("Unexpected type '%s' for key '%s'.", input.ValueType, input.Key))
continue
}
valInt := int64(val)
valueInt = &valInt
} else if input.ValueType == "BOOLEAN" {
val := input.Value.(bool)
val, ok := input.Value.(bool)
if !ok {
err = errors.Join(err, fmt.Errorf("Unexpected type '%s' for key '%s'.", input.ValueType, input.Key))
continue
}
valueBool = &val
}

if err != nil {
continue
}

result = append(result, io{
Key: types.StringValue(input.Key),
ValueString: types.StringPointerValue(valueString),
Expand All @@ -175,7 +194,7 @@ func (d *buildingBlockDataSource) Read(ctx context.Context, req datasource.ReadR
ValueType: types.StringValue(input.ValueType),
})
}
return &result
return &result, err
}

// get UUID for BB we want to query from the request
Expand All @@ -194,11 +213,27 @@ func (d *buildingBlockDataSource) Read(ctx context.Context, req datasource.ReadR
resp.State.SetAttribute(ctx, path.Root("spec").AtName("display_name"), bb.Spec.DisplayName)
resp.State.SetAttribute(ctx, path.Root("spec").AtName("parent_building_blocks"), bb.Spec.ParentBuildingBlocks)
if bb.Spec.Inputs != nil {
resp.State.SetAttribute(ctx, path.Root("spec").AtName("inputs"), mkIoList(&bb.Spec.Inputs))
inputs, err := mkIoList(&bb.Spec.Inputs)
if err != nil {
resp.Diagnostics.AddError(
"Error(s) while reading inputs/outputs",
err.Error(),
)
return
}
resp.State.SetAttribute(ctx, path.Root("spec").AtName("inputs"), inputs)
}

resp.State.SetAttribute(ctx, path.Root("status").AtName("status"), bb.Status.Status)
if bb.Status.Outputs != nil {
resp.State.SetAttribute(ctx, path.Root("status").AtName("outputs"), &bb.Status.Outputs)
outputs, err := mkIoList(&bb.Status.Outputs)
if err != nil {
resp.Diagnostics.AddError(
"Error(s) while reading inputs/outputs",
err.Error(),
)
return
}
resp.State.SetAttribute(ctx, path.Root("status").AtName("outputs"), outputs)
}
}

0 comments on commit b583ab0

Please sign in to comment.