Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Bishop committed Apr 4, 2022
1 parent df42262 commit 4241fb1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 51 deletions.
13 changes: 7 additions & 6 deletions ability_cash/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

type LedgerConverter struct {
Accounts schema.AccountsMap
Classifiers schema.ClassifiersMap
AccountClassifier string
GenerateEquity bool
Db schema.Database
Expand Down Expand Up @@ -70,12 +68,15 @@ func (c *LedgerConverter) AccountsList() <-chan string {
list := make(chan string)

go func() {
for _, account := range c.Accounts {
list <- account
for _, account := range *c.Db.GetAccounts() {
list <- account.Name
}
for _, account := range c.Classifiers[schema.ExpensesClassifier] {
list <- account
if accounts, ok := (*c.Db.GetClassifiers())[schema.ExpensesClassifier]; ok {
for _, account := range accounts {
list <- account
}
}

close(list)
}()

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
app := cli.App{
Name: "abilitycash2ledger",
Usage: "abilitycash db to ledger converter",
Version: "0.0.3",
Version: "0.0.4",
Commands: []*cli.Command{
{
Name: "add",
Expand Down
2 changes: 0 additions & 2 deletions scope/datafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ func (d *datafile) export(s *scope) (err error) {
}

converter := &ability_cash.LedgerConverter{
Accounts: s.Accounts,
Classifiers: s.Classifiers,
GenerateEquity: d.Equity,
Db: d.db,
}
Expand Down
47 changes: 5 additions & 42 deletions scope/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ import (
"fmt"
"path"
"strings"

"github.com/Bishop/abilitycash2ledger/ability_cash/schema"
)

func NewScope() *scope {
return new(scope)
}

type scope struct {
Datafiles []*datafile `json:"datafiles"`
Accounts schema.AccountsMap `json:"accounts"`
Classifiers schema.ClassifiersMap `json:"classifiers"`
Datafiles []*datafile `json:"datafiles"`
}

func (s *scope) AddFile(name string) error {
Expand All @@ -37,12 +33,10 @@ func (s *scope) AddFile(name string) error {
}

func (s *scope) Validate() ([]string, error) {
messages := make([]string, 0)

s.init()

var err error

messages := make([]string, 0)

for _, datafile := range s.Datafiles {
if !datafile.Active {
continue
Expand All @@ -52,37 +46,16 @@ func (s *scope) Validate() ([]string, error) {
return nil, err
}

for _, account := range *datafile.db.GetAccounts() {
if _, ok := s.Accounts[account.Name]; !ok {
s.Accounts[account.Name] = account.Name
}
}

for name, c := range *datafile.db.GetClassifiers() {
if _, ok := s.Classifiers[name]; !ok {
s.Classifiers[name] = make(schema.AccountsMap)
}

for _, category := range c {
if _, ok := s.Classifiers[name][category]; !ok {
s.Classifiers[name][category] = category
}
}
}
_ = *datafile.db.GetAccounts()
_ = *datafile.db.GetClassifiers()

messages = append(messages, fmt.Sprintf("file %s is ok; found %d transactions\n", datafile.Path, len(*datafile.db.GetTransactions())))

if err != nil {
return nil, err
}
}

return messages, nil
}

func (s *scope) Export() error {
s.init()

var err error

for _, datafile := range s.Datafiles {
Expand All @@ -99,13 +72,3 @@ func (s *scope) Export() error {
}
return nil
}

func (s *scope) init() {
if s.Accounts == nil {
s.Accounts = make(schema.AccountsMap)
}

if s.Classifiers == nil {
s.Classifiers = make(schema.ClassifiersMap)
}
}

0 comments on commit 4241fb1

Please sign in to comment.