Skip to content

Commit

Permalink
feat: API improvements to open and close files (#4)
Browse files Browse the repository at this point in the history
* io methods: read, save, open, write

* methods ordering

* some more tests
  • Loading branch information
fmartingr committed Sep 26, 2023
1 parent 1d670dd commit e9873f7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 26 deletions.
41 changes: 34 additions & 7 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,62 @@ import (
"bytes"
"encoding/xml"
"fmt"
"io"
"os"
)

// Write writes the ComicInfo spec to the specified path.
func Write(ci ComicInfo, path string) error {
// Read reads the ComicInfo spec from the specified reader
func Read(r io.Reader) (*ComicInfo, error) {
var ci ComicInfo
err := xml.NewDecoder(r).Decode(&ci)
if err != nil {
return nil, fmt.Errorf("error unmarshalling ComicInfo: %w", err)
}

return &ci, nil
}

// Write writes the ComicInfo spec to the specified writter
func Write(ci *ComicInfo, w io.Writer) error {
contents, err := xml.Marshal(ci)
if err != nil {
return fmt.Errorf("error marshalling ComicInfo: %w", err)
}

err = os.WriteFile(path, bytes.Join([][]byte{xmlHeader, contents}, []byte("")), 0644)
_, err = w.Write(bytes.Join([][]byte{xmlHeader, contents}, []byte("")))
if err != nil {
return fmt.Errorf("error writing ComicInfo to file: %w", err)
}
return nil
}

// Read reads the ComicInfo spec from the specified path.
func Open(path string) (ComicInfo, error) {
func Open(path string) (*ComicInfo, error) {
f, err := os.ReadFile(path)
if err != nil {
return ComicInfo{}, fmt.Errorf("error reading file: %w", err)
return nil, fmt.Errorf("error reading file: %w", err)
}

var ci ComicInfo
err = xml.Unmarshal(f, &ci)
if err != nil {
return ComicInfo{}, fmt.Errorf("error unmarshalling ComicInfo: %w", err)
return nil, fmt.Errorf("error unmarshalling ComicInfo: %w", err)
}

return &ci, nil
}

// Save writes the ComicInfo spec to the specified path.
func Save(ci *ComicInfo, path string) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("error creating file: %w", err)
}

err = Write(ci, f)
if err != nil {
return fmt.Errorf("error writing ComicInfo to file: %w", err)
}

return ci, nil
return nil
}
43 changes: 37 additions & 6 deletions io_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
package comicinfo

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

"github.com/stretchr/testify/require"
)

func TestWriteRead(t *testing.T) {
func TestSaveOpen(t *testing.T) {
tmpDir := t.TempDir()
ci := ComicInfo{
ci := &ComicInfo{
Title: "Test",
}

path := filepath.Join(tmpDir, "test.xml")
path := filepath.Join(tmpDir, "comicinfo.xml")

err := Write(ci, path)
err := Save(ci, path)
if err != nil {
t.Errorf("error writing ComicInfo: %v", err)
t.Fatalf("error writing ComicInfo: %v", err)
}

ci2, err := Open(path)
if err != nil {
t.Errorf("error reading ComicInfo: %v", err)
t.Fatalf("error reading ComicInfo: %v", err)
}

require.Equal(t, ci, ci2)
}

func TestWriteOpen(t *testing.T) {
ci := &ComicInfo{
Title: "Test",
}

var buf bytes.Buffer

err := Write(ci, &buf)
if err != nil {
t.Fatalf("error writing ComicInfo: %v", err)
}

ci2, err := Read(&buf)
if err != nil {
t.Fatalf("error reading ComicInfo: %v", err)
}

require.Equal(t, ci, ci2)
}

func TestReadError(t *testing.T) {
_, err := Read(bytes.NewReader([]byte("")))
require.Error(t, err)
}

func TestOpenError(t *testing.T) {
_, err := Open("nonexistent.xml")
require.Error(t, err)
}
4 changes: 2 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (ci *ComicInfo) SetXMLAttributes() {
}

// New provides a new ComicInfo struct with the XML attributes set
func NewComicInfo() ComicInfo {
func NewComicInfo() *ComicInfo {
ci := ComicInfo{}
ci.SetXMLAttributes()
return ci
return &ci
}

// YesNo defines the YesNo type
Expand Down
24 changes: 13 additions & 11 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,26 @@ func TestFullSchema(t *testing.T) {
ci.AgeRating = AgeRatingEveryone
ci.MainCharacterOrTeam = "Test"
ci.Review = "Test"
ci.Pages.Pages = append(ci.Pages.Pages, ComicPageInfo{
Image: 1,
Type: ComicPageTypeEditorial,
DoublePage: false,
ImageSize: 999,
Key: "?",
Bookmark: "yes",
ImageWidth: 20,
ImageHeight: 21,
})
ci.CommunityRating = 5.0
ci.MainCharacterOrTeam = "Test"
ci.Review = "Test"

page := NewComicPageInfo()
page.Image = 1
page.Type = ComicPageTypeEditorial
page.DoublePage = false
page.ImageSize = 999
page.Key = "?"
page.Bookmark = "yes"
page.ImageWidth = 20
page.ImageHeight = 21

ci.Pages.Pages = append(ci.Pages.Pages, page)

// Write the ComicInfo struct to a temporary file
ciPath := filepath.Join(tmpDir, "comicinfo.xml")

err = Write(ci, ciPath)
err = Save(ci, ciPath)
if err != nil {
t.Errorf("error writing ComicInfo: %v", err)
}
Expand Down

0 comments on commit e9873f7

Please sign in to comment.