-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
63 lines (51 loc) · 996 Bytes
/
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
// Package types - Content managed by Project Forge, see [projectforge.md] for details.
package types
import (
"fmt"
"strconv"
)
const KeyInt = "int"
type Int struct {
Bits int `json:"bits,omitempty"`
Unsigned bool `json:"unsigned,omitempty"`
}
var _ Type = (*Int)(nil)
func (x *Int) Key() string {
return KeyInt
}
func (x *Int) Sortable() bool {
return true
}
func (x *Int) Scalar() bool {
return true
}
func (x *Int) String() string {
if x.Bits > 0 {
return fmt.Sprintf("%s%d", x.Key(), x.Bits)
}
return x.Key()
}
func (x *Int) From(v any) any {
switch t := v.(type) {
case string:
ret, _ := strconv.ParseInt(t, 10, 32)
return int(ret)
case int:
return t
case int32:
return t
case int64:
return t
default:
return invalidInput(x.Key(), t)
}
}
func (x *Int) Default(string) any {
return 0
}
func NewInt(bits int) *Wrapped {
return Wrap(&Int{Bits: bits})
}
func NewUnsignedInt(bits int) *Wrapped {
return Wrap(&Int{Bits: bits, Unsigned: true})
}