diff --git a/pkg/runtime/values/int.go b/pkg/runtime/values/int.go index 8d3c5d36..4620f688 100644 --- a/pkg/runtime/values/int.go +++ b/pkg/runtime/values/int.go @@ -9,12 +9,12 @@ import ( "github.com/MontFerret/ferret/pkg/runtime/core" ) -type Int int +type Int int64 var ZeroInt = Int(0) func NewInt(input int) Int { - return Int(input) + return Int(int64(input)) } func ParseInt(input interface{}) (Int, error) { @@ -22,21 +22,19 @@ func ParseInt(input interface{}) (Int, error) { return ZeroInt, nil } - i, ok := input.(int) - - if ok { - if i == 0 { - return ZeroInt, nil - } - - return Int(i), nil - } - - // try to cast - str, ok := input.(string) - - if ok { - i, err := strconv.Atoi(str) + switch input.(type) { + case int: + return Int(input.(int)), nil + case int64: + return Int(input.(int64)), nil + case int32: + return Int(input.(int32)), nil + case int16: + return Int(input.(int16)), nil + case int8: + return Int(input.(int8)), nil + case string: + i, err := strconv.Atoi(input.(string)) if err == nil { if i == 0 { @@ -45,9 +43,11 @@ func ParseInt(input interface{}) (Int, error) { return Int(i), nil } - } - return ZeroInt, core.Error(core.ErrInvalidType, "expected 'int'") + return ZeroInt, err + default: + return ZeroInt, core.Error(core.ErrInvalidType, "expected 'int'") + } } func ParseIntP(input interface{}) Int { @@ -61,7 +61,7 @@ func ParseIntP(input interface{}) Int { } func (t Int) MarshalJSON() ([]byte, error) { - return json.Marshal(int(t)) + return json.Marshal(int64(t)) } func (t Int) Type() core.Type { @@ -73,30 +73,28 @@ func (t Int) String() string { } func (t Int) Compare(other core.Value) int { - raw := int(t) - switch other.Type() { case core.IntType: - i := other.Unwrap().(int) + i := other.(Int) - if raw == i { + if t == i { return 0 } - if raw < i { + if t < i { return -1 } return +1 case core.FloatType: - f := other.Unwrap().(float64) - i := int(f) + f := other.(Float) + f2 := Float(t) - if raw == i { + if f2 == f { return 0 } - if raw < i { + if f2 < f { return -1 }