-
Notifications
You must be signed in to change notification settings - Fork 3
/
public.go
36 lines (28 loc) · 890 Bytes
/
public.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
package ikea
import (
"io"
"reflect"
)
// Unpack will read exactly enough bytes from the specified Reader in order to fill the value passed to data.
// if data is not a pointer Unpack will panic
func Unpack(r io.Reader, data interface{}) error {
pv := reflect.ValueOf(data)
if pv.Kind() != reflect.Ptr {
panic("passed data argument is not a pointer")
}
v := pv.Elem()
h := getTypeHandler(v.Type())
return handleVariableReader(r, h, v)
}
// Pack will write the value passed in data to the specified Writer
func Pack(w io.Writer, data interface{}) error {
v := reflect.Indirect(reflect.ValueOf(data))
h := getTypeHandler(v.Type())
return handleVariableWriter(w, h, v)
}
// Len will return the amount of bytes Pack will use.
func Len(data interface{}) int {
v := reflect.Indirect(reflect.ValueOf(data))
h := getTypeHandler(v.Type())
return handleVariableLength(h, v)
}