forked from dromara/dongle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
46 lines (40 loc) · 780 Bytes
/
decode.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
package dongle
// decode defines a decode struct.
type decode struct {
dongle
}
// newDecode returns a new decode instance.
func newDecode() decode {
return decode{}
}
// FromString decodes from string.
func (d decode) FromString(s string) decode {
if s == "" {
return d
}
d.input = string2bytes(s)
return d
}
// FromBytes decodes from byte slice.
func (d decode) FromBytes(b []byte) decode {
if len(b) > 0 {
d.input = b
}
return d
}
// ToString decodes as string.
func (d decode) ToString() string {
output := d.output
if output == nil {
output = d.input
}
return bytes2string(output)
}
// ToBytes decodes as byte slice.
func (d decode) ToBytes() []byte {
input, output := d.input, d.output
if len(input) == 0 {
return emptyBytes
}
return output
}