-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
strint.go
50 lines (41 loc) · 808 Bytes
/
strint.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
package types
import (
"encoding/json"
"fmt"
"strconv"
)
type StrInt64 int64
func (s *StrInt64) MarshalJSON() ([]byte, error) {
ss := strconv.FormatInt(int64(*s), 10)
return json.Marshal(ss)
}
func (s *StrInt64) UnmarshalJSON(body []byte) error {
var arg interface{}
if err := json.Unmarshal(body, &arg); err != nil {
return err
}
switch ta := arg.(type) {
case string:
// parse string
i, err := strconv.ParseInt(ta, 10, 64)
if err != nil {
return err
}
*s = StrInt64(i)
case int64:
*s = StrInt64(ta)
case int32:
*s = StrInt64(ta)
case int:
*s = StrInt64(ta)
default:
return fmt.Errorf("StrInt64 error: unsupported value type %T", ta)
}
return nil
}
func (s *StrInt64) String() string {
if s == nil {
return ""
}
return strconv.FormatInt(int64(*s), 10)
}