categories

package
v0.0.0-...-f0c4f2b Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package categories maintains metadata on spending categories.

Index

Constants

This section is empty.

Variables

View Source
var (
	MalformedCategoryName        = errors.New("categories: Malformed catgory name")
	NoParentCategory             = errors.New("categories: No parent category")
	NoSuchCategory               = errors.New("categories: No such category")
	NeedExpenseIncomeCategory    = errors.New("categories: Need expense/income category")
	InvalidRename                = errors.New("categories: Invalid rename")
	InvalidRenameWouldCauseCycle = errors.New("categories: Rename would cause a cycle.")
	DuplicateCategoryName        = errors.New("categories: Duplicate category name.")
)

Functions

This section is empty.

Types

type AccountAdder

type AccountAdder interface {
	Add(name string) (newId int64, err error)
}

AccountAdder adds a new account to the database. name is the name of the account.

type AccountDetail

type AccountDetail struct {
	// contains filtered or unexported fields
}

AccountDetail represents account detail.

func (AccountDetail) Active

func (a AccountDetail) Active() bool

Active returns true if account is active.

func (AccountDetail) Id

func (a AccountDetail) Id() int64

Id returns the account Id.

func (AccountDetail) Name

func (a AccountDetail) Name() string

Name returns the account name.

type AccountDetailConsumer

type AccountDetailConsumer struct {
	// Builder is what is being populated
	Builder *CatDetailStoreBuilder
}

AccountDetailConsumer populates a CatDetailStoreBuilder value with accounts.

func (*AccountDetailConsumer) CanConsume

func (c *AccountDetailConsumer) CanConsume() bool

func (*AccountDetailConsumer) Consume

func (c *AccountDetailConsumer) Consume(account fin.Account)

type AccountRemover

type AccountRemover interface {
	Remove(id int64) error
}

AccountRemover removes a account in the database by marking it inactive.

type AccountUpdater

type AccountUpdater interface {
	Update(id int64, newName string) error
}

AccountUpdater updates a account in the database

type CatDbRow

type CatDbRow struct {
	Id       int64
	ParentId int64
	Name     string
	Active   bool
}

CatDbRow represents a category database row

type CatDetail

type CatDetail struct {
	// contains filtered or unexported fields
}

CatDetail represents category detail.

func MostPopularFirst

func MostPopularFirst(
	catDetails []CatDetail,
	catPopularity fin.CatPopularity,
	mostPopularRatio float64,
	maxMostPopularCount int) []CatDetail

MostPopularFirst returns catDetails prepended with the most popular categories followed by the least popular categories. MostPopularFirst prepends len(catDetails)*mostPopularRatio categories, but will not prepend more than maxMostPopularCount categories if maxMostPopularCount is >= 0. Categories with a popularity of 0 are never prepended.

func (CatDetail) Active

func (c CatDetail) Active() bool

Active returns true if category is active.

func (CatDetail) FullName

func (c CatDetail) FullName() string

FullName returns the full name of the category.

func (CatDetail) Id

func (c CatDetail) Id() fin.Cat

Id returns the category ID of the category.

type CatDetailConsumer

type CatDetailConsumer struct {
	// Builder is what is being populated
	Builder *CatDetailStoreBuilder
	// Type is either fin.ExpenseCat or fin.IncomeCat.
	Type fin.CatType
}

CatDetailConsumer populates a CatDetailStoreBuilder value with CatDbRow values.

func (*CatDetailConsumer) CanConsume

func (c *CatDetailConsumer) CanConsume() bool

func (*CatDetailConsumer) Consume

func (c *CatDetailConsumer) Consume(row CatDbRow)

type CatDetailStore

type CatDetailStore struct {
	// contains filtered or unexported fields
}

CatDetailStore contains all information on categories. The zero value of CatDetailStore contains only the trivial categories, "expense" and "income," and no accounts.

func (CatDetailStore) AccountAdd

func (cds CatDetailStore) AccountAdd(name string, adder AccountAdder) (
	updatedStore CatDetailStore, newId int64, err error)

AccountAdd adds a new account in the database and returns the updated store. Name is the name of the new account; adder adds the account to the database. On error, returns the receiver unchanged.

