/
hex.go
97 lines (78 loc) · 1.79 KB
/
hex.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
package formats
import (
"encoding/hex"
"fmt"
"github.com/corpix/reflect"
)
var (
stringerType = reflect.TypeOf(NewStringer(""))
)
// HEXFormat is a HEX marshaler.
type HEXFormat struct{}
func (f *HEXFormat) indirectToFstPtr(v interface{}) reflect.Value {
var (
rv = reflect.ValueOf(v)
)
for {
if rv.Kind() == reflect.Ptr && rv.Elem().Kind() == reflect.Ptr {
rv = rv.Elem()
continue
}
break
}
return rv
}
// Marshal serializes data represented by v into slice of bytes.
func (f *HEXFormat) Marshal(v interface{}) ([]byte, error) {
var (
rv = f.indirectToFstPtr(v)
vs fmt.Stringer
)
if rv.Type() != stringerType || !rv.IsValid() {
return nil, reflect.NewErrCanNotAssertType(
v,
stringerType,
)
}
vs = rv.Interface().(*Stringer)
var (
vv = []byte(vs.String())
buf = make([]byte, hex.EncodedLen(len(vv)))
)
hex.Encode(buf, vv)
return buf, nil
}
// Unmarshal deserializes data represented by data byte slice into v.
func (f *HEXFormat) Unmarshal(data []byte, v interface{}) error {
var (
buf = make([]byte, hex.DecodedLen(len(data)))
rv = f.indirectToFstPtr(v)
err error
)
if rv.Kind() != reflect.Ptr {
return reflect.NewErrWrongKind(reflect.Ptr, rv.Kind())
}
_, err = hex.Decode(buf, data)
if err != nil {
return err
}
if rv.Type() != stringerType {
if rv.Elem().Kind() == reflect.Interface {
rv.Elem().Set(reflect.ValueOf(*NewStringer(string(buf))))
return nil
}
return reflect.NewErrCanNotAssertType(
v,
stringerType,
)
}
rv.Set(reflect.ValueOf(NewStringer(string(buf))))
return nil
}
// Name returns a format name which is used to identify this format
// in the package.
func (f *HEXFormat) Name() string {
return HEX
}
// NewHEX constructs a new HEX format marshaler.
func NewHEX() *HEXFormat { return &HEXFormat{} }