Skip to content

Commit

Permalink
fix bug for taxnomy walk
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Dec 28, 2024
1 parent 5accb47 commit b94c5e6
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 16 deletions.
13 changes: 13 additions & 0 deletions internal/domain/contenthub/entity/pagetrees.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func (t *PageTrees) CreateMutableTrees() {
}
}

type weightPage struct {
*ordinalWeightPage
page contenthub.Page
}

func (n *weightPage) Page() contenthub.Page {
return n.page
}

func (n *weightPage) Owner() contenthub.Page {
return n.ordinalWeightPage.Page
}

type ordinalWeightPage struct {
ordinal int
weight int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"github.com/gohugonet/hugoverse/pkg/doctree"
)

type SourceShifter struct{}
type Shifter struct{}

func (s *SourceShifter) Delete(n *PageTreesNode, dimension doctree.Dimension) (bool, bool) {
func (s *Shifter) Delete(n *PageTreesNode, dimension doctree.Dimension) (bool, bool) {
wasDeleted := n.delete(dimension[doctree.DimensionLanguage.Index()])
return wasDeleted, n.isEmpty()
}

func (s *SourceShifter) Shift(n *PageTreesNode, dimension doctree.Dimension, exact bool) (*PageTreesNode, bool, doctree.DimensionFlag) {
func (s *Shifter) Shift(n *PageTreesNode, dimension doctree.Dimension, exact bool) (*PageTreesNode, bool, doctree.DimensionFlag) {
newNode, found := n.shift(dimension[doctree.DimensionLanguage.Index()], exact)
if newNode != nil {
if found {
Expand All @@ -23,17 +23,17 @@ func (s *SourceShifter) Shift(n *PageTreesNode, dimension doctree.Dimension, exa
return nil, false, doctree.DimensionNone
}

func (s *SourceShifter) ForEachInDimension(n *PageTreesNode, d int, f func(*PageTreesNode) bool) {
func (s *Shifter) ForEachInDimension(n *PageTreesNode, d int, f func(*PageTreesNode) bool) {
if d != doctree.DimensionLanguage.Index() {
panic("only language dimension supported")
}
f(n)
}

func (s *SourceShifter) InsertInto(old, new *PageTreesNode, dimension doctree.Dimension) *PageTreesNode {
func (s *Shifter) InsertInto(old, new *PageTreesNode, dimension doctree.Dimension) *PageTreesNode {
return old.mergeWithLang(new, dimension[doctree.DimensionLanguage.Index()])
}

func (s *SourceShifter) Insert(old, new *PageTreesNode) *PageTreesNode {
func (s *Shifter) Insert(old, new *PageTreesNode) *PageTreesNode {
return old.merge(new)
}
20 changes: 20 additions & 0 deletions internal/domain/contenthub/entity/shifterpage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package entity

import (
"github.com/gohugonet/hugoverse/pkg/doctree"
)

type PageShifter struct {
*Shifter
}

func (s *PageShifter) Shift(n *PageTreesNode, dimension doctree.Dimension, exact bool) (*PageTreesNode, bool, doctree.DimensionFlag) {
newNode, found := n.shift(dimension[doctree.DimensionLanguage.Index()], exact)
if newNode != nil {
if found {
return newNode, true, doctree.DimensionLanguage
}
}

return nil, false, doctree.DimensionNone
}
21 changes: 21 additions & 0 deletions internal/domain/contenthub/entity/shifterpagesource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package entity

import (
"github.com/gohugonet/hugoverse/pkg/doctree"
)

type SourceShifter struct {
*Shifter
}

func (s *SourceShifter) Shift(n *PageTreesNode, dimension doctree.Dimension, exact bool) (*PageTreesNode, bool, doctree.DimensionFlag) {
newNode, found := n.shift(dimension[doctree.DimensionLanguage.Index()], exact)
if newNode != nil {
if found {
return newNode, true, doctree.DimensionLanguage
}
return newNode, true, doctree.DimensionNone
}

return nil, false, doctree.DimensionNone
}
10 changes: 9 additions & 1 deletion internal/domain/contenthub/entity/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,16 @@ func (ch *ContentHub) WalkTaxonomies(langIndex int, walker contenthub.WalkTaxono
doctree.LockTypeRead,
paths.AddTrailingSlash(s),
func(ss string, wn *WeightedTermTreeNode) (bool, error) {
sp, found := wn.getPage()
if !found {
return false, nil
}

if err := walker(viewName.Plural(), k, wn.term); err != nil {
if err := walker(viewName.Plural(), k,
&weightPage{
ordinalWeightPage: wn.term,
page: sp},
); err != nil {
return false, err
}

Expand Down
12 changes: 6 additions & 6 deletions internal/domain/contenthub/factory/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ func newContentSpec() (*entity.ContentSpec, error) {
}

func newPageTree() *entity.PageTrees {
treeConfig := doctree.Config[*entity.PageTreesNode]{
Shifter: &entity.SourceShifter{},
}

pageTrees := &entity.PageTrees{
TreePages: doctree.New(
treeConfig,
doctree.Config[*entity.PageTreesNode]{
Shifter: &entity.PageShifter{Shifter: &entity.Shifter{}},
},
),
TreeResources: doctree.New(
treeConfig,
doctree.Config[*entity.PageTreesNode]{
Shifter: &entity.SourceShifter{Shifter: &entity.Shifter{}},
},
),
TreeTaxonomyEntries: doctree.NewTreeShiftTree[*entity.WeightedTermTreeNode](
doctree.DimensionLanguage.Index(), 2), // TODO: get this from config
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/contenthub/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ type Page interface {
type OrdinalWeightPage interface {
Weight() int
Ordinal() int

Page
Page() Page
Owner() Page
}

type PageMeta interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/site/entity/sitefields.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *Site) sitePage(p contenthub.Page) (*Page, error) {
}

func (s *Site) siteWeightedPage(p contenthub.OrdinalWeightPage) (*WeightedPage, error) {
sp, err := s.sitePage(p)
sp, err := s.sitePage(p.Page())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b94c5e6

Please sign in to comment.