Repository navigation
Expand file tree
/
Copy pathsimple_string.go
More file actions
155 lines (109 loc) · 2.44 KB
/
Copy pathsimple_string.go
File metadata and controls
155 lines (109 loc) · 2.44 KB
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
149
150
151
152
153
154
155
package goresp
import (
"errors"
"strconv"
"time"
)
type SimpleString struct {
internal string
}
func NewSimpleString(s string) *SimpleString {
return &SimpleString{
internal: s,
}
}
func (ss *SimpleString) Encode() ([]byte, error) {
b, err := ss.Bytes()
if err != nil {
return nil, err
}
return append(append([]byte{'+'}, b...), '\r', '\n'), nil
}
func (ss *SimpleString) Decode(bs []byte) error {
_, v, e := decodeSimpleString(0, bs)
if e != nil {
return e
}
ss.internal = v.internal
return nil
}
func (ss *SimpleString) Bool() (bool, error) {
return false, errors.New("cannot convert simple string to bool")
}
func (ss *SimpleString) Bytes() ([]byte, error) {
str, err := ss.String()
return []byte(str), err
}
func (ss *SimpleString) String() (string, error) {
return ss.internal, nil
}
func (ss *SimpleString) Uint() (uint, error) {
i, e := ss.Int()
return uint(i), e
}
func (ss *SimpleString) Uint8() (uint8, error) {
i, e := ss.Int()
return uint8(i), e
}
func (ss *SimpleString) Uint16() (uint16, error) {
i, e := ss.Int()
return uint16(i), e
}
func (ss *SimpleString) Uint32() (uint32, error) {
i, e := ss.Int()
return uint32(i), e
}
func (ss *SimpleString) Uint64() (uint64, error) {
i, e := ss.Int()
return uint64(i), e
}
func (ss *SimpleString) Int() (int, error) {
str, err := ss.String()
if err != nil {
return 0, err
}
return strconv.Atoi(str)
}
func (ss *SimpleString) Int32() (int32, error) {
x, e := ss.Int()
return int32(x), e
}
func (ss *SimpleString) Int64() (int64, error) {
x, e := ss.Int()
return int64(x), e
}
func (ss *SimpleString) Float32() (float32, error) {
f64, e := ss.Float64()
return float32(f64), e
}
func (ss *SimpleString) Float64() (float64, error) {
str, err := ss.String()
if err != nil {
return 0, err
}
return strconv.ParseFloat(str, 64)
}
func (ss *SimpleString) Error() (error, error) {
str, err := ss.String()
return errors.New(str), err
}
func (ss *SimpleString) Array() ([]Value, error) {
return nil, errors.New("cannot convert simple string to array")
}
func (ss *SimpleString) Time(l string) (time.Time, error) {
s, e := ss.String()
if e != nil {
return time.Now(), e
}
return time.Parse(l, s)
}
func (ss *SimpleString) Duration(d time.Duration) (time.Duration, error) {
s, e := ss.String()
if e != nil {
return d, e
}
return time.ParseDuration(s)
}
func (ss *SimpleString) Null() error {
return errors.New("simple string is not null")
}