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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
package binstruct
import (
"encoding/binary"
"fmt"
"reflect"
)
type Marshaler interface {
MarshalBinary() []byte
UnmarshalBinary([]byte)
BinarySize() int64
}
type handler interface {
Unmarshal(dat []byte) interface{}
Marshal(val interface{}) []byte
Size() int64
}
type extHandler struct {
typ reflect.Type
}
func (_ extHandler) Marshal(val interface{}) []byte {
return val.(Marshaler).MarshalBinary()
}
func (e extHandler) Unmarshal(dat []byte) interface{} {
val := reflect.New(e.typ).Elem().Interface().(Marshaler)
val.UnmarshalBinary(dat)
return val
}
func (e extHandler) Size() int64 {
val := reflect.New(e.typ).Elem().Interface().(Marshaler)
return val.BinarySize()
}
type primitive struct {
unmarshal func(dat []byte) interface{}
marshal func(val interface{}) []byte
size int64
}
func (p primitive) Unmarshal(dat []byte) interface{} { return p.unmarshal(dat) }
func (p primitive) Marshal(val interface{}) []byte { return p.marshal(val) }
func (p primitive) Size() int64 { return p.size }
var _ handler = primitive{}
func convert[T any](in interface{}) T {
var dstTyp T
return reflect.ValueOf(in).Convert(reflect.TypeOf(dstTyp)).Interface().(T)
}
func genHandler(typ reflect.Type) (handler, error) {
if _, ok := reflect.New(typ).Elem().Interface().(Marshaler); ok {
return extHandler{
typ: typ,
}, nil
}
switch typ.Kind() {
case reflect.Invalid: // invalid
return nil, fmt.Errorf("unsupported kind: %s: %v", typ.Kind(), typ)
case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128: // I don't wanna
return nil, fmt.Errorf("unsupported kind: %s: %v", typ.Kind(), typ)
case reflect.Int, reflect.Uint, reflect.Uintptr: // platform specific
return nil, fmt.Errorf("unsupported kind: %s: %v", typ.Kind(), typ)
case reflect.Chan, reflect.Func, reflect.Interface, reflect.UnsafePointer: // runtime
return nil, fmt.Errorf("unsupported kind: %s: %v", typ.Kind(), typ)
case reflect.Map, reflect.Slice, reflect.String: // dynamic size
return nil, fmt.Errorf("unsupported kind: %s: %v", typ.Kind(), typ)
// uint ////////////////////////////////////////////////////////////////
case reflect.Uint8:
return primitive{
unmarshal: func(dat []byte) interface{} { return dat[0] },
marshal: func(val interface{}) []byte { return []byte{convert[uint8](val)} },
size: 1,
}, nil
case reflect.Uint16:
return primitive{
unmarshal: func(dat []byte) interface{} { return binary.LittleEndian.Uint16(dat) },
marshal: func(val interface{}) []byte {
var buf [2]byte
binary.LittleEndian.PutUint16(buf[:], convert[uint16](val))
return buf[:]
},
size: 2,
}, nil
case reflect.Uint32:
return primitive{
unmarshal: func(dat []byte) interface{} { return binary.LittleEndian.Uint32(dat) },
marshal: func(val interface{}) []byte {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], convert[uint32](val))
return buf[:]
},
size: 4,
}, nil
case reflect.Uint64:
return primitive{
unmarshal: func(dat []byte) interface{} { return binary.LittleEndian.Uint64(dat) },
marshal: func(val interface{}) []byte {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], convert[uint64](val))
return buf[:]
},
size: 8,
}, nil
// int /////////////////////////////////////////////////////////////////
case reflect.Int8:
return primitive{
unmarshal: func(dat []byte) interface{} { return int8(dat[0]) },
marshal: func(val interface{}) []byte { return []byte{uint8(convert[int8](val))}},
size: 1,
}, nil
case reflect.Int16:
return primitive{
unmarshal: func(dat []byte) interface{} { return int16(binary.LittleEndian.Uint16(dat)) },
marshal: func(val interface{}) []byte {
var buf [2]byte
binary.LittleEndian.PutUint16(buf[:], uint16(convert[int16](val)))
return buf[:]
},
size: 2,
}, nil
case reflect.Int32:
return primitive{
unmarshal: func(dat []byte) interface{} { return int32(binary.LittleEndian.Uint32(dat)) },
marshal: func(val interface{}) []byte {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], uint32(convert[int32](val)))
return buf[:]
},
size: 4,
}, nil
case reflect.Int64:
return primitive{
unmarshal: func(dat []byte) interface{} { return int64(binary.LittleEndian.Uint64(dat)) },
marshal: func(val interface{}) []byte {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], uint64(convert[int64](val)))
return buf[:]
},
size: 8,
}, nil
// composite ///////////////////////////////////////////////////////////
case reflect.Ptr:
inner, err := getHandler(typ.Elem())
if err != nil {
return nil, fmt.Errorf("%v: %w", typ, err)
}
return primitive{
unmarshal: func(dat []byte) interface{} {
return reflect.ValueOf(inner.Unmarshal(dat)).Addr().Interface()
},
marshal: func(val interface{}) []byte {
return inner.Marshal(reflect.ValueOf(val).Elem().Interface())
},
size: inner.Size(),
}, nil
case reflect.Array:
inner, err := getHandler(typ.Elem())
if err != nil {
return nil, fmt.Errorf("%v: %w", typ, err)
}
return primitive{
unmarshal: func(dat []byte) interface{} {
val := reflect.New(typ).Elem()
for i := 0; i < typ.Len(); i++ {
fieldVal := inner.Unmarshal(dat[i*int(inner.Size()):])
val.Index(i).Set(reflect.ValueOf(fieldVal).Convert(typ.Elem()))
}
return val.Interface()
},
marshal: func(val interface{}) []byte {
_val := reflect.ValueOf(val)
var ret []byte
for i := 0; i < typ.Len(); i++ {
ret = append(ret, inner.Marshal(_val.Index(i).Interface())...)
}
return ret
},
size: inner.Size() * int64(typ.Len()),
}, nil
case reflect.Struct:
return genStructHandler(typ)
// end /////////////////////////////////////////////////////////////////
default:
panic(fmt.Errorf("unknown kind: %v: %v", typ.Kind(), typ))
}
}
|