From 0051ae71d9a1a5ceafee91ef928bdb72b2a4b40d Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 13 Aug 2024 18:17:39 +0200 Subject: [PATCH] Fix go services returning nulls I did not know that var foo []bar does not initialize foo to empty []! Fuck --- foodservice.go | 10 +++++----- weightservice.go | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/foodservice.go b/foodservice.go index ab982c1..973dcb1 100644 --- a/foodservice.go +++ b/foodservice.go @@ -35,7 +35,7 @@ const foodColumns = "rowid, date, food, description, amount, per100, energy" const foodAggregatedColumns = "period, amount, avgPer100, energy" func (s *FoodService) GetRecent() ([]Food, error) { - var res []Food + var res []Food = []Food{} if s.db == nil || !s.db.Ready { return res, fmt.Errorf("cannot get recent food, db is nil or is not ready") } @@ -183,7 +183,7 @@ func (s *FoodService) GetByRowid(rowid int64) (Food, error) { // I could probably refactor this to be less of a disaster... // But I think it'll work for now func (s *FoodService) GetDaily() ([]AggregatedFood, error) { - var res []AggregatedFood + res := []AggregatedFood{} if s.db == nil || !s.db.Ready { return res, fmt.Errorf("cannot get daily food, db is nil or is not ready") } @@ -209,7 +209,7 @@ func (s *FoodService) GetDaily() ([]AggregatedFood, error) { } func (s *FoodService) GetWeekly() ([]AggregatedFood, error) { - var res []AggregatedFood + res := []AggregatedFood{} if s.db == nil || !s.db.Ready { return res, fmt.Errorf("cannot get weekly food, db is nil or is not ready") } @@ -235,7 +235,7 @@ func (s *FoodService) GetWeekly() ([]AggregatedFood, error) { } func (s *FoodService) GetMonthly() ([]AggregatedFood, error) { - var res []AggregatedFood + res := []AggregatedFood{} if s.db == nil || !s.db.Ready { return res, fmt.Errorf("cannot get monthly food, db is nil or is not ready") } @@ -261,7 +261,7 @@ func (s *FoodService) GetMonthly() ([]AggregatedFood, error) { } func (s *FoodService) GetYearly() ([]AggregatedFood, error) { - var res []AggregatedFood + res := []AggregatedFood{} if s.db == nil || !s.db.Ready { return res, fmt.Errorf("cannot get yearly food, db is nil or is not ready") } diff --git a/weightservice.go b/weightservice.go index 82286ac..4dca002 100644 --- a/weightservice.go +++ b/weightservice.go @@ -24,7 +24,7 @@ const weightcolumns = "rowid, date, weight" const weightAggregatedColumns = "period, amount" func (w *WeightService) GetRecent() ([]Weight, error) { - var res []Weight + res := []Weight{} if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get recent weight, db is nil or is not ready") } @@ -88,7 +88,7 @@ func (w *WeightService) GetByRowid(rowid int64) (Weight, error) { // I could probably refactor this to be less of a disaster... // But I think it'll work for now func (w *WeightService) GetDaily() ([]AggregatedWeight, error) { - var res []AggregatedWeight + res := []AggregatedWeight{} if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get daily weight, db is nil or is not ready") } @@ -114,7 +114,7 @@ func (w *WeightService) GetDaily() ([]AggregatedWeight, error) { } func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) { - var res []AggregatedWeight + res := []AggregatedWeight{} if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get weekly weight, db is nil or is not ready") } @@ -140,7 +140,7 @@ func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) { } func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) { - var res []AggregatedWeight + res := []AggregatedWeight{} if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get monthly weight, db is nil or is not ready") } @@ -166,7 +166,7 @@ func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) { } func (w *WeightService) GetYearly() ([]AggregatedWeight, error) { - var res []AggregatedWeight + res := []AggregatedWeight{} if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get yearly weight, db is nil or is not ready") }