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"
|
2018-10-12 17:58:08 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2019-02-13 19:31:18 +02:00
|
|
|
"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
|
|
|
|
2019-02-13 19:31:18 +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
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
switch val := input.(type) {
|
2018-12-01 02:31:08 +02:00
|
|
|
case int:
|
2019-03-29 16:48:51 +02:00
|
|
|
return Int(val), nil
|
2018-12-01 02:31:08 +02:00
|
|
|
case int64:
|
2019-03-29 16:48:51 +02:00
|
|
|
return Int(val), nil
|
2018-12-01 02:31:08 +02:00
|
|
|
case int32:
|
2019-03-29 16:48:51 +02:00
|
|
|
return Int(val), nil
|
2018-12-01 02:31:08 +02:00
|
|
|
case int16:
|
2019-03-29 16:48:51 +02:00
|
|
|
return Int(val), nil
|
2018-12-01 02:31:08 +02:00
|
|
|
case int8:
|
2019-03-29 16:48:51 +02:00
|
|
|
return Int(val), nil
|
2018-12-01 02:31:08 +02:00
|
|
|
case string:
|
2019-03-29 16:48:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-13 01:01:49 +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 {
|
2019-02-13 19:31:18 +02:00
|
|
|
return types.Int
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t Int) String() string {
|
|
|
|
return strconv.Itoa(int(t))
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
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
|
2019-02-13 19:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-02-13 19:31:18 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-10-12 17:58:08 +02:00
|
|
|
func (t Int) Copy() core.Value {
|
2018-09-27 17:53:26 +02:00
|
|
|
return t
|
|
|
|
}
|