-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.go
148 lines (123 loc) · 3.32 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gocache
import (
"errors"
"strconv"
"time"
"github.com/alejandro-carstens/gocache/encoder"
)
type (
// Entry represents a cache entry or input
Entry struct {
Key string
Value interface{}
Duration time.Duration
}
// Item represents a retrieved entry from the cache
Item struct {
key string
value string
tagKey string
err error
encoder encoder.Encoder
}
// Items is an Item map keyed by the Item key
Items map[string]Item
)
// Key returns an Item's key
func (i Item) Key() string {
return i.key
}
// TagKey returns the actual key of an Item if it was retrieved with a tag
func (i Item) TagKey() string {
return i.tagKey
}
// String returns the string representation of an Item's value
func (i Item) String() (string, error) {
if i.err != nil {
return "", ErrFailedToRetrieveEntry
}
if isStringNumeric(i.value) || isStringBool(i.value) {
return i.value, nil
}
var v = i.value
if err := i.encoder.Decode([]byte(v), &v); err != nil {
return "", err
}
return v, nil
}
// Uint64 returns the uint64 representation of an Item's value
func (i Item) Uint64() (uint64, error) {
if i.err != nil {
return 0, ErrFailedToRetrieveEntry
}
if !isInterfaceNumericString(i.value) && !isNumeric(i.value) {
return 0, errors.New("invalid numeric value")
}
return strconv.ParseUint(i.value, 10, 64)
}
// Int returns the int representation of an Item's value
func (i Item) Int() (int, error) {
if i.err != nil {
return 0, ErrFailedToRetrieveEntry
}
if !isInterfaceNumericString(i.value) && !isNumeric(i.value) {
return 0, errors.New("invalid numeric value")
}
return strconv.Atoi(i.value)
}
// Bool returns the boolean representation of an Item's value
func (i Item) Bool() (bool, error) {
if i.err != nil {
return false, ErrFailedToRetrieveEntry
}
return stringToBool(i.value), nil
}
// Int64 returns the int64 representation of an Item's value
func (i Item) Int64() (int64, error) {
if i.err != nil {
return 0, ErrFailedToRetrieveEntry
}
if !isInterfaceNumericString(i.value) && !isNumeric(i.value) {
return 0, errors.New("invalid numeric value")
}
return strconv.ParseInt(i.value, 10, 64)
}
// Float32 returns the float32 representation of an Item's value
func (i Item) Float32() (float32, error) {
if i.err != nil {
return 0, ErrFailedToRetrieveEntry
}
if !isInterfaceNumericString(i.value) && !isNumeric(i.value) {
return 0, errors.New("invalid numeric value")
}
f, err := strconv.ParseFloat(i.value, 32)
if err != nil {
return 0, err
}
return float32(f), nil
}
// Float64 returns the float32 representation of an Item's value
func (i Item) Float64() (float64, error) {
if i.err != nil {
return 0, ErrFailedToRetrieveEntry
}
if !isInterfaceNumericString(i.value) && !isNumeric(i.value) {
return 0, errors.New("invalid numeric value")
}
return strconv.ParseFloat(i.value, 64)
}
// Unmarshal decodes an Item's value to the provided entity
func (i Item) Unmarshal(entity interface{}) error {
if i.err != nil {
return ErrFailedToRetrieveEntry
}
return i.encoder.Decode([]byte(i.value), entity)
}
// Error returns the error that occurred when trying to retrieve a given Item
func (i Item) Error() error {
return i.err
}
// EntryNotFound checks if an entry was retrieved for the given key
func (i Item) EntryNotFound() bool {
return errors.Is(i.err, ErrNotFound)
}