~chrisppy/go-barefeed

a45be836cbaeb80dfe7cff026ff0f9c64cf83212 — Chris Palmer 2 years ago e767630 v0.4.0
many fixes for read and write
4 files changed, 58 insertions(+), 21 deletions(-)

M barefeed.go
M barefeed_test.go
M message.go
A test-files/test.barefeed
M barefeed.go => barefeed.go +13 -9
@@ 5,6 5,7 @@ package barefeed

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path/filepath"


@@ 14,27 15,30 @@ import (

// FromFile will read barefeed from a file
func FromFile(path string) (*Message, error) {
	b, err := os.ReadFile(filepath.Clean(path))
	f, err := os.Open(filepath.Clean(path))
	if err != nil {
		return nil, err
	}
	return FromBytes(b)
	defer func() {
		if err := f.Close(); err != nil {
			return
		}
	}()

	return FromReader(f)
}

// FromReader will read barefeed from a Reader
func FromReader(reader io.Reader) (*Message, error) {
	buf := &bytes.Buffer{}
	if _, err := buf.ReadFrom(reader); err != nil {
		return nil, err
		return nil, fmt.Errorf("error reading from buffer: %s", err.Error())
	}
	return FromBytes(buf.Bytes())
}

// FromBytes will read barefeed from bytes
func FromBytes(b []byte) (*Message, error) {
	var m Message
	if err := bare.Unmarshal(b, &m); err != nil {
		return nil, err
	if err := bare.Unmarshal(buf.Bytes(), &m); err != nil {
		return nil, fmt.Errorf("error unmarshalling message: %s", err.Error())
	}

	return &m, nil
}

M barefeed_test.go => barefeed_test.go +27 -10
@@ 2,6 2,7 @@ package barefeed

import (
	"bytes"
	"path/filepath"
	"testing"
)



@@ 10,16 11,16 @@ func TestBytes(t *testing.T) {

	b, err := expected.Bytes()
	if err != nil {
		t.Errorf("Bytes should not have error: %e", err)
		t.Errorf("Bytes should not have error: %s", err.Error())
	} else if b == nil {
		t.Errorf("Bytes should not be nil: %e", err)
		t.Errorf("Bytes should not be nil")
	} else if len(b) == 0 {
		t.Errorf("Bytes should not be empty: %e", err)
		t.Errorf("Bytes should not be empty")
	}

	actual, err := FromBytes(b)
	actual, err := FromReader(bytes.NewReader(b))
	if err != nil {
		t.Errorf("Message should not have error: %e", err)
		t.Errorf("Message should not have error: %s", err.Error())
	} else if actual == nil {
		t.Errorf("Message should not have been nil")
	}


@@ 32,16 33,16 @@ func TestReader(t *testing.T) {

	b, err := expected.Bytes()
	if err != nil {
		t.Errorf("Bytes should not have error: %e", err)
		t.Errorf("Bytes should not have error: %s", err.Error())
	} else if b == nil {
		t.Errorf("Bytes should not be nil: %e", err)
		t.Errorf("Bytes should not be nil")
	} else if len(b) == 0 {
		t.Errorf("Bytes should not be empty: %e", err)
		t.Errorf("Bytes should not be empty")
	}

	actual, err := FromReader(bytes.NewReader(b))
	if err != nil {
		t.Errorf("Message should not have error: %e", err)
		t.Errorf("Message should not have error: %s", err.Error())
	} else if actual == nil {
		t.Errorf("Message should not have been nil")
	}


@@ 50,7 51,7 @@ func TestReader(t *testing.T) {
}

func TestFromBytesError(t *testing.T) {
	m, err := FromBytes([]byte("{"))
	m, err := FromReader(bytes.NewReader([]byte("{")))
	if m != nil {
		t.Errorf("Message should have been nil: %v", m)
	}


@@ 68,3 69,19 @@ func TestFromReaderError(t *testing.T) {
		t.Errorf("error should not have been nil")
	}
}

func TestWriteRead(t *testing.T) {
	path := filepath.Join("test-files", "test.barefeed")
	expected := getMessage()
	err := expected.WriteFile(filepath.Clean(path))
	if err != nil {
		t.Errorf("WriteFile should not have error: %s", err.Error())
	}

	actual, err := FromFile(path)
	if err != nil {
		t.Errorf("FromFile should not have error: %s", err.Error())
	} else {
		testMessage(t, expected, *actual)
	}
}

M message.go => message.go +18 -2
@@ 1,6 1,7 @@
package barefeed

import (
	"fmt"
	"os"
	"path/filepath"



@@ 24,10 25,25 @@ func (t Message) Bytes() ([]byte, error) {

// WriteFile will write barefeed to a file at the designated path
func (t Message) WriteFile(path string) error {
	b, err := t.Bytes()
	f, err := os.OpenFile(filepath.Clean(path), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		return err
	}

	return os.WriteFile(filepath.Clean(path), b, 0600)
	defer func() {
		if err := f.Close(); err != nil {
			return
		}
	}()

	w := bare.NewWriter(f)
	if w == nil {
		return fmt.Errorf("writer is nil")
	}

	if err := bare.MarshalWriter(w, &t); err != nil {
		return fmt.Errorf("unable to marshal message to file: %s", err.Error())
	}

	return nil
}

A test-files/test.barefeed => test-files/test.barefeed +0 -0