Skip to content

Commit

Permalink
Fix NPE on single user request
Browse files Browse the repository at this point in the history
  • Loading branch information
berlam committed Oct 4, 2019
1 parent f7f9dc9 commit 8889af5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 9 additions & 3 deletions pkg/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (ts Timesheet) ReadCsv(data []byte, spec *CsvSpecification) (Timesheet, err
}
}

func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmptyLine bool) {
func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmptyLine, seconds bool) {
if len(ts) == 0 {
return
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
currentUser := ts[0].User
currentDate := time.Date(ts[0].Date.Year(), time.Month(ts[0].Date.Month()), 1, 0, 0, 0, 0, time.UTC)
for _, effort := range ts {
if currentUser.DisplayName != effort.User.DisplayName {
if currentUser != nil && currentUser.DisplayName != effort.User.DisplayName {
if currentDate.Day() > 1 {
emptyLinesForDaysBetween(csvw, spec, currentDate, currentDate.AddDate(0, 1, 1-currentDate.Day()), currentUser)
}
Expand Down Expand Up @@ -221,7 +221,13 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
result[spec.date.index] = effort.Date.Format(IsoYearMonthDay)
}
if spec.duration.enabled {
result[spec.duration.index] = effort.Duration.String()
var duration string
if seconds {
duration = fmt.Sprintf("%.0f", effort.Duration.Seconds())
} else {
duration = effort.Duration.String()
}
result[spec.duration.index] = duration
}

err := csvw.Write(result)
Expand Down
12 changes: 8 additions & 4 deletions pkg/timesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (user User) Normalize() string {

func (ts Timesheet) sortByUserAndDateAndProjectAndTask() Timesheet {
sort.Slice(ts, func(i, j int) bool {
if ts[i].User.DisplayName != ts[j].User.DisplayName {
if ts[i].User != nil && ts[j].User != nil && ts[i].User.DisplayName != ts[j].User.DisplayName {
return ts[i].User.DisplayName < ts[j].User.DisplayName
}
if ts[i].Date != ts[j].Date {
Expand All @@ -79,7 +79,11 @@ func (ts Timesheet) summarize() Timesheet {
}
sum := map[Key]*Effort{}
for _, effort := range ts {
key := Key{effort.User.DisplayName, effort.Date}
name := ""
if effort.User != nil {
name = effort.User.DisplayName
}
key := Key{name, effort.Date}
tmp := sum[key]
if tmp == nil {
sum[key] = &Effort{
Expand All @@ -100,12 +104,12 @@ func (ts Timesheet) summarize() Timesheet {
return timesheet
}

func (ts Timesheet) Print(writer io.Writer, user, summarize, printEmptyLine bool) {
func (ts Timesheet) Print(writer io.Writer, user, summarize, printEmptyLine, seconds bool) {
spec := NewCsvSpecification().User(user).Date(true).Project(!summarize).Task(!summarize).Duration(true).Description(!summarize)

timesheet := ts
if summarize {
timesheet = timesheet.summarize()
}
timesheet.sortByUserAndDateAndProjectAndTask().WriteCsv(writer, &spec, printEmptyLine)
timesheet.sortByUserAndDateAndProjectAndTask().WriteCsv(writer, &spec, printEmptyLine, seconds)
}

0 comments on commit 8889af5

Please sign in to comment.