-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_codec.go
executable file
·78 lines (66 loc) · 1.93 KB
/
map_codec.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
package campid
import (
"io"
"github.com/influx6/npkg/nerror"
)
type MapCodec interface {
Decode(r io.Reader) (map[string]string, error)
Encode(w io.Writer, c map[string]string) error
}
// MsgPackMapCodec implements the MapCodec interface for using
// the MsgPack Codec.
type MsgPackMapCodec struct {
Codec MsgPackCodec
}
// Encode encodes giving session using the internal MsgPack format.
// Returning provided data.
func (gb *MsgPackMapCodec) Encode(w io.Writer, s map[string]string) error {
return gb.Codec.Encode(w, s)
}
// Decode decodes giving data into provided session instance.
func (gb *MsgPackMapCodec) Decode(r io.Reader) (map[string]string, error) {
var s map[string]string
var err = gb.Codec.Decode(r, &s)
if err != nil {
return s, nerror.WrapOnly(err)
}
return s, nil
}
// JsonMapCodec implements the MapCodec interface for using
// the Json Codec.
type JsonMapCodec struct {
Codec JsonCodec
}
// Encode encodes giving session using the internal Json format.
// Returning provided data.
func (gb *JsonMapCodec) Encode(w io.Writer, s map[string]string) error {
return gb.Codec.Encode(w, s)
}
// Decode decodes giving data into provided session instance.
func (gb *JsonMapCodec) Decode(r io.Reader) (map[string]string, error) {
var s map[string]string
var err = gb.Codec.Decode(r, &s)
if err != nil {
return s, nerror.WrapOnly(err)
}
return s, nil
}
// GobMapCodec implements the MapCodec interface for using
// the gob Codec.
type GobMapCodec struct {
Codec GobCodec
}
// Encode encodes giving session using the internal gob format.
// Returning provided data.
func (gb *GobMapCodec) Encode(w io.Writer, s map[string]string) error {
return gb.Codec.Encode(w, s)
}
// Decode decodes giving data into provided session instance.
func (gb *GobMapCodec) Decode(r io.Reader) (map[string]string, error) {
var s map[string]string
var err = gb.Codec.Decode(r, &s)
if err != nil {
return s, nerror.WrapOnly(err)
}
return s, nil
}