diff --git a/kern/number.go b/kern/number.go index bf1b85c..30ba440 100644 --- a/kern/number.go +++ b/kern/number.go @@ -65,6 +65,38 @@ func AnyInteger(v any) (i int64, ok bool) { return } +func FixAnyNumber(v any) (fixed any) { + switch unboxed := v.(type) { + case int: + fixed = int64(unboxed) + case int8: + fixed = int64(unboxed) + case int16: + fixed = int64(unboxed) + case int32: + fixed = int64(unboxed) + case uint: + fixed = int64(unboxed) + case uint8: + fixed = int64(unboxed) + case uint16: + fixed = int64(unboxed) + case uint32: + fixed = int64(unboxed) + case uint64: + if unboxed <= MaxUint64Allowed { + fixed = int64(unboxed) + } else { + fixed = float64(unboxed) + } + case float32: + fixed = float64(unboxed) + default: + fixed = v + } + return +} + func ToGoInt(value any, description string) (i int, err error) { if valueInt64, ok := value.(int64); ok { i = int(valueInt64) diff --git a/t_list_test.go b/t_list_test.go index ef8a822..6abbe2f 100644 --- a/t_list_test.go +++ b/t_list_test.go @@ -70,7 +70,7 @@ func TestLinkedListParser(t *testing.T) { section := "Linked-List" inputs := []inputType{ - /* 1 */ {`[<1,2>]`, kern.NewLinkedListA(int64(1), int64(2)), nil}, + /* 1 */ {`[<1,2>]`, kern.NewLinkedListA(1, 2), nil}, } // t.Setenv("EXPR_PATH", ".")