forked from stackus/edat-msgpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgpack_marshaler.go
37 lines (30 loc) · 921 Bytes
/
msgpack_marshaler.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
package msgpack
import (
"reflect"
"sync"
"github.com/shamaton/msgpack"
"github.com/stackus/edat/core"
"github.com/stackus/edat/core/register_types"
)
func init() {
core.RegisterDefaultMarshaller(newMsgPackMarshaller())
registertypes.RegisterTypes()
}
type msgPackMarshaler struct {
items map[string]reflect.Type
mu sync.Mutex
}
func newMsgPackMarshaller() *msgPackMarshaler {
return &msgPackMarshaler{
items: map[string]reflect.Type{},
mu: sync.Mutex{},
}
}
func (*msgPackMarshaler) Marshal(v interface{}) ([]byte, error) { return msgpack.Encode(v) }
func (*msgPackMarshaler) Unmarshal(data []byte, v interface{}) error { return msgpack.Decode(data, v) }
func (m *msgPackMarshaler) GetType(typeName string) reflect.Type { return m.items[typeName] }
func (m *msgPackMarshaler) RegisterType(typeName string, v reflect.Type) {
m.mu.Lock()
defer m.mu.Unlock()
m.items[typeName] = v
}