-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
110 lines (95 loc) · 2.06 KB
/
types.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package Pixivlee
import (
"encoding/json"
"sort"
"strconv"
"strings"
"time"
)
// IPixiver pixiver
type IPixiver interface {
Pid() TPid
SessionID() string
State() int
SetPid(TPid)
UpdateState(state int)
}
type (
TPid uint64 // pid type alise
TArtId uint64 // artId type alise
TIllustType uint8 // illust 0; manga 1; ugoira 2
TCount uint32
TTimestamp time.Time
artWorkIds []TArtId
jsonMap[k string, v any] map[k]v
)
func (t *TPid) UnmarshalJSON(body []byte) error {
data, err := strconv.ParseUint(strings.ReplaceAll(string(body), "\"", ""), 10, 64)
if err != nil {
return err
}
*t = TPid(data)
return nil
}
func (t *TArtId) UnmarshalJSON(body []byte) error {
data, err := strconv.ParseUint(strings.ReplaceAll(string(body), "\"", ""), 10, 64)
if err != nil {
return err
}
*t = TArtId(data)
return nil
}
func (t *TIllustType) UnmarshalJSON(body []byte) error {
if _t, err := strconv.ParseUint(strings.ReplaceAll(string(body), "\"", ""), 10, 8); err != nil {
return err
} else {
*t = TIllustType(_t)
}
return nil
}
func (t *TCount) UnmarshalJSON(body []byte) error {
if _t, err := strconv.ParseUint(strings.ReplaceAll(string(body), "\"", ""), 10, 32); err != nil {
return err
} else {
*t = TCount(_t)
}
return nil
}
func (t *TTimestamp) UnmarshalJSON(body []byte) error {
if _t, err := strconv.ParseInt(string(body), 10, 64); err != nil {
return err
} else {
*t = TTimestamp(time.Unix(_t, 0))
}
return nil
}
func (s *artWorkIds) UnmarshalJSON(body []byte) error {
var (
data map[string]struct{}
ids artWorkIds
)
if err := json.Unmarshal(body, &data); err == nil {
for idStr := range data {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return err
}
ids = append(ids, TArtId(id))
}
}
sort.Slice(ids, func(i, j int) bool {
return ids[i] < ids[j]
})
*s = ids
return nil
}
func (m *jsonMap[k, v]) UnmarshalJSON(body []byte) error {
data := map[k]v{}
if len(body) > 5 {
if err := json.Unmarshal(body, &data); err != nil {
return err
}
}
*m = data
return nil
}