forked from sensorbee/sensorbee
/
int.go
64 lines (51 loc) · 1.37 KB
/
int.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package data
import (
"encoding/json"
"fmt"
"time"
)
// Int is an integer. It can be assigned to Value interface. A value more than
// 2^53 - 1 cannot exactly be marshaled to JSON because some languages like
// JavaScript or Lua only has float as a numeric type.
type Int int64
// Type returns TypeID of Int. It's always TypeInt.
func (i Int) Type() TypeID {
return TypeInt
}
func (i Int) asBool() (bool, error) {
return false, castError(i.Type(), TypeBool)
}
func (i Int) asInt() (int64, error) {
return int64(i), nil
}
func (i Int) asFloat() (float64, error) {
return 0, castError(i.Type(), TypeFloat)
}
func (i Int) asString() (string, error) {
return "", castError(i.Type(), TypeString)
}
func (i Int) asBlob() ([]byte, error) {
return nil, castError(i.Type(), TypeBlob)
}
func (i Int) asTimestamp() (time.Time, error) {
return time.Time{}, castError(i.Type(), TypeTimestamp)
}
func (i Int) asArray() (Array, error) {
return nil, castError(i.Type(), TypeArray)
}
func (i Int) asMap() (Map, error) {
return nil, castError(i.Type(), TypeMap)
}
func (i Int) clone() Value {
return Int(i)
}
// String returns JSON representation of an Int.
func (i Int) String() string {
// the String return value is defined via the
// default JSON serialization
bytes, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("(unserializable int: %v)", err)
}
return string(bytes)
}