From 649f937254f2a9c589f1e609112e9c4ee6d29a7a Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sat, 19 Feb 2022 21:28:28 +0000 Subject: [PATCH] Remove context from YNABImport struct --- postgres/ynab-import.go | 42 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/postgres/ynab-import.go b/postgres/ynab-import.go index 6d28488..1438659 100644 --- a/postgres/ynab-import.go +++ b/postgres/ynab-import.go @@ -13,7 +13,6 @@ import ( ) type YNABImport struct { - Context context.Context accounts []Account payees []Payee categories []GetCategoriesRow @@ -44,7 +43,6 @@ func NewYNABImport(context context.Context, q *Queries, budgetID uuid.UUID) (*YN } return &YNABImport{ - Context: context, accounts: accounts, payees: payees, categories: categories, @@ -60,7 +58,7 @@ func NewYNABImport(context context.Context, q *Queries, budgetID uuid.UUID) (*YN //"Apr 2019" "Income: Next Month" "Income" "Next Month" 0,00€ 0,00€ 0,00€ // // Activity and Available are not imported, since they are determined by the transactions and historic assignments -func (ynab *YNABImport) ImportAssignments(r io.Reader) error { +func (ynab *YNABImport) ImportAssignments(context context.Context, r io.Reader) error { csv := csv.NewReader(r) csv.Comma = '\t' csv.LazyQuotes = true @@ -80,7 +78,7 @@ func (ynab *YNABImport) ImportAssignments(r io.Reader) error { } categoryGroup, categoryName := record[2], record[3] //also in 1 joined by : - category, err := ynab.GetCategory(categoryGroup, categoryName) + category, err := ynab.GetCategory(context, categoryGroup, categoryName) if err != nil { return fmt.Errorf("get category %s/%s: %w", categoryGroup, categoryName, err) } @@ -100,7 +98,7 @@ func (ynab *YNABImport) ImportAssignments(r io.Reader) error { CategoryID: category.UUID, Amount: amount, } - _, err = ynab.queries.CreateAssignment(ynab.Context, assignment) + _, err = ynab.queries.CreateAssignment(context, assignment) if err != nil { return fmt.Errorf("save assignment %v: %w", assignment, err) } @@ -122,7 +120,7 @@ type Transfer struct { // ImportTransactions expects a TSV-file as exported by YNAB in the following format: -func (ynab *YNABImport) ImportTransactions(r io.Reader) error { +func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader) error { csv := csv.NewReader(r) csv.Comma = '\t' csv.LazyQuotes = true @@ -137,7 +135,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { count := 0 for _, record := range csvData[1:] { accountName := record[0] - account, err := ynab.GetAccount(accountName) + account, err := ynab.GetAccount(context, accountName) if err != nil { return fmt.Errorf("get account %s: %w", accountName, err) } @@ -151,7 +149,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { } categoryGroup, categoryName := record[5], record[6] //also in 4 joined by : - category, err := ynab.GetCategory(categoryGroup, categoryName) + category, err := ynab.GetCategory(context, categoryGroup, categoryName) if err != nil { return fmt.Errorf("get category %s/%s: %w", categoryGroup, categoryName, err) } @@ -188,7 +186,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { if strings.HasPrefix(payeeName, "Transfer : ") { // Transaction is a transfer to transferToAccountName := payeeName[11:] - transferToAccount, err := ynab.GetAccount(transferToAccountName) + transferToAccount, err := ynab.GetAccount(context, transferToAccountName) if err != nil { return fmt.Errorf("get transfer account %s: %w", transferToAccountName, err) } @@ -221,11 +219,11 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { transfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true} openTransfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true} - _, err = ynab.queries.CreateTransaction(ynab.Context, transfer.CreateTransactionParams) + _, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams) if err != nil { return fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err) } - _, err = ynab.queries.CreateTransaction(ynab.Context, openTransfer.CreateTransactionParams) + _, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams) if err != nil { return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err) } @@ -236,13 +234,13 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { openTransfers = append(openTransfers, transfer) } } else { - payeeID, err := ynab.GetPayee(payeeName) + payeeID, err := ynab.GetPayee(context, payeeName) if err != nil { return fmt.Errorf("get payee %s: %w", payeeName, err) } transaction.PayeeID = payeeID - _, err = ynab.queries.CreateTransaction(ynab.Context, transaction) + _, err = ynab.queries.CreateTransaction(context, transaction) if err != nil { return fmt.Errorf("save transaction %v: %w", transaction, err) } @@ -253,7 +251,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error { for _, openTransfer := range openTransfers { fmt.Printf("Saving unmatched transfer from %s to %s on %s over %f as regular transaction\n", openTransfer.FromAccount, openTransfer.ToAccount, openTransfer.Date, openTransfer.Amount.GetFloat64()) - _, err = ynab.queries.CreateTransaction(ynab.Context, openTransfer.CreateTransactionParams) + _, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams) if err != nil { return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err) } @@ -295,14 +293,14 @@ func GetAmount(inflow string, outflow string) (Numeric, error) { return num, nil } -func (ynab *YNABImport) GetAccount(name string) (*Account, error) { +func (ynab *YNABImport) GetAccount(context context.Context, name string) (*Account, error) { for _, acc := range ynab.accounts { if acc.Name == name { return &acc, nil } } - account, err := ynab.queries.CreateAccount(ynab.Context, CreateAccountParams{Name: name, BudgetID: ynab.budgetID}) + account, err := ynab.queries.CreateAccount(context, CreateAccountParams{Name: name, BudgetID: ynab.budgetID}) if err != nil { return nil, err } @@ -311,7 +309,7 @@ func (ynab *YNABImport) GetAccount(name string) (*Account, error) { return &account, nil } -func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) { +func (ynab *YNABImport) GetPayee(context context.Context, name string) (uuid.NullUUID, error) { if name == "" { return uuid.NullUUID{}, nil } @@ -322,7 +320,7 @@ func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) { } } - payee, err := ynab.queries.CreatePayee(ynab.Context, CreatePayeeParams{Name: name, BudgetID: ynab.budgetID}) + payee, err := ynab.queries.CreatePayee(context, CreatePayeeParams{Name: name, BudgetID: ynab.budgetID}) if err != nil { return uuid.NullUUID{}, err } @@ -331,7 +329,7 @@ func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) { return uuid.NullUUID{UUID: payee.ID, Valid: true}, nil } -func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, error) { +func (ynab *YNABImport) GetCategory(context context.Context, group string, name string) (uuid.NullUUID, error) { if group == "" || name == "" { return uuid.NullUUID{}, nil } @@ -345,7 +343,7 @@ func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, e for _, categoryGroup := range ynab.categoryGroups { if categoryGroup.Name == group { createCategory := CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID} - category, err := ynab.queries.CreateCategory(ynab.Context, createCategory) + category, err := ynab.queries.CreateCategory(context, createCategory) if err != nil { return uuid.NullUUID{}, err } @@ -361,13 +359,13 @@ func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, e } } - categoryGroup, err := ynab.queries.CreateCategoryGroup(ynab.Context, CreateCategoryGroupParams{Name: group, BudgetID: ynab.budgetID}) + categoryGroup, err := ynab.queries.CreateCategoryGroup(context, CreateCategoryGroupParams{Name: group, BudgetID: ynab.budgetID}) if err != nil { return uuid.NullUUID{}, err } ynab.categoryGroups = append(ynab.categoryGroups, categoryGroup) - category, err := ynab.queries.CreateCategory(ynab.Context, CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}) + category, err := ynab.queries.CreateCategory(context, CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}) if err != nil { return uuid.NullUUID{}, err }