-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtt.go
74 lines (62 loc) · 1.68 KB
/
tt.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
package tt
import (
"bytes"
"errors"
v2 "github.com/jaicewizard/tt/v2"
)
type (
//Data is the type
Data map[interface{}]interface{}
//Key is the key used for storing the key of a Value
Key struct {
Value []byte //the key to the data of this object in its final form
Vtype byte //the type of the data of this object in its final form
}
ikeytype uint32
valuelen uint32
keylen uint32
//Transmitter transmits the data
Transmitter interface {
Encode() ([]byte, error)
Decode([]byte) (interface{}, error)
GetCode() byte
}
)
var (
//ErrCodeUsed is for when the code for the transmitter is already used
ErrCodeUsed = errors.New("code already used")
//ErrInvalidInput is used for when the input it invalid
ErrInvalidInput = errors.New("invalid input")
transmitters = make(map[byte]Transmitter)
)
//RegisterTransmitter registers a new transmitter
func RegisterTransmitter(tr Transmitter) error {
code := tr.GetCode()
if code == v2.StringT || code == v2.FinalValueT || code == v2.MapT || code == v2.ArrT {
return ErrCodeUsed
}
transmitters[code] = tr
return nil
}
//GobEncode encodes the data of a map[interface{}]interface{}
func (d Data) GobEncode() ([]byte, error) {
var byt []byte
buf := bytes.NewBuffer(byt)
Encodev3((map[interface{}]interface{})(d), buf)
return buf.Bytes(), nil
}
//GobDecode decodes the data back into a map[interface{}]interface{}
func (d *Data) GobDecode(data []byte) error {
if len(data) == 0 {
return ErrInvalidInput
}
switch data[0] {
case version1:
return Decodev1(data[1:], d)
case version2:
return Decodev2(data[1:], d)
case version3:
return Decodev3(bytes.NewBuffer(data), (*map[interface{}]interface{})(d))
}
return nil
}