Skip to content

Commit

Permalink
fix: Default GO parsing is float64 for int (#539)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Poignant <[email protected]>
  • Loading branch information
thomaspoignant authored Jul 26, 2024
1 parent 1e2a7f4 commit 2f6a40e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions providers/ofrep/internal/evaluate/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func (h Flags) ResolveInt(ctx context.Context, key string, defaultValue int64, e
value = int64(evalSuccess.Value.(int))
case int64:
value = evalSuccess.Value.(int64)
case float64:
value = int64(evalSuccess.Value.(float64))
if float64(value) != evalSuccess.Value.(float64) {
return of.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf(
"resolved value %v is not of integer type", evalSuccess.Value)),
Reason: of.ErrorReason,
},
}
}
default:
return of.IntResolutionDetail{
Value: defaultValue,
Expand Down
15 changes: 15 additions & 0 deletions providers/ofrep/internal/evaluate/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ var successInt64 = successDto{
Metadata: nil,
}

var successInt64WithFloat64 = successDto{
Value: float64(10),
Reason: string(of.StaticReason),
Variant: "10",
Metadata: nil,
}

var successFloat = successDto{
Value: float32(1.10),
Reason: string(of.StaticReason),
Expand Down Expand Up @@ -143,6 +150,14 @@ func TestIntegerEvaluation(t *testing.T) {
defaultValue: 1,
expect: successInt64.Value.(int64),
},
{
name: "Success evaluation - int64 with a float64",
resolver: mockResolver{
success: &successInt64WithFloat64,
},
defaultValue: 1,
expect: int64(successInt64WithFloat64.Value.(float64)),
},
{
name: "Error evaluation",
resolver: mockResolver{
Expand Down

0 comments on commit 2f6a40e

Please sign in to comment.