Skip to content

Commit 43cb87e

Browse files
author
en-ken
committed
refactor: deleted redundant structs.
1 parent 9c95e02 commit 43cb87e

File tree

7 files changed

+13
-192
lines changed

7 files changed

+13
-192
lines changed

kadai2/en-ken/cli/cli.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
// CLI is for DI
1212
type CLI struct {
13-
DirPath imgcnv.DirPath
14-
ImageFactory imgcnv.ImageFileFactory
13+
AllFilePaths imgcnv.IAllFilePaths
14+
NewImageFIle imgcnv.INewImageFile
1515
}
1616

1717
// Execute executes this app according to options
@@ -43,13 +43,13 @@ func (cli *CLI) Execute(args []string) error {
4343
outputDir = inputDir
4444
}
4545

46-
paths, err := cli.DirPath.AllFilePaths(inputDir, *inputExt)
46+
paths, err := cli.AllFilePaths(inputDir, *inputExt)
4747
if err != nil {
4848
return err
4949
}
5050

5151
for _, path := range paths {
52-
img, err := cli.ImageFactory.Create(path)
52+
img, err := cli.NewImageFIle(path)
5353
if err != nil {
5454
return err
5555
}

kadai2/en-ken/imgcnv/dirpath.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@ import (
55
"path/filepath"
66
)
77

8-
// DirPath expresses I/F to DirPathStruct
9-
type DirPath interface {
10-
AllFilePaths(path string, ext string) ([]string, error)
11-
}
12-
13-
// DirPathStruct expresses searching dir
14-
type DirPathStruct struct {
15-
}
16-
17-
// NewDirPath is a constructor of DirPath
18-
func NewDirPath() DirPath {
19-
return &DirPathStruct{}
20-
}
8+
// IAllFilePaths is I/F of AllFilePaths
9+
type IAllFilePaths func(path string, ext string) ([]string, error)
2110

2211
// AllFilePaths returns
2312
// the paths of the files in the specified directory
2413
// filtered by the specified extension.
25-
func (dirPath *DirPathStruct) AllFilePaths(path string, ext string) ([]string, error) {
14+
func AllFilePaths(path string, ext string) ([]string, error) {
2615
absPath, err := filepath.Abs(path)
2716
if err != nil {
2817
return nil, err

kadai2/en-ken/imgcnv/dirpath_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
)
88

99
func TestAllFilePathsSuccess(t *testing.T) {
10-
dirPath := NewDirPath()
11-
actual, _ := dirPath.AllFilePaths("../testdata/", "jpg")
10+
actual, _ := AllFilePaths("../testdata/", "jpg")
1211
expected := []string{
1312
"../testdata/lenna_color.jpg",
1413
"../testdata/lenna_gray.jpg",
@@ -26,8 +25,7 @@ func TestAllFilePathsSuccess(t *testing.T) {
2625
}
2726

2827
func TestAllFilePathsFailure(t *testing.T) {
29-
dirPath := NewDirPath()
30-
_, err := dirPath.AllFilePaths("../foo/", "jpg")
28+
_, err := AllFilePaths("../foo/", "jpg")
3129

3230
if err == nil {
3331
t.Errorf("directory does not exist")

kadai2/en-ken/imgcnv/imgfile.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,8 @@ type ImageFileStruct struct {
2222
ImageFile
2323
}
2424

25-
// ImageFileFactory is I/F of ImageFileFactoryStruct
26-
type ImageFileFactory interface {
27-
Create(path string) (ImageFile, error)
28-
}
29-
30-
// ImageFileFactoryStruct is for DI
31-
type ImageFileFactoryStruct struct {
32-
}
33-
34-
// NewImageFileFactory is a constructor of NewImageFileFactory
35-
func NewImageFileFactory() ImageFileFactory {
36-
return &ImageFileFactoryStruct{}
37-
}
38-
39-
// Create generates ImageFile
40-
func (factory *ImageFileFactoryStruct) Create(path string) (ImageFile, error) {
41-
return NewImageFile(path)
42-
}
25+
// INewImageFile is I/F to NewImageFile
26+
type INewImageFile func(path string) (ImageFile, error)
4327

4428
// NewImageFile is a constructor of ImageFile
4529
func NewImageFile(path string) (ImageFile, error) {

kadai2/en-ken/kadai2/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88
)
99

1010
func main() {
11-
dirPath := imgcnv.NewDirPath()
12-
factory := imgcnv.NewImageFileFactory()
1311
cli := &cli.CLI{
14-
DirPath: dirPath,
15-
ImageFactory: factory,
12+
AllFilePaths: func(path string, ext string) ([]string, error) { return imgcnv.AllFilePaths(path, ext) },
13+
NewImageFIle: func(path string) (imgcnv.ImageFile, error) { return imgcnv.NewImageFile(path) },
1614
}
1715

1816
cli.Execute(os.Args)

kadai2/en-ken/mock_imgcnv/dirpath.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

kadai2/en-ken/mock_imgcnv/imgfile.go

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)