Files
gof3/tree/f3/f3.go
2024-03-07 16:36:47 +08:00

127 lines
4.4 KiB
Go

// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package f3
import (
"context"
"fmt"
"lab.forgefriends.org/friendlyforgeformat/gof3/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/options"
objects_helper "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3/objects"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
)
type treeF3 struct {
generic.Tree
options options.Interface
objectsHelper objects_helper.Interface
}
type setFunc func(parent generic.Path, node generic.NodeInterface)
type TreeInterface interface {
generic.TreeInterface
IsContainer(generic.Kind) bool
NewFormat(kind generic.Kind) f3.Interface
CreateChild(ctx context.Context, pathString string, set setFunc)
GetObjectsHelper() objects_helper.Interface
}
func (o *treeF3) GetChildrenKind(parentKind generic.Kind) generic.Kind {
if childrenKind, ok := childrenKind[parentKind]; ok {
return childrenKind
}
panic(fmt.Errorf("unexpected kind %s", parentKind))
}
func (o *treeF3) IsContainer(kind generic.Kind) bool {
if isContainer, ok := isContainer[kind]; ok {
return isContainer
}
return false
}
func (o *treeF3) NewFormat(kind generic.Kind) f3.Interface {
return f3.New(string(kind))
}
func (o *treeF3) GetObjectsHelper() objects_helper.Interface {
return o.objectsHelper
}
func (o *treeF3) CreateChild(ctx context.Context, pathString string, set setFunc) {
path := generic.NewPathFromString(pathString)
o.Apply(ctx, path, generic.NewApplyOptions(func(ctx context.Context, parent, path generic.Path, node generic.NodeInterface) {
o.Trace("%s %s | %T %s", parent.String(), node.GetID(), node, node.GetKind())
child := o.Factory(ctx, o.GetChildrenKind(node.GetKind()))
child.SetParent(node)
childParentPath := parent.Clone()
childParentPath = childParentPath.Append(node)
if set != nil {
set(childParentPath, child)
}
child.Upsert(ctx)
child.List(ctx)
}))
}
func newTreeF3(ctx context.Context, opts options.Interface) generic.TreeInterface {
tree := &treeF3{}
tree.Init(tree, opts)
tree.objectsHelper = objects_helper.NewObjectsHelper()
tree.SetDriver(GetForgeFactory(opts.GetName())(tree, opts))
tree.Register(generic.KindRoot, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindForge})
})
tree.Register(KindForge, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindTopics, KindUsers, KindOrganizations})
})
tree.Register(KindUser, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindProjects})
})
tree.Register(KindOrganization, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindProjects})
})
tree.Register(KindProject, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindRepositories, KindIssues, KindLabels, KindMilestones, KindPullRequests, KindReleases})
})
tree.Register(KindIssue, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindComments, KindReactions})
})
tree.Register(KindRepository, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newRepositoryNode(ctx, tree)
})
tree.Register(KindPullRequest, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newPullRequestNode(ctx, tree)
})
tree.Register(KindPullRequest, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindComments, KindReactions, KindReviews})
})
tree.Register(KindReview, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindReviewComments, KindReactions})
})
tree.Register(KindComment, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindReactions})
})
tree.Register(KindRelease, func(ctx context.Context, kind generic.Kind) generic.NodeInterface {
return newFixedChildrenNode(ctx, tree, []generic.Kind{KindAssets})
})
root := tree.Factory(ctx, generic.KindRoot)
tree.SetRoot(root)
return tree
}
func init() {
generic.RegisterFactory("f3", newTreeF3)
}