Skip to content

Commit 40f717f

Browse files
yasirfolio3Mat001
andauthored
nit(attribute-validation): Using switch instead of multiple if checks. (#341)
* Using switch instead of multiple if checks. * Covering all basic types. * Added further checks and unit tests following Csharp sdk. Co-authored-by: Matjaz Pirnovar <[email protected]>
1 parent c27e4f2 commit 40f717f

File tree

2 files changed

+101
-15
lines changed

2 files changed

+101
-15
lines changed

pkg/event/factory.go

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package event
1919

2020
import (
2121
"errors"
22+
"fmt"
23+
"math"
24+
"reflect"
2225
"strings"
2326
"time"
2427

@@ -292,22 +295,76 @@ func getTagValue(eventTags map[string]interface{}) (float64, error) {
292295
return 0, errors.New("no event tag found for value")
293296
}
294297

298+
// Validates if the type of provided value is numeric.
299+
func isNumericType(value interface{}) (float64, error) {
300+
switch i := value.(type) {
301+
case int:
302+
return float64(i), nil
303+
case int8:
304+
return float64(i), nil
305+
case int16:
306+
return float64(i), nil
307+
case int32:
308+
return float64(i), nil
309+
case int64:
310+
return float64(i), nil
311+
case uint:
312+
return float64(i), nil
313+
case uint8:
314+
return float64(i), nil
315+
case uint16:
316+
return float64(i), nil
317+
case uint32:
318+
return float64(i), nil
319+
case uint64:
320+
return float64(i), nil
321+
case uintptr:
322+
return float64(i), nil
323+
case float32:
324+
return float64(i), nil
325+
case float64:
326+
return i, nil
327+
default:
328+
v := reflect.ValueOf(value)
329+
v = reflect.Indirect(v)
330+
return math.NaN(), fmt.Errorf("can't convert %v to float64", v.Type())
331+
}
332+
}
333+
334+
// Validates if the provided value is a valid numeric value.
335+
func isValidNumericValue(value interface{}) bool {
336+
if floatValue, err := isNumericType(value); err == nil {
337+
if math.IsNaN(floatValue) {
338+
return false
339+
}
340+
if math.IsInf(floatValue, 1) {
341+
return false
342+
}
343+
if math.IsInf(floatValue, -1) {
344+
return false
345+
}
346+
if math.IsInf(floatValue, 0) {
347+
return false
348+
}
349+
if math.Abs(floatValue) > math.Pow(2, 53) {
350+
return false
351+
}
352+
return true
353+
}
354+
return false
355+
}
356+
295357
// check if attribute value is valid
296358
func isValidAttribute(value interface{}) bool {
297359
if value == nil {
298360
return false
299361
}
300-
if _, ok := value.(string); ok {
301-
return true
302-
}
303-
if _, ok := value.(float64); ok {
304-
return true
305-
}
306-
if _, ok := value.(int); ok {
307-
return true
308-
}
309-
if _, ok := value.(bool); ok {
362+
363+
switch value.(type) {
364+
// https://go.dev/tour/basics/11
365+
case bool, string:
310366
return true
367+
default:
368+
return isValidNumericValue(value)
311369
}
312-
return false
313370
}

pkg/event/factory_test.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package event
1919

2020
import (
2121
"context"
22+
"math"
2223
"math/rand"
2324
"testing"
2425
"time"
@@ -241,8 +242,36 @@ func TestIsValidAttribute(t *testing.T) {
241242
assert.False(t, isValidAttribute([]string{}))
242243
assert.False(t, isValidAttribute([]interface{}{}))
243244
assert.False(t, isValidAttribute(make(chan int)))
244-
assert.True(t, isValidAttribute("123"))
245-
assert.True(t, isValidAttribute(1.11))
246-
assert.True(t, isValidAttribute(1))
247-
assert.True(t, isValidAttribute(true))
245+
assert.False(t, isValidAttribute(complex64(1234.1231)))
246+
assert.False(t, isValidAttribute(complex128(123446.123123)))
247+
assert.False(t, isValidAttribute(math.Pow(2, 54)))
248+
249+
assert.False(t, isValidAttribute(math.NaN()))
250+
posInf := math.Inf(1)
251+
assert.False(t, isValidAttribute(posInf))
252+
posInf += 12.2 // infinite value will still propagate after add operation
253+
assert.False(t, isValidAttribute(posInf))
254+
negInf := math.Inf(-1)
255+
assert.False(t, isValidAttribute(negInf))
256+
negInf /= negInf // will turn infinite into NaN
257+
assert.False(t, isValidAttribute(negInf))
258+
assert.False(t, isValidAttribute(math.Inf(0)))
259+
260+
assert.True(t, isValidAttribute(bool(true)))
261+
assert.True(t, isValidAttribute(string("abcd")))
262+
assert.True(t, isValidAttribute(int(1)))
263+
assert.True(t, isValidAttribute(int8(-12)))
264+
assert.True(t, isValidAttribute(int16(-123)))
265+
assert.True(t, isValidAttribute(int32(1234)))
266+
assert.True(t, isValidAttribute(int64(123446)))
267+
assert.True(t, isValidAttribute(uint(1)))
268+
assert.True(t, isValidAttribute(uint8(12)))
269+
assert.True(t, isValidAttribute(uint16(123)))
270+
assert.True(t, isValidAttribute(uint32(1234)))
271+
assert.True(t, isValidAttribute(uint64(123446)))
272+
assert.True(t, isValidAttribute(uintptr(1)))
273+
assert.True(t, isValidAttribute(float32(12.11)))
274+
assert.True(t, isValidAttribute(float64(123.1231)))
275+
assert.True(t, isValidAttribute(byte(134)))
276+
assert.True(t, isValidAttribute(rune(123446)))
248277
}

0 commit comments

Comments
 (0)