Skip to content

Commit

Permalink
coerce result of round function to integer
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Nov 4, 2024
1 parent e0f68e7 commit dcc6c07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 1.5.1 (Next)
- Refine attribute error messages.
- Coerce result of `round` function to integer.

### 1.5.0
- Do not coerce `number` type ID to `string` type ID (may cause superficial plan changes to existing states).
Expand Down
10 changes: 5 additions & 5 deletions stdlib/number/round_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type roundDataSource struct{}
type roundDataSourceModel struct {
ID types.Float64 `tfsdk:"id"`
Param types.Float64 `tfsdk:"param"`
Result types.Float64 `tfsdk:"result"`
Result types.Int64 `tfsdk:"result"`
}

// data source metadata
Expand All @@ -44,7 +44,7 @@ func (_ *roundDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
Description: "Input number parameter for determining the rounding.",
Required: true,
},
"result": schema.Float64Attribute{
"result": schema.Int64Attribute{
Computed: true,
Description: "Function result storing the rounding of the input parameter.",
},
Expand All @@ -66,15 +66,15 @@ func (_ *roundDataSource) Read(ctx context.Context, req datasource.ReadRequest,
inputNum := state.Param.ValueFloat64()

// determine the rounded integer
round := math.Round(inputNum)
round := int64(math.Round(inputNum))

// provide debug logging
ctx = tflog.SetField(ctx, "stdlib_round_result", round)
tflog.Debug(ctx, fmt.Sprintf("Input number parameter \"%f\" rounded is \"%f\"", inputNum, round))
tflog.Debug(ctx, fmt.Sprintf("Input number parameter \"%f\" rounded is \"%d\"", inputNum, round))

// store rounded result in state
state.ID = types.Float64Value(inputNum)
state.Result = types.Float64Value(round)
state.Result = types.Int64Value(round)

// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down

0 comments on commit dcc6c07

Please sign in to comment.