Skip to content

Commit 345d3f5

Browse files
sagarp337MeharwadeDivya
authored andcommitted
Added - support for string to float32 conversion
1 parent 82b2b18 commit 345d3f5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

internal/tfresource/filters.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ func orComparator(target interface{}, filters []interface{}, stringsEqual String
315315
if val.Int() == fInt {
316316
return true
317317
}
318+
case reflect.Float32:
319+
// same comment as above for Ints
320+
fFloat, err := strconv.ParseFloat(fVal.(string), 32)
321+
if err != nil {
322+
log.Println("[WARN] Filtering against Type Float field with non-float filter value")
323+
return false
324+
}
325+
if val.Float() == fFloat {
326+
return true
327+
}
318328
case reflect.Float64:
319329
// same comment as above for Ints
320330
fFloat, err := strconv.ParseFloat(fVal.(string), 64)

internal/tfresource/filters_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,43 @@ func TestUnitApplyFilters_numberTypes(t *testing.T) {
694694
filters.Remove(floatFilter)
695695
}
696696

697+
// issue-routing-tag: terraform/default
698+
func TestUnitOrComparator(t *testing.T) {
699+
type args struct {
700+
target interface{}
701+
filters []interface{}
702+
}
703+
type testFormat struct {
704+
name string
705+
args args
706+
output bool
707+
}
708+
float32Filter := "200.34567"
709+
float64Filter := "200.34567321453457"
710+
filters := []interface{}{}
711+
filters = append(filters, float32Filter)
712+
filters = append(filters, float64Filter)
713+
tests := []testFormat{
714+
{
715+
name: "Test valid string to float32 conversion",
716+
args: args{target: float32(200.3456732145), filters: filters},
717+
output: true,
718+
},
719+
{
720+
name: "Test valid string to float64 conversion",
721+
args: args{target: float64(200.34567321453456788), filters: filters},
722+
output: true,
723+
},
724+
}
725+
726+
for _, test := range tests {
727+
t.Logf("Running %s", test.name)
728+
if res := orComparator(test.args.target, test.args.filters, nil); res != test.output {
729+
t.Errorf("Outputnot equal to expected")
730+
}
731+
}
732+
}
733+
697734
// issue-routing-tag: terraform/default
698735
func TestUnitApplyFilters_multiProperty(t *testing.T) {
699736
items := []map[string]interface{}{

0 commit comments

Comments
 (0)