1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-17 01:32:22 +02:00

Switched from int to int64 (#188)

This commit is contained in:
Tim Voronov
2018-11-30 19:31:08 -05:00
committed by GitHub
parent 39e379f0f2
commit a219e776ad

View File

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