From de2e55e8eb1ced2e1ec3d0a8cefb85d6ce3b7dbc Mon Sep 17 00:00:00 2001 From: Robson Cruz Date: Fri, 10 Nov 2023 18:15:37 -0300 Subject: [PATCH] feat(utils.go): parametrize `CurrentWeekEntries` The function will now accept a time.Time value as param. This should improve testability, and make allow us to extend functionality refactor(utils.go): rename `CurrentWeekEntries` to make the intention more clear as the function has been changed to be parametrized it would now be possible to use it fetch entries from any week given a date, so this renaming will try to make that more clear BREAKING CHANGE: as both the signature and name of the function changed, any code calling it should be updated --- cmd/report_week.go | 4 +++- internal/utils.go | 17 ++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/report_week.go b/cmd/report_week.go index dc5543b..d5567f8 100644 --- a/cmd/report_week.go +++ b/cmd/report_week.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "time" journal "github.com/deadpyxel/workday/internal" "github.com/spf13/cobra" @@ -29,7 +30,8 @@ func reportWeek(cmd *cobra.Command, args []string) error { if err != nil { return err } - currentWeek, err := journal.CurrentWeekEntries(journalEntries) + now := time.Now() + currentWeek, err := journal.FetchEntriesByWeekDate(journalEntries, now) if err != nil { return err } diff --git a/internal/utils.go b/internal/utils.go index 129940b..5c59d4c 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -17,7 +17,7 @@ func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int) { return nil, -1 } -// CurrentWeekEntries filters a slice of JournalEntry objects and returns a new slice +// FetchEntriesByWeekDate filters a slice of JournalEntry objects and returns a new slice // containing only the entries from the current week. The function uses the ISO week date // system, where weeks start on a Monday and the first week of the year is the one that // includes at least four days of the new year. @@ -32,20 +32,19 @@ func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int) { // Example: // // entries := []JournalEntry{...} -// currentWeekEntries, err := CurrentWeekEntries(entries) +// currentWeekEntries, err := FetchEntriesByWeekDate(entries) // if err != nil { // log.Fatal(err) // } // fmt.Println(currentWeekEntries) // Prints the entries for the current week -func CurrentWeekEntries(journalEntries []JournalEntry) ([]JournalEntry, error) { - if journalEntries == nil { - return []JournalEntry{}, fmt.Errorf("No entries were passed") +func FetchEntriesByWeekDate(journalEntries []JournalEntry, currentDate time.Time) ([]JournalEntry, error) { + if len(journalEntries) == 0 { + return nil, fmt.Errorf("No entries were passed") } var currentWeekEntries []JournalEntry // Get the current year and ISO week number. - now := time.Now() - currentYear, currentWeek := now.ISOWeek() + currentYear, currentWeek := currentDate.ISOWeek() for _, entry := range journalEntries { // get the year and ISO week number of the entry. @@ -56,8 +55,8 @@ func CurrentWeekEntries(journalEntries []JournalEntry) ([]JournalEntry, error) { currentWeekEntries = append(currentWeekEntries, entry) } } - if currentWeekEntries == nil { - return currentWeekEntries, fmt.Errorf("No entries found for the current week") + if len(currentWeekEntries) == 0 { + return nil, fmt.Errorf("No entries found for the current week") } return currentWeekEntries, nil