Add MarshalBytesWithoutHeaders

This commit is contained in:
dualfroz committed 2026-10-02 15:52:52 +02:00
1 parent 4d32abefad
commit 90a1c52508
2 files changed
+24

No files matched your search

+9
View File
@@ -149,6 +149,15 @@ func MarshalBytes(in interface{}) (out []byte, err error) {
return bufferString.Bytes(), nil return bufferString.Bytes(), nil
} }
// MarshalBytesWithoutHeaders returns the CSV bytes from the interface.
func MarshalBytesWithoutHeaders(in interface{}) (out []byte, err error) {
bufferString := bytes.NewBuffer(out)
if err := MarshalWithoutHeaders(in, bufferString); err != nil {
return nil, err
}
return bufferString.Bytes(), nil
}
// Marshal returns the CSV in writer from the interface. // Marshal returns the CSV in writer from the interface.
func Marshal(in interface{}, out io.Writer) (err error) { func Marshal(in interface{}, out io.Writer) (err error) {
writer := getCSVWriter(out) writer := getCSVWriter(out)
+15
View File
@@ -102,6 +102,21 @@ func Test_writeTo_NoHeaders(t *testing.T) {
assertLine(t, []string{"e", "3", "b", "0.46153846153846156", "", "", ""}, lines[1]) assertLine(t, []string{"e", "3", "b", "0.46153846153846156", "", "", ""}, lines[1])
} }
func TestMarshalBytesWithoutHeaders(t *testing.T) {
s := []Sample{
{Foo: "f", Bar: 1, Baz: "baz", Frop: 0.1},
{Foo: "e", Bar: 3, Baz: "b", Frop: 0.5},
}
csvContent, err := MarshalBytesWithoutHeaders(&s)
if err != nil {
t.Fatal(err)
}
if expected := "f,1,baz,0.1,,,\ne,3,b,0.5,,,\n"; string(csvContent) != expected {
t.Fatalf("expected: %q\ngot: %q", expected, csvContent)
}
}
func Test_writeTo_multipleTags(t *testing.T) { func Test_writeTo_multipleTags(t *testing.T) {
b := bytes.Buffer{} b := bytes.Buffer{}
e := &encoder{out: &b} e := &encoder{out: &b}