Skip to content

Commit

Permalink
feat(files): add a file factory to return factories
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Dec 3, 2023
1 parent e986fc3 commit f4a8a66
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/files/file_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package files

import "fmt"

type FileFactory interface {
NewFile(string) (File, error)
}

const (
Img = "image"
Doc = "document"
)

func BuildFactory(f string) (FileFactory, error) {
switch f {
case Img:
return new(ImageFactory), nil
case Doc:
return new(DocumentFactory), nil
default:
return nil, fmt.Errorf("factory with id %s not recognized", f)
}
}
39 changes: 39 additions & 0 deletions pkg/files/file_factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package files

import (
"testing"

"github.com/danvergara/morphos/pkg/files/documents"
"github.com/danvergara/morphos/pkg/files/images"
"github.com/stretchr/testify/require"
)

func TestImageFactory(t *testing.T) {
imgF, err := BuildFactory(Img)
require.NoError(t, err)

imageFile, err := imgF.NewFile(images.PNG)
require.NoError(t, err)

png, ok := imageFile.(Image)
if !ok {
t.Fatal("struct assertion has failed")
}

t.Logf("Png image has type %s", png.ImageType())
}

func TestDocumentFactory(t *testing.T) {
docF, err := BuildFactory(Doc)
require.NoError(t, err)

docFile, err := docF.NewFile(documents.PDF)
require.NoError(t, err)

pdf, ok := docFile.(Document)
if !ok {
t.Fatal("struct assertion has failed")
}

t.Logf("PDF document has type %s", pdf.DocumentType())
}

0 comments on commit f4a8a66

Please sign in to comment.