github.com/philpearl/plenc@v0.0.15/plenc.go (about) 1 package plenc 2 3 import ( 4 "reflect" 5 "time" 6 7 "github.com/philpearl/plenc/plenccodec" 8 ) 9 10 // Plenc is an instance of the plenc encoding and decoding system. It contains 11 // a registry of plenc codecs. Use it if you need fine-grain control on plenc's 12 // behaviour. 13 // 14 // var p plenc.Plenc 15 // p.RegisterDefaultCodecs() 16 // data, err := p.Marshal(nil, mystruct) 17 type Plenc struct { 18 // Plenc's original time handling was not compatible with protobuf's 19 // google.protobuf.Timestamp. Set this to true to enable a proto compatible 20 // time codec. Set it before calling RegisterDefaultCodecs. 21 ProtoCompatibleTime bool 22 // ProtoCompatibleArrays controls how plenc handles slices and arrays of 23 // data. When set to true Plenc writes arrays that are compatible with 24 // protobuf. If not true it uses a format that allows arrays to be read more 25 // efficiently. Set it before calling RegisterDefaultCodecs. 26 ProtoCompatibleArrays bool 27 28 codecRegistry baseRegistry 29 } 30 31 func (p *Plenc) RegisterCodec(typ reflect.Type, c plenccodec.Codec) { 32 p.codecRegistry.Store(typ, "", c) 33 } 34 35 func (p *Plenc) RegisterCodecWithTag(typ reflect.Type, tag string, c plenccodec.Codec) { 36 p.codecRegistry.Store(typ, tag, c) 37 } 38 39 // RegisterDefaultCodecs sets up the default codecs for plenc. It is called 40 // automatically for the default plenc instance, but if you create your own 41 // instance of Plenc you should call this before using it. 42 func (p *Plenc) RegisterDefaultCodecs() { 43 p.RegisterCodec(reflect.TypeOf(false), plenccodec.BoolCodec{}) 44 45 p.RegisterCodec(reflect.TypeOf(float64(0)), plenccodec.Float64Codec{}) 46 p.RegisterCodec(reflect.TypeOf(float32(0)), plenccodec.Float32Codec{}) 47 48 p.RegisterCodec(reflect.TypeOf(int(0)), plenccodec.IntCodec[int]{}) 49 p.RegisterCodec(reflect.TypeOf(int8(0)), plenccodec.IntCodec[int8]{}) 50 p.RegisterCodec(reflect.TypeOf(int16(0)), plenccodec.IntCodec[int16]{}) 51 p.RegisterCodec(reflect.TypeOf(int32(0)), plenccodec.IntCodec[int32]{}) 52 p.RegisterCodec(reflect.TypeOf(int64(0)), plenccodec.IntCodec[int64]{}) 53 54 p.RegisterCodecWithTag(reflect.TypeOf(int(0)), "flat", plenccodec.FlatIntCodec[uint]{}) 55 p.RegisterCodecWithTag(reflect.TypeOf(int8(0)), "flat", plenccodec.FlatIntCodec[uint8]{}) 56 p.RegisterCodecWithTag(reflect.TypeOf(int16(0)), "flat", plenccodec.FlatIntCodec[uint16]{}) 57 p.RegisterCodecWithTag(reflect.TypeOf(int32(0)), "flat", plenccodec.FlatIntCodec[uint32]{}) 58 p.RegisterCodecWithTag(reflect.TypeOf(int64(0)), "flat", plenccodec.FlatIntCodec[uint64]{}) 59 60 p.RegisterCodec(reflect.TypeOf(uint(0)), plenccodec.UintCodec[uint]{}) 61 p.RegisterCodec(reflect.TypeOf(uint64(0)), plenccodec.UintCodec[uint64]{}) 62 p.RegisterCodec(reflect.TypeOf(uint32(0)), plenccodec.UintCodec[uint32]{}) 63 p.RegisterCodec(reflect.TypeOf(uint16(0)), plenccodec.UintCodec[uint16]{}) 64 p.RegisterCodec(reflect.TypeOf(uint8(0)), plenccodec.UintCodec[uint8]{}) 65 p.RegisterCodec(reflect.TypeOf(""), plenccodec.StringCodec{}) 66 p.RegisterCodecWithTag(reflect.TypeOf(""), "intern", &plenccodec.InternedStringCodec{}) 67 p.RegisterCodec(reflect.TypeOf([]byte(nil)), plenccodec.BytesCodec{}) 68 if p.ProtoCompatibleTime { 69 p.RegisterCodec(reflect.TypeOf(time.Time{}), plenccodec.TimeCompatCodec{}) 70 } else { 71 p.RegisterCodec(reflect.TypeOf(time.Time{}), plenccodec.TimeCodec{}) 72 } 73 }