♻️ Replaced Language property in Page module

This commit is contained in:
Maxim Lebedev 2023-11-13 05:18:31 +06:00
parent 9cdb470ada
commit 6046187817
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
7 changed files with 13 additions and 29 deletions

View File

@ -1,9 +1,7 @@
package domain
import "golang.org/x/text/language"
type Page struct {
Language language.Tag
Language Language
Params map[string]any
File File
Description string

View File

@ -3,11 +3,9 @@ package page
import (
"context"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain"
)
type Repository interface {
Get(ctx context.Context, lang language.Tag, path string) (*domain.Page, error)
Get(ctx context.Context, lang domain.Language, path string) (*domain.Page, error)
}

View File

@ -6,7 +6,6 @@ import (
"io/fs"
"github.com/adrg/frontmatter"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
"source.toby3d.me/toby3d/home/internal/domain"
@ -37,11 +36,10 @@ func NewFileSystemPageRepository(rootDir fs.FS) page.Repository {
}
}
func (repo *fileSystemPageRepository) Get(ctx context.Context, lang language.Tag, p string) (*domain.Page, error) {
func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Language, p string) (*domain.Page, error) {
ext := ".md"
if lang != language.Und {
base, _ := lang.Base()
ext = "." + base.String() + ext
if lang != domain.LanguageUnd {
ext = "." + lang.Lang() + ext
}
target := p + ext

View File

@ -8,7 +8,6 @@ import (
"testing/fstest"
"github.com/google/go-cmp/cmp"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain"
repository "source.toby3d.me/toby3d/home/internal/page/repository/fs"
@ -77,12 +76,12 @@ func TestGet(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
out, err := repo.Get(context.Background(), language.Und, tc.input)
out, err := repo.Get(context.Background(), domain.LanguageUnd, tc.input)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(out, tc.expect, cmp.AllowUnexported(language.Und)); diff != "" {
if diff := cmp.Diff(out, tc.expect, cmp.AllowUnexported(domain.File{}, domain.LanguageUnd)); diff != "" {
t.Error(diff)
}
})

View File

@ -4,13 +4,11 @@ import (
"context"
"errors"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain"
)
type UseCase interface {
Do(ctx context.Context, lang language.Tag, path string) (*domain.Page, error)
Do(ctx context.Context, lang domain.Language, path string) (*domain.Page, error)
}
var ErrNotExist error = errors.New("page not exists")

View File

@ -6,8 +6,6 @@ import (
"path"
"strings"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain"
"source.toby3d.me/toby3d/home/internal/page"
"source.toby3d.me/toby3d/home/internal/static"
@ -26,7 +24,7 @@ func NewPageUseCase(pages page.Repository, statics static.Repository) page.UseCa
}
}
func (ucase *pageUseCase) Do(ctx context.Context, lang language.Tag, p string) (*domain.Page, error) {
func (ucase *pageUseCase) Do(ctx context.Context, lang domain.Language, p string) (*domain.Page, error) {
ext := path.Ext(p)
if ext == ".html" {
p = p[:len(p)-len(ext)]
@ -65,8 +63,8 @@ func (ucase *pageUseCase) Do(ctx context.Context, lang language.Tag, p string) (
continue
}
resLang := language.Make(resParts[len(resParts)-1])
if resLang == language.Und {
resLang := domain.NewLanguage(resParts[len(resParts)-1])
if resLang == domain.LanguageUnd {
continue
}
@ -78,11 +76,6 @@ func (ucase *pageUseCase) Do(ctx context.Context, lang language.Tag, p string) (
out.Translations = append(out.Translations, translation)
}
translations := make([]string, 0)
for i := range out.Translations {
translations = append(translations, out.Translations[i].Language.String())
}
return out, nil
}

View File

@ -7,8 +7,8 @@ import (
"testing/fstest"
"github.com/google/go-cmp/cmp"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain"
pagefsrepo "source.toby3d.me/toby3d/home/internal/page/repository/fs"
"source.toby3d.me/toby3d/home/internal/page/usecase"
"source.toby3d.me/toby3d/home/internal/static"
@ -49,7 +49,7 @@ func TestDo(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
actual, err := ucase.Do(context.Background(), language.Und, tc.input)
actual, err := ucase.Do(context.Background(), domain.LanguageUnd, tc.input)
if err != nil {
t.Fatal(err)
}