func (CatDetailStore) AccountDetailById

func (cds CatDetailStore) AccountDetailById(id int64) AccountDetail

AccountDetailById returns account details by acount Id.

func (CatDetailStore) AccountDetailByName

func (cds CatDetailStore) AccountDetailByName(name string) (accountDetail AccountDetail, exists bool)

AccountDetailByName returns account detail by account name. If no such account exists or is inactive, then exists is false.

func (CatDetailStore) AccountRemove

func (cds CatDetailStore) AccountRemove(
	id int64, remover AccountRemover) (
	updatedStore CatDetailStore, err error)

AccountRemove removes a account from database and returns the updated store. id specifies the account to be removed; remover does the actual remove in the database. On error, returns the receiver unchanged.

func (CatDetailStore) AccountRename

func (cds CatDetailStore) AccountRename(
	id int64, name string, updater AccountUpdater) (
	updatedStore CatDetailStore, err error)

AccountRename renames an account in the database and returns the updated store. id is the id of the account to be renamed; newName is the new name; updater does the rename in the database. On error, returns the receiver unchanged. If id represents an inactive account, it is made active when renamed.

func (CatDetailStore) ActiveAccountDetails

func (cds CatDetailStore) ActiveAccountDetails() []AccountDetail

ActiveAccountDetails returns all active details sorted by name.

func (CatDetailStore) ActiveCatDetails

func (cds CatDetailStore) ActiveCatDetails(accounts bool) []CatDetail

ActiveCatDetails returns all active category details sorted by full name. If accounts is true also includes account categories.

func (CatDetailStore) Add

func (cds CatDetailStore) Add(name string, adder CategoryAdder) (
	updatedStore CatDetailStore, newId fin.Cat, err error)

Add adds a new category in the database and returns the updated store. Name is the full name of the new category; adder adds the category to the database. On error, returns the receiver unchanged.

func (CatDetailStore) DetailByFullName

func (cds CatDetailStore) DetailByFullName(fullName string) (catDetail CatDetail, exists bool)

DetailByFullName returns details by category full name. If no such category exists or is inactive, then exists is false.

func (CatDetailStore) DetailById

func (cds CatDetailStore) DetailById(cat fin.Cat) CatDetail

DetailById returns details by category Id.

func (CatDetailStore) DetailsByIds

func (cds CatDetailStore) DetailsByIds(cats fin.CatSet) []CatDetail

DetailsByIds returns details sorted by full name for selected categories.

func (CatDetailStore) Filter

func (cds CatDetailStore) Filter(cat fin.Cat, includeChildren bool) fin.CatFilter

Filter returns a CatFilter for finding entries under a specific category.

func (CatDetailStore) ImmediateParent

func (cds CatDetailStore) ImmediateParent(cat fin.Cat) fin.Cat

ImmediateParent returns the immediate parent category of given category. If given category is already top level, returns the same top level category.

func (CatDetailStore) InactiveDetailByFullName

func (cds CatDetailStore) InactiveDetailByFullName(fullName string) (
	catDetail CatDetail, exists bool)

InactiveDetailByFullName returns details for inactive categories by category full name that can be made active again. If no such category exists or is active, or can't be made active because it has some ancestor category that is inactive then exists is false.

func (CatDetailStore) IsChildOf

func (cds CatDetailStore) IsChildOf(childCat, parentCat fin.Cat) bool

IsChildOf returns true if childCat is a child of parentCat.

func (CatDetailStore) LeafNameById

func (cds CatDetailStore) LeafNameById(cat fin.Cat) string

LeafNameById returns the category leaf name by category Id.

func (CatDetailStore) PurgeableAccounts

func (cds CatDetailStore) PurgeableAccounts(
	accountSet fin.AccountSet) fin.AccountSet

PurgeableAccounts returns account ids eligible for purging. An account is eligible for purging it it is inactive and not used in any entry. accountSet should include the account ids used in all entries in the data store.

func (CatDetailStore) PurgeableCats

func (cds CatDetailStore) PurgeableCats(total fin.CatTotals) fin.CatSet

