-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(files): add a file factory to return factories
- Loading branch information
1 parent
e986fc3
commit f4a8a66
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |