Documentation ¶
Index ¶
- Constants
- Variables
- func CreateCmd(name string, items ...Item) command.Command
- func HandleCommands(ctx context.Context, bus command.Bus, repo Repository, lookup *Lookup) <-chan error
- func RegisterCommands(r *codec.GobRegistry)
- func RegisterEvents(r *codec.GobRegistry)
- type CreatedData
- type Item
- type ItemOption
- type ItemType
- type ItemsAddedData
- type ItemsRemovedData
- type Lookup
- type Nav
- func (nav *Nav) Append(items ...Item) error
- func (nav *Nav) AppendAt(path string, items ...Item) error
- func (nav *Nav) ApplyEvent(evt event.Event)
- func (nav *Nav) Create(name string) error
- func (nav *Nav) HasItem(items ...string) bool
- func (nav *Nav) Insert(index int, items ...Item) error
- func (nav *Nav) InsertAt(path string, index int, items ...Item) error
- func (n *Nav) MarshalJSON() ([]byte, error)
- func (nav *Nav) Prepend(items ...Item) error
- func (nav *Nav) PrependAt(path string, items ...Item) error
- func (nav *Nav) Remove(items ...string) error
- func (nav *Nav) Sort(sorting []string)
- func (nav *Nav) SortAt(path string, sorting []string)
- func (n *Nav) UnmarshalJSON(b []byte) error
- type ReadCache
- type Repository
- type SortedData
- type Tree
Constants ¶
const ( // Created means a Nav was created. Created = "cms.static.nav.created" // ItemsAdded means Items were added to a Nav. ItemsAdded = "cms.static.nav.items_added" // ItemsRemoved means Items were removed from a Nav. ItemsRemoved = "cms.static.nav.items_removed" // Sorted means a Nav was sorted. Sorted = "cms.static.nav.sorted" )
const ( Label = ItemType("label") StaticLink = ItemType("static_link") )
Item types
const Aggregate = "cms.static.nav"
Aggregate is the name of the Nav aggregate.
const (
// CreateCommand is the command for creating a Nav.
CreateCommand = "cms.static.nav.create"
)
Variables ¶
var ( // ErrEmptyName is returned when trying to create a Nav with an empty name. ErrEmptyName = errors.New("empty name") // ErrInitialItem is returned when trying to remove an initial Item from a Nav. ErrInitialItem = errors.New("initial item") // ErrItemNotFound is returned when an Item cannot be found within the Tree of a Nav. ErrItemNotFound = errors.New("item not found") // ErrDuplicateItem is returned when trying to add an Item with an already // used ID to a Tree. ErrDuplicateItem = errors.New("duplicate item") )
var Events = [...]string{ Created, ItemsAdded, ItemsRemoved, Sorted, }
Events are all navigation events.
Functions ¶
func HandleCommands ¶
func HandleCommands(ctx context.Context, bus command.Bus, repo Repository, lookup *Lookup) <-chan error
HandleCommands handles navigation commands until ctx is canceled. The returned error channel is also closed when ctx is canceled.
func RegisterCommands ¶
func RegisterCommands(r *codec.GobRegistry)
RegisterCommands register commands into a registry.
func RegisterEvents ¶
func RegisterEvents(r *codec.GobRegistry)
RegisterEvents registers events into an event registry.
Types ¶
type CreatedData ¶
type CreatedData struct {
Name string
}
CreatedData is the event data for Created.
type Item ¶
type Item struct { ID string `json:"id"` Type ItemType `json:"type"` Initial bool `json:"initial"` Paths map[string]string `json:"localePaths"` Labels map[string]string `json:"localeLabels"` Tree *Tree `json:"tree"` }
Item is a navigation item.
func NewItem ¶
func NewItem(id string, typ ItemType, opts ...ItemOption) Item
NewItem returns an Item with the given ID and ItemType.
func NewLabel ¶
func NewLabel(id, label string, opts ...ItemOption) Item
NewLabel returns an Item of type Label with the given default label.
func NewStaticLink ¶
func NewStaticLink(id, path, label string, opts ...ItemOption) Item
NewStaticLink returns an Item of type StaticLink with the given default path and label.
type ItemOption ¶
type ItemOption func(*Item)
ItemOption is an option for an Item.
func Initial ¶
func Initial() ItemOption
Initial returns an ItemOption that marks an Item as "initial". An initial Item cannot be removed from a Nav.
func LocaleLabel ¶
func LocaleLabel(locale, label string) ItemOption
LocaleLabel returns an ItemOption that adds a localized label to an Item.
func LocalePath ¶
func LocalePath(locale, path string) ItemOption
LocalePath returns an ItemOption that adds a localized path to an Item.
func SubTree ¶
func SubTree(items ...Item) ItemOption
SubTree returns an ItemOption that adds a subtree to an Item.
type ItemsAddedData ¶
ItemsAddedData is the event data for ItemsAdded.
type ItemsRemovedData ¶
type ItemsRemovedData struct {
Items []string
}
ItemsRemovedData is the event data for ItemsRemoved.
type Lookup ¶
type Lookup struct {
// contains filtered or unexported fields
}
Lookup provides UUID lookup for Navs.
Use NewLookup to create a Lookup.
type Nav ¶
type Nav struct {}
Nav is a navigation.
func Create ¶
Create creates a Nav with the provided name and adds the given Items to it. The provided Items are added as initial items and cannot be removed from the Nav.
Create should typically not be called manually, but rather by the command handler because the command handler validates the uniqueness of the provided name through a *Lookup:
var bus command.Bus cmd := nav.CreateCmd(name, items...) bus.Dispatch(context.TODO(), cmd)
func CreateWithID ¶
CreateWithID does the same as Create, but accepts a custom UUID.
func (*Nav) ApplyEvent ¶
ApplyEvent applies aggregate events.
func (*Nav) HasItem ¶
HasItem returns whether the Nav has the given items. HasItem accepts dot-seperated paths to reference nested items:
nav, _ := Create("example", NewLabel("foo", "Foo"), NewLabel("bar", "Bar", SubTree( NewLabel("baz", "Baz"), ))) nav.HasItem("foo", "bar", "bar.baz")
func (*Nav) MarshalJSON ¶
func (*Nav) Remove ¶
Remove removes Items from Nav. Remove accepts dot-seperated paths to reference nested Items:
nav.Remove("foo.bar.baz")
func (*Nav) Sort ¶
Sort sorts the root level of the navigation. Items are sorted by the sorting of the Item IDs in sorting:
nav.Sort([]string{"bar", "baz", "foo"})
func (*Nav) SortAt ¶
SortAt sorts the tree at the given path. Items are sorted by the sorting of the Item IDs in sorting:
nav.SortAt("foo.bar", []string{"bar", "baz", "foo"})
func (*Nav) UnmarshalJSON ¶
type ReadCache ¶
type ReadCache struct {
// contains filtered or unexported fields
}
ReadCache is a read-cache for Navs.
func NewReadCache ¶
func NewReadCache(repo Repository) *ReadCache
NewReadCache returns a new ReadCache.
func (*ReadCache) Fetch ¶
Fetch fetches the Nav with the given UUID. Fetch first tries to find the Nav in the cache. If it's not cached, Fetch actually fetches from the underlying Repository.
func (*ReadCache) Run ¶
func (c *ReadCache) Run(ctx context.Context, bus event.Bus, debounce time.Duration) (<-chan error, error)
Run starts the ReadCache in a new goroutine and returns a channel of asynchronous errors that is closed when ctx is canceled.
Whenever a Nav event is published, ReadCache flushes the Cache for that Nav.
type Repository ¶
type Repository interface { // Save saves a Nav. Save(context.Context, *Nav) error // Fetch returns the Nav with the given UUID. Fetch(context.Context, uuid.UUID) (*Nav, error) // Use fetches the Nav with the given UUID, calls the provided function with // the Nav as the argument and then saves the Nav. If the provided function // returns a non-nil error, the Nav is not saved and that error is returned. Use(context.Context, uuid.UUID, func(*Nav) error) error // Delete deletes the given Nav. Delete(context.Context, *Nav) error }
Repository is the repository for navigations.
func GoesRepository ¶
func GoesRepository(repo aggregate.Repository) Repository
GoesRepository returns a Repository that uses the provided aggregate repository under the hood.
type SortedData ¶
SortedData is the event data for Sorted.