Skip to content

Commit

Permalink
Move Account() to Accounts().MustGet()
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvio Böhler committed Aug 31, 2023
1 parent 2a1de83 commit a1288c4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
12 changes: 6 additions & 6 deletions lib/journal/performance/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func TestComputeFlows(t *testing.T) {
usd := ctx.Commodity("USD")
gbp := ctx.Commodity("GBP")
aapl := ctx.Commodity("AAPL")
portfolio := ctx.Account("Assets:Portfolio")
acc1 := ctx.Account("Assets:Acc1")
acc2 := ctx.Account("Assets:Acc2")
dividend := ctx.Account("Income:Dividends")
expense := ctx.Account("Expenses:Investments")
equity := ctx.Account("Equity:Equity")
portfolio := ctx.Accounts().MustGet("Assets:Portfolio")
acc1 := ctx.Accounts().MustGet("Assets:Acc1")
acc2 := ctx.Accounts().MustGet("Assets:Acc2")
dividend := ctx.Accounts().MustGet("Income:Dividends")
expense := ctx.Accounts().MustGet("Expenses:Investments")
equity := ctx.Accounts().MustGet("Equity:Equity")

chf.IsCurrency = true
usd.IsCurrency = true
Expand Down
2 changes: 1 addition & 1 deletion lib/journal/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func CloseAccounts(j *Journal, enable bool, partition date.Partition) DayFn {
// j.Day creates the entry for the given date as a side effect.
closingDays.Add(j.Day(d))
}
equityAccount := j.Registry.Account("Equity:Equity")
equityAccount := j.Registry.Accounts().MustGet("Equity:Equity")
return func(d *Day) error {
if closingDays.Has(d) {
for k, quantity := range quantities {
Expand Down
8 changes: 8 additions & 0 deletions lib/model/account/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (as *Registry) Create(a syntax.Account) (*Account, error) {
return as.Get(a.Extract())
}

func (as *Registry) MustGet(name string) *Account {
a, err := as.Get(name)
if err != nil {
panic(err)
}
return a
}

func isValidSegment(s string) bool {
if len(s) == 0 {
return false
Expand Down
15 changes: 3 additions & 12 deletions lib/model/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ func (reg Registry) GetAccount(name string) (*Account, error) {
return reg.accounts.Get(name)
}

// Account returns a commodity or panics.
func (reg Registry) Account(name string) *Account {
c, err := reg.GetAccount(name)
if err != nil {
panic(err)
}
return c
}

// GetCommodity returns a commodity.
func (reg Registry) GetCommodity(name string) (*Commodity, error) {
return reg.commodities.Get(name)
Expand All @@ -69,15 +60,15 @@ func (reg Registry) Commodity(name string) *Commodity {

// TBDAccount returns the TBD account.
func (reg Registry) TBDAccount() *Account {
return reg.Account("Expenses:TBD")
return reg.Accounts().MustGet("Expenses:TBD")
}

// ValuationAccountFor returns the valuation account which corresponds to
// the given Asset or Liability account.
func (reg Registry) ValuationAccountFor(a *Account) *Account {
suffix := a.Split()[1:]
segments := append(reg.Account("Income").Split(), suffix...)
return reg.Account(strings.Join(segments, ":"))
segments := append(reg.Accounts().MustGet("Income").Split(), suffix...)
return reg.Accounts().MustGet(strings.Join(segments, ":"))
}

// Accounts returns the accounts.
Expand Down

0 comments on commit a1288c4

Please sign in to comment.