-
Notifications
You must be signed in to change notification settings - Fork 3
/
format.go
144 lines (112 loc) · 3.36 KB
/
format.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
package feedx
import (
"encoding/json"
"errors"
"fmt"
"io"
"path"
"github.com/bsm/pbio"
"google.golang.org/protobuf/proto"
)
var errNoFormat = errors.New("feedx: no format detected")
// Format represents the data format.
type Format interface {
// NewDecoder wraps a decoder around a reader.
NewDecoder(io.Reader) (FormatDecoder, error)
// NewEncoder wraps an encoder around a writer.
NewEncoder(io.Writer) (FormatEncoder, error)
}
// DetectFormat detects the data format from a URL path or file name.
// May return nil.
func DetectFormat(name string) Format {
ext := path.Ext(path.Base(name))
switch ext {
case ".json":
return JSONFormat
case ".pb", ".proto", ".protobuf":
return ProtobufFormat
default:
if name != "" && ext != "" && ext[0] == '.' {
if ext[len(ext)-1] == 'z' {
return DetectFormat(name[0 : len(name)-1])
}
return DetectFormat(name[0 : len(name)-len(ext)])
}
}
return (*noFormat)(nil)
}
// FormatDecoder methods
type FormatDecoder interface {
// Decode decodes the next message into an interface.
Decode(v interface{}) error
io.Closer
}
// FormatEncoder methods
type FormatEncoder interface {
// Encode encodes the value to the stream.
Encode(v interface{}) error
io.Closer
}
// --------------------------------------------------------------------
type noFormat struct{}
func (*noFormat) NewDecoder(r io.Reader) (FormatDecoder, error) { return nil, errNoFormat }
func (*noFormat) NewEncoder(w io.Writer) (FormatEncoder, error) { return nil, errNoFormat }
// --------------------------------------------------------------------
// JSONFormat provides a Format implemention for JSON.
var JSONFormat = jsonFormat{}
type jsonFormat struct{}
// NewDecoder implements Format.
func (jsonFormat) NewDecoder(r io.Reader) (FormatDecoder, error) {
return jsonDecoderWrapper{Decoder: json.NewDecoder(r)}, nil
}
// NewEncoder implements Format.
func (jsonFormat) NewEncoder(w io.Writer) (FormatEncoder, error) {
return jsonEncoderWrapper{Encoder: json.NewEncoder(w)}, nil
}
type jsonDecoderWrapper struct{ *json.Decoder }
func (jsonDecoderWrapper) Close() error { return nil }
type jsonEncoderWrapper struct{ *json.Encoder }
func (jsonEncoderWrapper) Close() error { return nil }
// --------------------------------------------------------------------
// ProtobufFormat provides a Format implemention for Protobuf.
var ProtobufFormat = protobufFormat{}
type protobufFormat struct{}
// NewDecoder implements Format.
func (protobufFormat) NewDecoder(r io.Reader) (FormatDecoder, error) {
return &protobufWrapper{r: r}, nil
}
// NewEncoder implements Format.
func (protobufFormat) NewEncoder(w io.Writer) (FormatEncoder, error) {
return &protobufWrapper{w: w}, nil
}
type protobufWrapper struct {
r io.Reader
dec *pbio.Decoder
w io.Writer
enc *pbio.Encoder
}
func (w *protobufWrapper) Decode(v interface{}) error {
switch msg := v.(type) {
case proto.Message:
if w.dec == nil {
w.dec = pbio.NewDecoder(w.r)
}
return w.dec.Decode(msg)
default:
return fmt.Errorf("value %v (%T) is not a proto.Message", v, v)
}
}
func (w *protobufWrapper) Encode(v interface{}) error {
switch msg := v.(type) {
case proto.Message:
if w.enc == nil {
w.enc = pbio.NewEncoder(w.w)
}
return w.enc.Encode(msg)
default:
return fmt.Errorf("value %v (%T) is not a proto.Message", v, v)
}
}
func (*protobufWrapper) Close() error {
return nil
}