-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.go
71 lines (62 loc) · 1.3 KB
/
item.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
package youcrawl
import "errors"
var (
KeyNotContainError = errors.New("key not in item")
TypeError = errors.New("error type of item value")
)
const (
ItemKeyChannelToken ="channelToken"
)
type DefaultItem struct {
Store map[string]interface{}
}
func (i DefaultItem) GetToken() string {
tokenString,err := i.GetString(ItemKeyChannelToken)
if err != nil {
return ""
}
return tokenString
}
func (i *DefaultItem) GetValue(key string) (interface{}, error) {
value, isExist := i.Store[key]
if !isExist {
return "", KeyNotContainError
}
return value, nil
}
func (i *DefaultItem) SetValue(key string, value interface{}) {
i.Store[key] = value
}
func (i *DefaultItem) GetString(key string) (string, error) {
rawValue, err := i.GetValue(key)
if err != nil {
return "", err
}
value, ok := rawValue.(string)
if !ok {
return "", TypeError
}
return value, nil
}
func (i *DefaultItem) GetInt(key string) (int, error) {
rawValue, err := i.GetValue(key)
if err != nil {
return 0, err
}
value, ok := rawValue.(int)
if !ok {
return 0, TypeError
}
return value, nil
}
func (i *DefaultItem) GetFloat64(key string) (float64, error) {
rawValue, err := i.GetValue(key)
if err != nil {
return 0, err
}
value, ok := rawValue.(float64)
if !ok {
return 0, TypeError
}
return value, nil
}