diff --git a/utils.go b/utils.go index 892ae94..1b4b600 100644 --- a/utils.go +++ b/utils.go @@ -13,6 +13,16 @@ func convertToInt(num any) (int, error) { switch val := num.(type) { case int: return val, nil + case int8: + return int(val), nil + case int16: + return int(val), nil + case int32: + return int(val), nil + case int64: + return int(val), nil + case float32: + return int(val), nil case float64: return int(val), nil } diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..dc89e36 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,33 @@ +package gitlab + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConvertToInt(t *testing.T) { + var tests = []struct { + in any + outVal int + outErr error + }{ + {int(52), int(52), nil}, + {int8(13), int(13), nil}, + {int16(612), int(612), nil}, + {int32(56236), int(56236), nil}, + {int64(23462346), int(23462346), nil}, + {float32(62346.62), int(62346), nil}, + {float64(263467.26), int(263467), nil}, + {"1", int(0), ErrInvalidValue}, + } + + for _, tst := range tests { + t.Logf("convertToInt(%T(%v))", tst.in, tst.in) + val, err := convertToInt(tst.in) + assert.Equal(t, tst.outVal, val) + if tst.outErr != nil { + assert.ErrorIs(t, err, tst.outErr) + } + } +}