From 733a3e4e8bac5d6bf6589e47aa9f959da9ed5777 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Mon, 22 Jan 2024 12:44:14 +0600 Subject: [PATCH] :truck: Renamed File domain into Path --- internal/domain/file.go | 93 ------------- internal/domain/page.go | 2 +- internal/domain/path.go | 122 ++++++++++++++++++ .../domain/{file_test.go => path_test.go} | 76 +++++------ internal/domain/resource.go | 4 +- internal/domain/site.go | 4 +- internal/page/repository/fs/fs_page.go | 2 +- internal/site/repository/fs/fs_site.go | 2 +- 8 files changed, 167 insertions(+), 138 deletions(-) delete mode 100644 internal/domain/file.go create mode 100644 internal/domain/path.go rename internal/domain/{file_test.go => path_test.go} (53%) diff --git a/internal/domain/file.go b/internal/domain/file.go deleted file mode 100644 index 0b62c32..0000000 --- a/internal/domain/file.go +++ /dev/null @@ -1,93 +0,0 @@ -package domain - -import ( - "crypto/md5" - "path/filepath" - "strings" -) - -type File struct { - Language Language - baseFileName string - contentBaseName string - dir string - ext string - filename string - logicalName string - path string - translationBaseName string - uniqueId string -} - -func NewFile(path string) File { - out := File{ - Language: LanguageUnd, - baseFileName: "", - contentBaseName: "", - dir: filepath.Dir(path) + string(filepath.Separator), - ext: strings.TrimPrefix(filepath.Ext(path), "."), - filename: path, - logicalName: filepath.Base(path), - path: path, - translationBaseName: "", - uniqueId: "", - } - out.baseFileName = strings.TrimSuffix(out.logicalName, filepath.Ext(out.logicalName)) - - parts := strings.Split(out.baseFileName, ".") - out.Language = NewLanguage(parts[len(parts)-1]) - out.translationBaseName = strings.Join(parts[:len(parts)-1], ".") - out.contentBaseName = out.translationBaseName - - switch out.translationBaseName { - default: - out.contentBaseName = out.translationBaseName - case "_index", "index": - out.contentBaseName = filepath.Base(out.dir) - } - - hash := md5.New() - _, _ = hash.Write([]byte(out.path)) - out.uniqueId = string(hash.Sum(nil)) - - return out -} - -// BaseFileName returns file name without extention. -func (f File) BaseFileName() string { - return f.baseFileName -} - -func (f File) ContentBaseName() string { - return f.contentBaseName -} - -// Dir returns directory path. -func (f File) Dir() string { - return f.dir -} - -// Ext returns file extention. -func (f File) Ext() string { - return f.ext -} - -func (f File) Filename() string { - return f.filename -} - -func (f File) LogicalName() string { - return f.logicalName -} - -func (f File) Path() string { - return f.path -} - -func (f File) TranslationBaseName() string { - return f.translationBaseName -} - -func (f File) UniqueID() string { - return f.uniqueId -} diff --git a/internal/domain/page.go b/internal/domain/page.go index d27da02..c11ddf7 100644 --- a/internal/domain/page.go +++ b/internal/domain/page.go @@ -3,7 +3,7 @@ package domain type Page struct { Language Language Params map[string]any - File File + File Path Description string Title string Content []byte diff --git a/internal/domain/path.go b/internal/domain/path.go new file mode 100644 index 0000000..0161eb9 --- /dev/null +++ b/internal/domain/path.go @@ -0,0 +1,122 @@ +package domain + +import ( + "crypto/md5" + "path/filepath" + "strings" +) + +type Path struct { + Language Language + baseFileName string + contentBaseName string + dir string + ext string + filename string + logicalName string + path string + translationBaseName string + uniqueId string +} + +func NewPath(path string) Path { + out := Path{ + Language: LanguageUnd, + baseFileName: "", + contentBaseName: "", + dir: filepath.Dir(path) + "/", + ext: strings.TrimPrefix(filepath.Ext(path), "."), + filename: path, + logicalName: filepath.Base(path), + path: path, + translationBaseName: "", + uniqueId: "", + } + out.baseFileName = strings.TrimSuffix(out.logicalName, filepath.Ext(out.logicalName)) + + parts := strings.Split(out.baseFileName, ".") + out.Language = NewLanguage(parts[len(parts)-1]) + out.translationBaseName = strings.Join(parts[:len(parts)-1], ".") + out.contentBaseName = out.translationBaseName + + switch out.translationBaseName { + default: + out.contentBaseName = out.translationBaseName + case "_index", "index": + out.contentBaseName = filepath.Base(out.dir) + } + + hash := md5.New() + _, _ = hash.Write([]byte(out.path)) + out.uniqueId = string(hash.Sum(nil)) + + return out +} + +// BaseFileName returns file name without extention: +// +// /news/a.en.md => a.en +// /news/b/index.en.md => index.en +// /news/_index.en.md => _index.en +func (p Path) BaseFileName() string { + return p.baseFileName +} + +// ContentBaseName returns file or folder name based of index location: +// +// /news/a.en.md => a +// /news/b/index.en.md => b +// /news/_index.en.md => news +func (p Path) ContentBaseName() string { + return p.contentBaseName +} + +// Dir returns directory path: +// +// /news/a.en.md => news/ +// /news/b/index.en.md => news/b/ +// /news/_index.en.md => news/ +func (p Path) Dir() string { + return p.dir +} + +// Ext returns file extention: +// +// /news/b/index.en.md => md +func (p Path) Ext() string { + return p.ext +} + +func (p Path) Filename() string { + return p.filename +} + +// LogicalName returns fille file name in directory: +// +// /news/a.en.md => a.en.md +// /news/b/index.en.md => index.en.md +// /news/_index.en.md => _index.en.md +func (p Path) LogicalName() string { + return p.logicalName +} + +func (p Path) Path() string { + return p.path +} + +// TranslationBaseName returns file name without language code and extention: +// +// /news/a.en.md => a +// /news/b/index.en.md => index +// /news/_index.en.md => _index +func (p Path) TranslationBaseName() string { + return p.translationBaseName +} + +func (p Path) UniqueID() string { + return p.uniqueId +} + +func (p Path) GoString() string { + return "domain.Path(" + p.path + ")" +} diff --git a/internal/domain/file_test.go b/internal/domain/path_test.go similarity index 53% rename from internal/domain/file_test.go rename to internal/domain/path_test.go index acaf365..67a83dd 100644 --- a/internal/domain/file_test.go +++ b/internal/domain/path_test.go @@ -8,159 +8,159 @@ import ( ) var ( - testRegularFile string = filepath.Join("news", "a.en.md") - testLeafFile string = filepath.Join("news", "b", "index.en.md") - testBranchFile string = filepath.Join("news", "_index.en.md") + testRegularPath string = filepath.Join("news", "a.en.md") + testLeafPath string = filepath.Join("news", "b", "index.en.md") + testBranchPath string = filepath.Join("news", "_index.en.md") ) -func TestFile_BaseFileName(t *testing.T) { +func TestPath_BaseFileName(t *testing.T) { t.Parallel() for name, tc := range map[string]struct { input, expect string }{ - "regular": {testRegularFile, "a.en"}, - "leaf": {testLeafFile, "index.en"}, - "branch": {testBranchFile, "_index.en"}, + "regular": {testRegularPath, "a.en"}, + "leaf": {testLeafPath, "index.en"}, + "branch": {testBranchPath, "_index.en"}, } { name, tc := name, tc t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(tc.input).BaseFileName(); actual != tc.expect { + if actual := domain.NewPath(tc.input).BaseFileName(); actual != tc.expect { t.Errorf("BaseFileName() = '%s', want '%s'", actual, tc.expect) } }) } } -func TestFile_ContentBaseName(t *testing.T) { +func TestPath_ContentBaseName(t *testing.T) { t.Parallel() for name, tc := range map[string]struct { input, expect string }{ - "regular": {testRegularFile, "a"}, - "leaf": {testLeafFile, "b"}, - "branch": {testBranchFile, "news"}, + "regular": {testRegularPath, "a"}, + "leaf": {testLeafPath, "b"}, + "branch": {testBranchPath, "news"}, } { name, tc := name, tc t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(tc.input).ContentBaseName(); actual != tc.expect { + if actual := domain.NewPath(tc.input).ContentBaseName(); actual != tc.expect { t.Errorf("ContentBaseName() = '%s', want '%s'", actual, tc.expect) } }) } } -func TestFile_Dir(t *testing.T) { +func TestPath_Dir(t *testing.T) { t.Parallel() for name, tc := range map[string]struct { input, expect string }{ - "regular": {testRegularFile, "news/"}, - "leaf": {testLeafFile, "news/b/"}, - "branch": {testBranchFile, "news/"}, + "regular": {testRegularPath, "news/"}, + "leaf": {testLeafPath, "news/b/"}, + "branch": {testBranchPath, "news/"}, } { name, tc := name, tc t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(tc.input).Dir(); actual != tc.expect { + if actual := domain.NewPath(tc.input).Dir(); actual != tc.expect { t.Errorf("Dir() = '%s', want '%s'", actual, tc.expect) } }) } } -func TestFile_Ext(t *testing.T) { +func TestPath_Ext(t *testing.T) { t.Parallel() const expect string = "md" for name, input := range map[string]string{ - "regular": testRegularFile, - "leaf": testLeafFile, - "branch": testBranchFile, + "regular": testRegularPath, + "leaf": testLeafPath, + "branch": testBranchPath, } { name, input := name, input t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(input).Ext(); actual != expect { + if actual := domain.NewPath(input).Ext(); actual != expect { t.Errorf("Ext() = '%s', want '%s'", actual, expect) } }) } } -func TestFile_Language(t *testing.T) { +func TestPath_Language(t *testing.T) { t.Parallel() var expect domain.Language = domain.NewLanguage("en") for name, input := range map[string]string{ - "regular": testRegularFile, - "leaf": testLeafFile, - "branch": testBranchFile, + "regular": testRegularPath, + "leaf": testLeafPath, + "branch": testBranchPath, } { name, input := name, input t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(input).Language; actual != expect { + if actual := domain.NewPath(input).Language; actual != expect { t.Errorf("Language() = '%s', want '%s'", actual, expect) } }) } } -func TestFile_LogicalName(t *testing.T) { +func TestPath_LogicalName(t *testing.T) { t.Parallel() for name, tc := range map[string]struct { input, expect string }{ - "regular": {testRegularFile, "a.en.md"}, - "leaf": {testLeafFile, "index.en.md"}, - "branch": {testBranchFile, "_index.en.md"}, + "regular": {testRegularPath, "a.en.md"}, + "leaf": {testLeafPath, "index.en.md"}, + "branch": {testBranchPath, "_index.en.md"}, } { name, tc := name, tc t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(tc.input).LogicalName(); actual != tc.expect { + if actual := domain.NewPath(tc.input).LogicalName(); actual != tc.expect { t.Errorf("LogicalName() = '%s', want '%s'", actual, tc.expect) } }) } } -func TestFile_TranslationBaseName(t *testing.T) { +func TestPath_TranslationBaseName(t *testing.T) { t.Parallel() for name, tc := range map[string]struct { input, expect string }{ - "regular": {testRegularFile, "a"}, - "leaf": {testLeafFile, "index"}, - "branch": {testBranchFile, "_index"}, + "regular": {testRegularPath, "a"}, + "leaf": {testLeafPath, "index"}, + "branch": {testBranchPath, "_index"}, } { name, tc := name, tc t.Run(name, func(t *testing.T) { t.Parallel() - if actual := domain.NewFile(tc.input).TranslationBaseName(); actual != tc.expect { + if actual := domain.NewPath(tc.input).TranslationBaseName(); actual != tc.expect { t.Errorf("TranslationBaseName() = '%s', want '%s'", actual, tc.expect) } }) diff --git a/internal/domain/resource.go b/internal/domain/resource.go index 174b321..ff96a60 100644 --- a/internal/domain/resource.go +++ b/internal/domain/resource.go @@ -15,7 +15,7 @@ import ( ) type Resource struct { - File File + File Path modTime time.Time params map[string]any // TODO(toby3d): set from Page configuration @@ -30,7 +30,7 @@ type Resource struct { func NewResource(modTime time.Time, r io.Reader, key string) *Resource { mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key))) out := &Resource{ - File: NewFile(key), + File: NewPath(key), modTime: modTime, key: key, name: key, // TODO(toby3d): set from Page configuration diff --git a/internal/domain/site.go b/internal/domain/site.go index 8e3e51f..aba53fc 100644 --- a/internal/domain/site.go +++ b/internal/domain/site.go @@ -15,7 +15,7 @@ type Site struct { BaseURL *url.URL Params map[string]any TimeZone *time.Location - File File + File Path Title string Resources Resources } @@ -44,7 +44,7 @@ func TestSite(tb testing.TB) *Site { Languages: []Language{en, ru}, BaseURL: &url.URL{Scheme: "http", Host: "127.0.0.1:3000", Path: "/"}, TimeZone: time.UTC, - File: NewFile(filepath.Join("content", "index.en.md")), + File: NewPath(filepath.Join("content", "index.en.md")), Title: "Testing", Resources: make([]*Resource, 0), Params: map[string]any{ diff --git a/internal/page/repository/fs/fs_page.go b/internal/page/repository/fs/fs_page.go index 015a27b..90d8aa9 100644 --- a/internal/page/repository/fs/fs_page.go +++ b/internal/page/repository/fs/fs_page.go @@ -57,7 +57,7 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Langu } return &domain.Page{ - File: domain.NewFile(target), + File: domain.NewPath(target), Language: lang, Title: data.Title, Content: data.Content, diff --git a/internal/site/repository/fs/fs_site.go b/internal/site/repository/fs/fs_site.go index 090774e..bc0e81b 100644 --- a/internal/site/repository/fs/fs_site.go +++ b/internal/site/repository/fs/fs_site.go @@ -71,7 +71,7 @@ func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang domain.Langu } return &domain.Site{ - File: domain.NewFile(target), + File: domain.NewPath(target), DefaultLanguage: data.DefaultLanguage.Language, Language: lang, Title: data.Title,