-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv.go
44 lines (35 loc) · 1.31 KB
/
csv.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
package encoders
import (
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/jszwec/csvutil"
"io"
)
var _ runtime.Marshaler = (*csvEncoder)(nil)
// csvEncoder implements Marshaller for the CSV format.
type csvEncoder struct {
}
// NewDecoder returns a Decoder which reads byte sequence from "r".
func (e csvEncoder) NewDecoder(r io.Reader) runtime.Decoder {
return newDecoderFunc(r, e)
}
// NewEncoder returns an Encoder which writes bytes sequence into "w".
func (e csvEncoder) NewEncoder(w io.Writer) runtime.Encoder {
return newEncoderFunc(w, e)
}
// ContentType returns the Content-Type which this marshaler is responsible for.
// The parameter describes the type which is being marshalled, which can sometimes
// affect the content type returned.
func (e csvEncoder) ContentType(v interface{}) string {
return "text/csv"
}
// Marshal returns the CSV encoding of v.
func (csvEncoder) Marshal(v interface{}) ([]byte, error) {
return csvutil.Marshal(v)
}
// Unmarshal parses the CSV-encoded data and stores the results in the value pointed to by v.
// NOTE: Given the nature of CSV files, v must be a non-nil pointer to a slice.
func (csvEncoder) Unmarshal(data []byte, v interface{}) error {
return csvutil.Unmarshal(data, v)
}
// CSV holds a csv encoder instance implementing Marshaller.
var CSV = &csvEncoder{}