♻️ Updated target page searching

This commit is contained in:
Maxim Lebedev 2023-11-08 07:02:11 +06:00
parent 9bd2f81c46
commit 9d128a7665
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
6 changed files with 124 additions and 38 deletions

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io/fs" "io/fs"
"path/filepath"
"github.com/adrg/frontmatter" "github.com/adrg/frontmatter"
"golang.org/x/text/language" "golang.org/x/text/language"
@ -38,12 +37,12 @@ func NewFileSystemPageRepository(rootDir fs.FS) page.Repository {
} }
func (repo *fileSystemPageRepository) Get(ctx context.Context, lang language.Tag, path string) (*domain.Page, error) { func (repo *fileSystemPageRepository) Get(ctx context.Context, lang language.Tag, path string) (*domain.Page, error) {
target := "index." + lang.String() + ".md" ext := ".md"
if lang == language.Und { if lang != language.Und {
target = "index.md" ext = "." + lang.String() + ext
} }
target = filepath.Join(path, target) target := path + ext
f, err := repo.dir.Open(target) f, err := repo.dir.Open(target)
if err != nil { if err != nil {

View File

@ -18,38 +18,38 @@ func TestGet(t *testing.T) {
t.Parallel() t.Parallel()
testData := fstest.MapFS{ testData := fstest.MapFS{
filepath.Join("index.ru.md"): &fstest.MapFile{ filepath.Join("both", "index.md"): &fstest.MapFile{Data: []byte("both/index.md")},
Data: []byte("---\ntitle: страница\n---\nпривет, мир!\n"), filepath.Join("both.md"): &fstest.MapFile{Data: []byte("both.md")},
}, filepath.Join("file.md"): &fstest.MapFile{Data: []byte("file.md")},
filepath.Join("dir", "index.en.md"): &fstest.MapFile{ filepath.Join("folder", "index.md"): &fstest.MapFile{Data: []byte("folder/index.md")},
Data: []byte("---\ntitle: page\n---\nhello, world!\n"), filepath.Join("index.md"): &fstest.MapFile{Data: []byte("index.md")},
},
} }
repo := repository.NewFileSystemPageRepository(testData) repo := repository.NewFileSystemPageRepository(testData)
for name, tc := range map[string]struct { for name, tc := range map[string]struct {
lang language.Tag
expect *domain.Page expect *domain.Page
path string input string
}{ }{
"file": { "index": {
lang: language.English, input: path.Join("index"),
path: path.Join("dir"), expect: &domain.Page{Content: []byte("index.md")},
expect: &domain.Page{
Language: language.English,
Title: "page",
Content: []byte("hello, world!\n"),
},
}, },
"dir": { "file": {
lang: language.Russian, input: path.Join("file"),
path: path.Join(""), expect: &domain.Page{Content: []byte("file.md")},
expect: &domain.Page{ },
Language: language.Russian, "folder": {
Title: "страница", input: path.Join("folder", "index"),
Content: []byte("привет, мир!\n"), expect: &domain.Page{Content: []byte("folder/index.md")},
}, },
"both-file": {
input: path.Join("both"),
expect: &domain.Page{Content: []byte("both.md")},
},
"both-folder": {
input: path.Join("both", "index"),
expect: &domain.Page{Content: []byte("both/index.md")},
}, },
} { } {
name, tc := name, tc name, tc := name, tc
@ -57,7 +57,7 @@ func TestGet(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
out, err := repo.Get(context.Background(), tc.lang, tc.path) out, err := repo.Get(context.Background(), language.Und, tc.input)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -2,6 +2,7 @@ package page
import ( import (
"context" "context"
"errors"
"golang.org/x/text/language" "golang.org/x/text/language"
@ -11,3 +12,5 @@ import (
type UseCase interface { type UseCase interface {
Do(ctx context.Context, lang language.Tag, path string) (*domain.Page, error) Do(ctx context.Context, lang language.Tag, path string) (*domain.Page, error)
} }
var ErrNotExist error = errors.New("page not exists")

View File

@ -3,11 +3,13 @@ package usecase
import ( import (
"context" "context"
"fmt" "fmt"
"path"
"golang.org/x/text/language" "golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain" "source.toby3d.me/toby3d/home/internal/domain"
"source.toby3d.me/toby3d/home/internal/page" "source.toby3d.me/toby3d/home/internal/page"
"source.toby3d.me/toby3d/home/internal/urlutil"
) )
type pageUseCase struct { type pageUseCase struct {
@ -20,11 +22,32 @@ func NewPageUseCase(pages page.Repository) page.UseCase {
} }
} }
func (ucase *pageUseCase) Do(ctx context.Context, lang language.Tag, path string) (*domain.Page, error) { func (ucase *pageUseCase) Do(ctx context.Context, lang language.Tag, p string) (*domain.Page, error) {
page, err := ucase.pages.Get(ctx, lang, path) ext := path.Ext(p)
if err != nil { if ext == ".html" {
return nil, fmt.Errorf("cannot find '%s' page: %w", path, err) p = p[:len(p)-len(ext)]
} }
return page, nil hasTrailingSlash := p[len(p)-1] == '/'
head, tail := urlutil.ShiftPath(p)
targets := []string{path.Join(head, tail)}
if tail == "/" {
if hasTrailingSlash || ext == "" {
targets = append([]string{path.Join(head, "index")}, targets...)
}
targets = append(targets, head)
}
for i := range targets {
out, err := ucase.pages.Get(ctx, lang, targets[i])
if err != nil {
continue
}
return out, nil
}
return nil, fmt.Errorf("cannot find page on path '%s': %w", p, page.ErrNotExist)
} }

View File

@ -0,0 +1,59 @@
package usecase_test
import (
"context"
"path/filepath"
"testing"
"testing/fstest"
"github.com/google/go-cmp/cmp"
"golang.org/x/text/language"
pagefsrepo "source.toby3d.me/toby3d/home/internal/page/repository/fs"
"source.toby3d.me/toby3d/home/internal/page/usecase"
)
func TestDo(t *testing.T) {
t.Parallel()
pages := pagefsrepo.NewFileSystemPageRepository(fstest.MapFS{
filepath.Join("both", "index.md"): &fstest.MapFile{Data: []byte(`both/index.md`)},
filepath.Join("folder", "index.md"): &fstest.MapFile{Data: []byte(`folder/index.md`)},
filepath.Join("both.md"): &fstest.MapFile{Data: []byte(`both.md`)},
filepath.Join("file.md"): &fstest.MapFile{Data: []byte(`file.md`)},
filepath.Join("index.md"): &fstest.MapFile{Data: []byte(`index.md`)},
})
ucase := usecase.NewPageUseCase(pages)
for name, tc := range map[string]struct {
input string
expect []byte
}{
"file": {input: "/file", expect: []byte(`file.md`)},
"file-slash": {input: "/file/", expect: []byte(`file.md`)},
"file-ext": {input: "/file.html", expect: []byte(`file.md`)},
"folder": {input: "/folder", expect: []byte(`folder/index.md`)},
"folder-slash": {input: "/folder/", expect: []byte(`folder/index.md`)},
"folder-index": {input: "/folder/index.html", expect: []byte(`folder/index.md`)},
"both": {input: "/both", expect: []byte(`both/index.md`)},
"both-slash": {input: "/both/", expect: []byte(`both/index.md`)},
"both-ext": {input: "/both.html", expect: []byte(`both.md`)},
"both-index": {input: "/both/index.html", expect: []byte(`both/index.md`)},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
actual, err := ucase.Do(context.Background(), language.Und, tc.input)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(actual.Content), string(tc.expect)); diff != "" {
t.Error(diff)
}
})
}
}

View File

@ -37,11 +37,13 @@ func NewFileSystemSiteRepository(rootDir fs.FS) site.Repository {
} }
func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang language.Tag) (*domain.Site, error) { func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang language.Tag) (*domain.Site, error) {
target := "index." + lang.String() + ".md" ext := ".md"
if lang == language.Und { if lang != language.Und {
target = "index.md" ext = "." + lang.String() + ext
} }
target := "index" + ext
f, err := repo.dir.Open(target) f, err := repo.dir.Open(target)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot open '%s' site file: %w", target, err) return nil, fmt.Errorf("cannot open '%s' site file: %w", target, err)