forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_msgpack.go
187 lines (159 loc) · 5.23 KB
/
codec_msgpack.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
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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfscodec
import (
"fmt"
"reflect"
"github.com/keybase/go-codec/codec"
"github.com/pkg/errors"
)
// ext is a no-op extension that's useful for tagging interfaces with
// a type. Note that it cannot be used for anything that has nested
// extensions.
type ext struct {
// codec should NOT encode extension types
codec Codec
}
// ConvertExt implements the codec.Ext interface for ext.
func (e ext) ConvertExt(v interface{}) interface{} {
panic("ConvertExt not supported")
}
// UpdateExt implements the codec.Ext interface for ext.
func (e ext) UpdateExt(dest interface{}, v interface{}) {
panic("UpdateExt not supported")
}
// WriteExt implements the codec.Ext interface for ext.
func (e ext) WriteExt(v interface{}) (buf []byte) {
buf, err := e.codec.Encode(v)
if err != nil {
panic(fmt.Sprintf("Couldn't encode data in %v", v))
}
return buf
}
// ReadExt implements the codec.Ext interface for ext.
func (e ext) ReadExt(v interface{}, buf []byte) {
err := e.codec.Decode(buf, v)
if err != nil {
panic(fmt.Sprintf("Couldn't decode data into %v", v))
}
}
// extSlice is an extension that's useful for slices that contain
// extension types as elements. The contained extension types cannot
// themselves contain nested extension types.
type extSlice struct {
// codec SHOULD encode extension types
codec Codec
typer func(interface{}) reflect.Value
}
// ConvertExt implements the codec.Ext interface for extSlice.
func (es extSlice) ConvertExt(v interface{}) interface{} {
panic("ConvertExt not supported")
}
// UpdateExt implements the codec.Ext interface for extSlice.
func (es extSlice) UpdateExt(dest interface{}, v interface{}) {
panic("UpdateExt not supported")
}
// WriteExt implements the codec.Ext interface for extSlice.
func (es extSlice) WriteExt(v interface{}) (buf []byte) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Slice {
panic(fmt.Sprintf("Non-slice passed to extSlice.WriteExt %v",
val.Kind()))
}
ifaceArray := make([]interface{}, val.Len())
for i := 0; i < val.Len(); i++ {
ifaceArray[i] = val.Index(i).Interface()
}
buf, err := es.codec.Encode(ifaceArray)
if err != nil {
panic(fmt.Sprintf("Couldn't encode data in %v", v))
}
return buf
}
// ReadExt implements the codec.Ext interface for extSlice.
func (es extSlice) ReadExt(v interface{}, buf []byte) {
// ReadExt actually receives a pointer to the list
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
panic(fmt.Sprintf("Non-pointer passed to extSlice.ReadExt: %v",
val.Kind()))
}
val = val.Elem()
if val.Kind() != reflect.Slice {
panic(fmt.Sprintf("Non-slice passed to extSlice.ReadExt %v",
val.Kind()))
}
var ifaceArray []interface{}
err := es.codec.Decode(buf, &ifaceArray)
if err != nil {
panic(fmt.Sprintf("Couldn't decode data into %v", v))
}
if len(ifaceArray) > 0 {
val.Set(reflect.MakeSlice(val.Type(), len(ifaceArray),
len(ifaceArray)))
}
for i, v := range ifaceArray {
if es.typer != nil {
val.Index(i).Set(es.typer(v))
} else {
val.Index(i).Set(reflect.ValueOf(v))
}
}
}
// CodecMsgpack implements the Codec interface using msgpack
// marshaling and unmarshaling.
type CodecMsgpack struct {
h codec.Handle
ExtCodec *CodecMsgpack
}
// newCodecMsgpackHelper constructs a new CodecMsgpack that may or may
// not handle unknown fields.
func newCodecMsgpackHelper(handleUnknownFields bool) *CodecMsgpack {
handle := codec.MsgpackHandle{}
handle.Canonical = true
handle.WriteExt = true
handle.DecodeUnknownFields = handleUnknownFields
handle.EncodeUnknownFields = handleUnknownFields
// save a codec that doesn't write extensions, so that we can just
// call Encode/Decode when we want to (de)serialize extension
// types.
handleNoExt := handle
handleNoExt.WriteExt = false
ExtCodec := &CodecMsgpack{&handleNoExt, nil}
return &CodecMsgpack{&handle, ExtCodec}
}
// NewMsgpack constructs a new CodecMsgpack.
func NewMsgpack() *CodecMsgpack {
return newCodecMsgpackHelper(true)
}
// NewMsgpackNoUnknownFields constructs a new CodecMsgpack that
// doesn't handle unknown fields.
func NewMsgpackNoUnknownFields() *CodecMsgpack {
return newCodecMsgpackHelper(false)
}
// Decode implements the Codec interface for CodecMsgpack
func (c *CodecMsgpack) Decode(buf []byte, obj interface{}) error {
err := codec.NewDecoderBytes(buf, c.h).Decode(obj)
if err != nil {
return errors.Wrap(err, "failed to decode")
}
return nil
}
// Encode implements the Codec interface for CodecMsgpack
func (c *CodecMsgpack) Encode(obj interface{}) (buf []byte, err error) {
err = codec.NewEncoderBytes(&buf, c.h).Encode(obj)
if err != nil {
return nil, errors.Wrap(err, "failed to encode")
}
return buf, nil
}
// RegisterType implements the Codec interface for CodecMsgpack
func (c *CodecMsgpack) RegisterType(rt reflect.Type, code ExtCode) {
c.h.(*codec.MsgpackHandle).SetExt(rt, uint64(code), ext{c.ExtCodec})
}
// RegisterIfaceSliceType implements the Codec interface for CodecMsgpack
func (c *CodecMsgpack) RegisterIfaceSliceType(rt reflect.Type, code ExtCode,
typer func(interface{}) reflect.Value) {
c.h.(*codec.MsgpackHandle).SetExt(rt, uint64(code), extSlice{c, typer})
}