-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpt_integer.go
More file actions
54 lines (44 loc) · 1.07 KB
/
pt_integer.go
File metadata and controls
54 lines (44 loc) · 1.07 KB
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
package main
import "strconv"
func init() {
RegisterPropertyType(&propertyTypeInteger{})
}
type propertyTypeInteger struct{}
var PropertyTypeInteger PropertyType = &propertyTypeInteger{}
var ErrIsNotInteger = ObserveError{Msg: "value is not an integer"}
func (*propertyTypeInteger) TypeName() string { return "int" }
func (p *propertyTypeInteger) Present(i any) (string, error) {
return p.ToString(i)
}
func (*propertyTypeInteger) ToString(i any) (string, error) {
if i == nil {
return "", nil
}
switch v := i.(type) {
case *int64:
if v == nil {
return "", nil
}
return strconv.FormatInt(*v, 10), nil
case int64:
return strconv.FormatInt(v, 10), nil
default:
return "", ErrIsNotInteger
}
}
func (*propertyTypeInteger) FromString(s string) (any, error) {
i64, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil, ErrIsNotInteger
}
if s[0] == '+' || s[0] == '0' {
return nil, ErrIsNotInteger
}
return i64, nil
}
func (*propertyTypeInteger) FromGQL(v any) any {
if v == nil {
return nil
}
return must(strconv.ParseInt(v.(string), 10, 64))
}