This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
forked from mojura/enkodo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
127 lines (104 loc) · 2.29 KB
/
decoder.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
package enkodo
import (
"bufio"
"io"
)
func newDecoder(r io.Reader) *Decoder {
var (
d Decoder
ok bool
)
if d.r, ok = r.(reader); !ok {
d.r = bufio.NewReader(r)
}
return &d
}
// Decoder helps to Marshal data
type Decoder struct {
r reader
}
// Uint decodes a uint type
func (d *Decoder) Uint() (v uint, err error) {
var v64 uint64
if v64, err = d.Uint64(); err != nil {
return
}
v = uint(v64)
return
}
// Uint8 decodes a uint8 type
func (d *Decoder) Uint8() (v uint8, err error) {
v, err = decodeUint8(d.r)
return
}
// Uint16 decodes a uint16 type
func (d *Decoder) Uint16() (v uint16, err error) {
v, err = decodeUint16(d.r)
return
}
// Uint32 decodes a uint32 type
func (d *Decoder) Uint32() (v uint32, err error) {
v, err = decodeUint32(d.r)
return
}
// Uint64 decodes a uint64 type
func (d *Decoder) Uint64() (v uint64, err error) {
v, err = decodeUint64(d.r)
return
}
// Int decodes an int type
func (d *Decoder) Int() (v int, err error) {
v, err = decodeInt(d.r)
return
}
// Int8 decodes an int8 type
func (d *Decoder) Int8() (v int8, err error) {
v, err = decodeInt8(d.r)
return
}
// Int16 decodes an int16 type
func (d *Decoder) Int16() (v int16, err error) {
v, err = decodeInt16(d.r)
return
}
// Int32 decodes an int32 type
func (d *Decoder) Int32() (v int32, err error) {
v, err = decodeInt32(d.r)
return
}
// Int64 decodes an int64 type
func (d *Decoder) Int64() (v int64, err error) {
v, err = decodeInt64(d.r)
return
}
// Float32 decodes a float64 type
func (d *Decoder) Float32() (v float32, err error) {
v, err = decodeFloat32(d.r)
return
}
// Float64 decodes a float64 type
func (d *Decoder) Float64() (v float64, err error) {
v, err = decodeFloat64(d.r)
return
}
// Bool will return a decoded boolean value
func (d *Decoder) Bool() (v bool, err error) {
v, err = decodeBool(d.r)
return
}
// Bytes will append bytes to the inbound byteslice
func (d *Decoder) Bytes(in *[]byte) (err error) {
return decodeBytes(d.r, in)
}
// String will return a decoded string
func (d *Decoder) String() (str string, err error) {
return decodeString(d.r)
}
// Decode will decode a decodee
func (d *Decoder) Decode(v Decodee) (err error) {
return v.UnmarshalEnkodo(d)
}
// Decodee is a data structure to be dedoded
type Decodee interface {
UnmarshalEnkodo(*Decoder) error
}