Skip to content

Commit

Permalink
do not coerce number type ID to string type ID
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Jul 30, 2024
1 parent 0779ca8 commit fbe39e6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.5.0 (Next)
- Do not coerce `number` type ID to `string` type ID (may cause superficial plan changes to existing states).

### 1.4.1
- Add `sorted` parameter to `list_index` function.
- Add `end_index` parameter to `replace` function.
Expand Down
2 changes: 1 addition & 1 deletion internal/id_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func IDStringAttribute() schema.StringAttribute {
}
}

func IDIntAttribute() schema.Int64Attribute {
func IDInt64Attribute() schema.Int64Attribute {
return schema.Int64Attribute{
Computed: true,
Description: "Aliased to number input parameter(s) for efficiency and proper plan diff detection.",
Expand Down
11 changes: 5 additions & 6 deletions stdlib/map/flatten_map_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mapfunc

import (
"context"
"fmt"
"golang.org/x/exp/maps"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand All @@ -29,9 +28,9 @@ type flattenMapDataSource struct{}

// maps the data source schema data to the model
type flattenMapDataSourceModel struct {
ID types.String `tfsdk:"id"`
Param types.List `tfsdk:"param"`
Result types.Map `tfsdk:"result"`
ID types.Int64 `tfsdk:"id"`
Param types.List `tfsdk:"param"`
Result types.Map `tfsdk:"result"`
}

// data source metadata
Expand All @@ -43,7 +42,7 @@ func (_ *flattenMapDataSource) Metadata(_ context.Context, req datasource.Metada
func (_ *flattenMapDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": util.IDStringAttribute(),
"id": util.IDInt64Attribute(),
"param": schema.ListAttribute{
Description: "Input list of maps to flatten.",
ElementType: types.MapType{
Expand Down Expand Up @@ -94,7 +93,7 @@ func (_ *flattenMapDataSource) Read(ctx context.Context, req datasource.ReadRequ
}

// store number of entries of output map as id
state.ID = types.StringValue(fmt.Sprint(len(outputMap)))
state.ID = types.Int64Value(int64(len(outputMap)))
// store flattened map in state
var mapConvertDiags diag.Diagnostics
state.Result, mapConvertDiags = types.MapValueFrom(ctx, types.StringType, outputMap)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/slice/max_number_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type maxNumberDataSource struct{}

// maps the data source schema data to the model
type maxNumberDataSourceModel struct {
ID types.String `tfsdk:"id"`
ID types.Int64 `tfsdk:"id"`
Param types.List `tfsdk:"param"`
Result types.Float64 `tfsdk:"result"`
}
Expand All @@ -42,7 +42,7 @@ func (_ *maxNumberDataSource) Metadata(_ context.Context, req datasource.Metadat
func (_ *maxNumberDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": util.IDStringAttribute(),
"id": util.IDInt64Attribute(),
"param": schema.ListAttribute{
Description: "Input list parameter for determining the maximum number.",
ElementType: types.Float64Type,
Expand Down Expand Up @@ -81,7 +81,7 @@ func (_ *maxNumberDataSource) Read(ctx context.Context, req datasource.ReadReque
tflog.Debug(ctx, fmt.Sprintf("Input list parameter \"%v\" max number is \"%f\"", inputList, maxNumber))

// store maxNumber from element(s) of list in state
state.ID = types.StringValue(fmt.Sprintf("%f", inputList[0]))
state.ID = types.Int64Value(int64(inputList[0]))
state.Result = types.Float64Value(maxNumber)

// set state
Expand Down
2 changes: 1 addition & 1 deletion stdlib/slice/max_number_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAccMaxNumber(test *testing.T) {
// verify maximum number result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_max_number.test", "result", "13"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_max_number.test", "id", "0.000000"),
resource.TestCheckResourceAttr("data.stdlib_max_number.test", "id", "0"),
),
},
},
Expand Down
6 changes: 3 additions & 3 deletions stdlib/slice/min_number_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type minNumberDataSource struct{}

// maps the data source schema data to the model
type minNumberDataSourceModel struct {
ID types.String `tfsdk:"id"`
ID types.Int64 `tfsdk:"id"`
Param types.List `tfsdk:"param"`
Result types.Float64 `tfsdk:"result"`
}
Expand All @@ -42,7 +42,7 @@ func (_ *minNumberDataSource) Metadata(_ context.Context, req datasource.Metadat
func (_ *minNumberDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": util.IDStringAttribute(),
"id": util.IDInt64Attribute(),
"param": schema.ListAttribute{
Description: "Input list parameter for determining the minimum number.",
ElementType: types.Float64Type,
Expand Down Expand Up @@ -81,7 +81,7 @@ func (_ *minNumberDataSource) Read(ctx context.Context, req datasource.ReadReque
tflog.Debug(ctx, fmt.Sprintf("Input list parameter \"%v\" min number is \"%f\"", inputList, minNumber))

// store minNumber from element(s) of list in state
state.ID = types.StringValue(fmt.Sprintf("%f", inputList[0]))
state.ID = types.Int64Value(int64(inputList[0]))
state.Result = types.Float64Value(minNumber)

// set state
Expand Down
2 changes: 1 addition & 1 deletion stdlib/slice/min_number_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAccMinNumber(test *testing.T) {
// verify minimum number result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_min_number.test", "result", "0"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_min_number.test", "id", "0.000000"),
resource.TestCheckResourceAttr("data.stdlib_min_number.test", "id", "0"),
),
},
},
Expand Down

0 comments on commit fbe39e6

Please sign in to comment.