-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplenc.go
73 lines (62 loc) · 3.19 KB
/
plenc.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
package plenc
import (
"reflect"
"time"
"github.com/philpearl/plenc/plenccodec"
)
// Plenc is an instance of the plenc encoding and decoding system. It contains
// a registry of plenc codecs. Use it if you need fine-grain control on plenc's
// behaviour.
//
// var p plenc.Plenc
// p.RegisterDefaultCodecs()
// data, err := p.Marshal(nil, mystruct)
type Plenc struct {
// Plenc's original time handling was not compatible with protobuf's
// google.protobuf.Timestamp. Set this to true to enable a proto compatible
// time codec. Set it before calling RegisterDefaultCodecs.
ProtoCompatibleTime bool
// ProtoCompatibleArrays controls how plenc handles slices and arrays of
// data. When set to true Plenc writes arrays that are compatible with
// protobuf. If not true it uses a format that allows arrays to be read more
// efficiently. Set it before calling RegisterDefaultCodecs.
ProtoCompatibleArrays bool
codecRegistry baseRegistry
}
func (p *Plenc) RegisterCodec(typ reflect.Type, c plenccodec.Codec) {
p.codecRegistry.Store(typ, "", c)
}
func (p *Plenc) RegisterCodecWithTag(typ reflect.Type, tag string, c plenccodec.Codec) {
p.codecRegistry.Store(typ, tag, c)
}
// RegisterDefaultCodecs sets up the default codecs for plenc. It is called
// automatically for the default plenc instance, but if you create your own
// instance of Plenc you should call this before using it.
func (p *Plenc) RegisterDefaultCodecs() {
p.RegisterCodec(reflect.TypeOf(false), plenccodec.BoolCodec{})
p.RegisterCodec(reflect.TypeOf(float64(0)), plenccodec.Float64Codec{})
p.RegisterCodec(reflect.TypeOf(float32(0)), plenccodec.Float32Codec{})
p.RegisterCodec(reflect.TypeOf(int(0)), plenccodec.IntCodec[int]{})
p.RegisterCodec(reflect.TypeOf(int8(0)), plenccodec.IntCodec[int8]{})
p.RegisterCodec(reflect.TypeOf(int16(0)), plenccodec.IntCodec[int16]{})
p.RegisterCodec(reflect.TypeOf(int32(0)), plenccodec.IntCodec[int32]{})
p.RegisterCodec(reflect.TypeOf(int64(0)), plenccodec.IntCodec[int64]{})
p.RegisterCodecWithTag(reflect.TypeOf(int(0)), "flat", plenccodec.FlatIntCodec[uint]{})
p.RegisterCodecWithTag(reflect.TypeOf(int8(0)), "flat", plenccodec.FlatIntCodec[uint8]{})
p.RegisterCodecWithTag(reflect.TypeOf(int16(0)), "flat", plenccodec.FlatIntCodec[uint16]{})
p.RegisterCodecWithTag(reflect.TypeOf(int32(0)), "flat", plenccodec.FlatIntCodec[uint32]{})
p.RegisterCodecWithTag(reflect.TypeOf(int64(0)), "flat", plenccodec.FlatIntCodec[uint64]{})
p.RegisterCodec(reflect.TypeOf(uint(0)), plenccodec.UintCodec[uint]{})
p.RegisterCodec(reflect.TypeOf(uint64(0)), plenccodec.UintCodec[uint64]{})
p.RegisterCodec(reflect.TypeOf(uint32(0)), plenccodec.UintCodec[uint32]{})
p.RegisterCodec(reflect.TypeOf(uint16(0)), plenccodec.UintCodec[uint16]{})
p.RegisterCodec(reflect.TypeOf(uint8(0)), plenccodec.UintCodec[uint8]{})
p.RegisterCodec(reflect.TypeOf(""), plenccodec.StringCodec{})
p.RegisterCodecWithTag(reflect.TypeOf(""), "intern", &plenccodec.InternedStringCodec{})
p.RegisterCodec(reflect.TypeOf([]byte(nil)), plenccodec.BytesCodec{})
if p.ProtoCompatibleTime {
p.RegisterCodec(reflect.TypeOf(time.Time{}), plenccodec.TimeCompatCodec{})
} else {
p.RegisterCodec(reflect.TypeOf(time.Time{}), plenccodec.TimeCodec{})
}
}