1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/values/int.go

131 lines
1.8 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package values
import (
2018-10-05 21:17:22 +02:00
"encoding/binary"
2018-09-18 22:42:38 +02:00
"encoding/json"
2018-10-05 21:17:22 +02:00
"hash/fnv"
2018-09-18 22:42:38 +02:00
"strconv"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
2018-09-18 22:42:38 +02:00
)
2018-12-01 02:31:08 +02:00
type Int int64
2018-09-18 22:42:38 +02:00
const ZeroInt = Int(0)
2018-09-18 22:42:38 +02:00
func NewInt(input int) Int {
2018-12-01 02:31:08 +02:00
return Int(int64(input))
2018-09-18 22:42:38 +02:00
}
func ParseInt(input interface{}) (Int, error) {
if core.IsNil(input) {
return ZeroInt, nil
}
switch val := input.(type) {
2018-12-01 02:31:08 +02:00
case int:
return Int(val), nil
2018-12-01 02:31:08 +02:00
case int64:
return Int(val), nil
2018-12-01 02:31:08 +02:00
case int32:
return Int(val), nil
2018-12-01 02:31:08 +02:00
case int16:
return Int(val), nil
2018-12-01 02:31:08 +02:00
case int8:
return Int(val), nil
2018-12-01 02:31:08 +02:00
case string:
i, err := strconv.Atoi(val)
2018-09-18 22:42:38 +02:00
if err == nil {
if i == 0 {
return ZeroInt, nil
}
return Int(i), nil
}
2018-12-01 02:31:08 +02:00
return ZeroInt, err
default:
return ZeroInt, core.Error(core.ErrInvalidType, "expected 'int'")
}
2018-09-18 22:42:38 +02:00
}
func MustParseInt(input interface{}) Int {
2018-09-18 22:42:38 +02:00
res, err := ParseInt(input)
if err != nil {
panic(err)
}
return res
}
func (t Int) MarshalJSON() ([]byte, error) {
2018-12-01 02:31:08 +02:00
return json.Marshal(int64(t))
2018-09-18 22:42:38 +02:00
}
func (t Int) Type() core.Type {
return types.Int
2018-09-18 22:42:38 +02:00
}
func (t Int) String() string {
return strconv.Itoa(int(t))
}
func (t Int) Compare(other core.Value) int64 {
otherType := other.Type()
if otherType == types.Int {
2018-12-01 02:31:08 +02:00
i := other.(Int)
2018-09-18 22:42:38 +02:00
2018-12-01 02:31:08 +02:00
if t == i {
2018-09-18 22:42:38 +02:00
return 0
}
2018-12-01 02:31:08 +02:00
if t < i {
2018-09-18 22:42:38 +02:00
return -1
}
return +1
}
if otherType == types.Float {
2018-12-01 02:31:08 +02:00
f := other.(Float)
f2 := Float(t)
2018-09-18 22:42:38 +02:00
2018-12-01 02:31:08 +02:00
if f2 == f {
2018-09-18 22:42:38 +02:00
return 0
}
2018-12-01 02:31:08 +02:00
if f2 < f {
2018-09-18 22:42:38 +02:00
return -1
}
return +1
}
return types.Compare(types.Int, otherType)
2018-09-18 22:42:38 +02:00
}
func (t Int) Unwrap() interface{} {
return int(t)
}
2018-10-05 21:17:22 +02:00
func (t Int) Hash() uint64 {
h := fnv.New64a()
h.Write([]byte(t.Type().String()))
h.Write([]byte(":"))
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(t))
h.Write(bytes)
return h.Sum64()
2018-09-18 22:42:38 +02:00
}
2018-09-27 17:53:26 +02:00
func (t Int) Copy() core.Value {
2018-09-27 17:53:26 +02:00
return t
}