PurgeableCats returns category ids eligible for purging. A category is eligible for purging it it is inactive and it nor any of its children are used in any entry. total should include totals for all entries in the data store. PurgeableCats calls RollUp on total internally. PurgeableCats will only return expense and income categories.

func (CatDetailStore) Remove

func (cds CatDetailStore) Remove(
	cat fin.Cat, remover CategoryRemover) (
	updatedStore CatDetailStore, err error)

Remove removes a category form database and returns the updated store. cat specifies the category to be removed; remover does the actual remove in the database. On error, returns the receiver unchanged.

func (CatDetailStore) Rename

func (cds CatDetailStore) Rename(id fin.Cat, newName string, updater CategoryUpdater) (
	updatedStore CatDetailStore, err error)

Rename renames a category in the database and returns the updated store. id is the id of the category to be renamed; newName is the new name; updater does the rename in the database. On error, returns the receiver unchanged. If id represents an inactive category, it is made active when renamed.

func (CatDetailStore) RollUp

func (cds CatDetailStore) RollUp(totals fin.CatTotals) (rolledUp fin.CatTotals, children map[fin.Cat]fin.CatSet)

RollUp rolls totals from child categories into parent categories. The children return value contains the immediate child categories that were rolled into each parent category.

func (CatDetailStore) SortedCatRecs

func (cds CatDetailStore) SortedCatRecs(catrecs []fin.CatRec) []fin.CatRec

SortedCatRecs sorts catrecs by category full name and returns catrecs.

type CatDetailStoreBuilder

type CatDetailStoreBuilder struct {
	// contains filtered or unexported fields
}

CatDetailsStoreBuilder is used to initialize CatDetailStore values.

func (*CatDetailStoreBuilder) AddAccount

func (cdsb *CatDetailStoreBuilder) AddAccount(
	account *fin.Account) *CatDetailStoreBuilder

AddAccount adds an account.

func (*CatDetailStoreBuilder) AddCatDbRow

func (cdsb *CatDetailStoreBuilder) AddCatDbRow(
	t fin.CatType, row *CatDbRow) *CatDetailStoreBuilder

AddCatDbRow adds expense or income category depending on t. t is either fin.ExpenseCat or fin.IncomeCat.

func (*CatDetailStoreBuilder) Build

func (cdsb *CatDetailStoreBuilder) Build() CatDetailStore

Build returns a new CatDetailStore and resets this builder.

type CategoryAdder

type CategoryAdder interface {
	Add(t fin.CatType, row *CatDbRow) error
}

CategoryAdder adds a new category to the database. t is fin.ExpenseCat or fin.IncomeCat. row is the category to be added.

type CategoryRemover

type CategoryRemover interface {
	Remove(t fin.CatType, id int64) error
}

CategoryRemover removes a category in the database by marking it inactive. t is fin.ExpenseCat or fin.IncomeCat. id specifies what is to be deleted.

type CategoryUpdater

type CategoryUpdater interface {
	Update(t fin.CatType, row *CatDbRow) error
}

CategoryUpdater updates a category in the database t is fin.ExpenseCat or fin.IncomeCat. row denotes the updated category.

type NamedCat

type NamedCat struct {
	Id   fin.Cat
	Name string
}

NamedCat represents a category Id and name

func Ancestors

func Ancestors(cds CatDetailStore, cat fin.Cat) []NamedCat

Ancestors returns all the ancestor categories of cat. The first item in returned slice is always one of the top level categories; the last item is always cat. The items in between are the other ancestor categories of cat with the most distant ones coming first. The name in each returned category is that category's leaf name.

Directories

Path Synopsis
Package categoriesdb contains the persistence layer for the categories package.
Package categoriesdb contains the persistence layer for the categories package.
fixture
Package fixture provides test suites to test implementations of the interfaces in the categoriesdb package.
Package fixture provides test suites to test implementations of the interfaces in the categoriesdb package.
for_sqlite
Package for_sqlite stores types in categories package in a sqlite database.
Package for_sqlite stores types in categories package in a sqlite database.

Jump to

Keyboard shortcuts

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