-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
compat.go
49 lines (44 loc) · 861 Bytes
/
compat.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
package mastodon
import (
"encoding/json"
"fmt"
"strconv"
)
type ID string
func (id *ID) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*id = ID(s)
return nil
}
var n int64
if err := json.Unmarshal(data, &n); err != nil {
return err
}
*id = ID(fmt.Sprint(n))
return nil
}
type Sbool bool
func (s *Sbool) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
b, err := strconv.ParseBool(str)
if err != nil {
return err
}
*s = Sbool(b)
return nil
}
var b bool
if err := json.Unmarshal(data, &b); err != nil {
return err
}
*s = Sbool(b)
return nil
}