nav

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
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"
)
View Source
const (
	Label      = ItemType("label")
	StaticLink = ItemType("static_link")
)

Item types

View Source
const Aggregate = "cms.static.nav"

Aggregate is the name of the Nav aggregate.

View Source
const (
	// CreateCommand is the command for creating a Nav.
	CreateCommand = "cms.static.nav.create"
)

Variables

View Source
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")
)

Events are all navigation events.

Functions

func CreateCmd

func CreateCmd(name string, items ...Item) command.Command

CreateCmd returns the command for creating a Nav.

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(id, path, label string, opts ...ItemOption) Item

NewStaticLink returns an Item of type StaticLink with the given default path and label.

func (Item) Label

func (i Item) Label(locale string) string

Label returns the label for the given locale or the default label.

func (Item) Path

func (i Item) Path(locale string) string

Path returns the path for the given locale or the default path.

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 ItemType

type ItemType string

ItemType is an Item type.

type ItemsAddedData

type ItemsAddedData struct {
	Items []Item
	Index int
	Path  string
}

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.

func NewLookup

func NewLookup() *Lookup

NewLookup returns a new Lookup.

func (*Lookup) ApplyEvent

func (l *Lookup) ApplyEvent(evt event.Event)

ApplyEvent applies events.

func (*Lookup) Name

func (l *Lookup) Name(name string) (uuid.UUID, bool)

Name returns the UUID of the Nav with the given name, or false.

func (*Lookup) Project

func (l *Lookup) Project(ctx context.Context, bus event.Bus, store event.Store, opts ...schedule.ContinuousOption) (<-chan error, error)

Project projects the Lookup in a new goroutine and returns a channel of asynchronous errors.

type Nav struct {
	*aggregate.Base
	*Tree

	Name string
}

Nav is a navigation.

func Create

func Create(name string, items ...Item) (*Nav, error)

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

func CreateWithID(id uuid.UUID, name string, items ...Item) (*Nav, error)

CreateWithID does the same as Create, but accepts a custom UUID.

func New

func New(id uuid.UUID) *Nav

New returns an uncreated Nav n. Call n.Create with the name of the Nav to create it.

func (nav *Nav) Append(items ...Item) error

Append appends Items at the root level of the navigation.

func (nav *Nav) AppendAt(path string, items ...Item) error

AppendAt appends Items at the given path.

func (nav *Nav) ApplyEvent(evt event.Event)

ApplyEvent applies aggregate events.

func (nav *Nav) Create(name string) error

Create creates the navigation by giving it a name.

func (nav *Nav) HasItem(items ...string) bool

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 *Nav) Insert(index int, items ...Item) error

Insert inserts Items at the given index at the root level of the navigation.

func (nav *Nav) InsertAt(path string, index int, items ...Item) error

InsertAt inserts Items at the given index of the Tree at path.

func (n *Nav) MarshalJSON() ([]byte, error)
func (nav *Nav) Prepend(items ...Item) error

Prepend prepends Items at the root level of the navigation.

func (nav *Nav) PrependAt(path string, items ...Item) error

PrependAt prepends Items at the given path.

func (nav *Nav) Remove(items ...string) error

Remove removes Items from Nav. Remove accepts dot-seperated paths to reference nested Items:

nav.Remove("foo.bar.baz")
func (nav *Nav) Sort(sorting []string)

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 *Nav) SortAt(path string, sorting []string)

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 (n *Nav) UnmarshalJSON(b []byte) error

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

func (c *ReadCache) Fetch(ctx context.Context, id uuid.UUID) (*Nav, error)

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

type SortedData struct {
	Sorting []string
	Path    string
}

SortedData is the event data for Sorted.

type Tree

type Tree struct {
	Items []Item `json:"items"`
}

Tree is an Item tree.

func NewTree

func NewTree(items ...Item) *Tree

NewTree returns a Tree of the given Items.

func (*Tree) Item

func (t *Tree) Item(path string) (Item, error)

Item returns the Item at the given path, or ErrItemNotFound.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL