From 87605024c7f7cb99a5ac7ee06665079983c8dbac Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Wed, 18 Jun 2025 08:44:52 +0200 Subject: [PATCH] Fixed cache data violation bug --- .gitignore | 1 + controller/controller.go | 45 ++++++++++++++++++++++++++-------------- model/forecastModel.go | 2 +- model/weatherModel.go | 2 +- types/date.go | 8 +++---- 5 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d74e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ diff --git a/controller/controller.go b/controller/controller.go index 531f38b..79ed320 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -47,6 +47,19 @@ func fmtWind(windSpeed string, isImperial bool) string { return fmt.Sprintf("%.1f km/h", (parsedSpeed * 3.6)) } +func deepCopyForecast(original types.Forecast) types.Forecast { + // Copy the outer structure + fc_copy := original + + // Allocate enough space + fc_copy.Forecast = make([]types.ForecastEntity, len(original.Forecast)) + + // Copy inner structure + copy(fc_copy.Forecast, original.Forecast) + + return fc_copy +} + func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Weather], vars *types.Variables) { if req.Method != http.MethodGet { jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed) @@ -202,15 +215,19 @@ func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[ cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive) if found { + forecast := deepCopyForecast(cachedValue) + // Format forecast object and then return it - for idx := range cachedValue.Forecast { - cachedValue.Forecast[idx].Min = fmtTemperature(cachedValue.Forecast[idx].Min, isImperial) - cachedValue.Forecast[idx].Max = fmtTemperature(cachedValue.Forecast[idx].Max, isImperial) - cachedValue.Forecast[idx].FeelsLike = fmtTemperature(cachedValue.Forecast[idx].FeelsLike, isImperial) - cachedValue.Forecast[idx].Wind.Speed = fmtWind(cachedValue.Forecast[idx].Wind.Speed, isImperial) + for idx := range forecast.Forecast { + val := &forecast.Forecast[idx] + + val.Min = fmtTemperature(val.Min, isImperial) + val.Max = fmtTemperature(val.Max, isImperial) + val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial) + val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial) } - jsonValue(res, cachedValue) + jsonValue(res, forecast) } else { // Get city coordinates city, err := model.GetCoordinates(cityName, vars.Token) @@ -227,18 +244,16 @@ func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[ } // Add result to cache - cache.AddEntry(forecast, cityName) - - // ***************** - // FIXME: formatting 'forecast' alters cached value - // ***************** + cache.AddEntry(deepCopyForecast(forecast), cityName) // Format forecast object and then return it for idx := range forecast.Forecast { - forecast.Forecast[idx].Min = fmtTemperature(forecast.Forecast[idx].Min, isImperial) - forecast.Forecast[idx].Max = fmtTemperature(forecast.Forecast[idx].Max, isImperial) - forecast.Forecast[idx].FeelsLike = fmtTemperature(forecast.Forecast[idx].FeelsLike, isImperial) - forecast.Forecast[idx].Wind.Speed = fmtWind(forecast.Forecast[idx].Wind.Speed, isImperial) + val := &forecast.Forecast[idx] + + val.Min = fmtTemperature(val.Min, isImperial) + val.Max = fmtTemperature(val.Max, isImperial) + val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial) + val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial) } jsonValue(res, forecast) diff --git a/model/forecastModel.go b/model/forecastModel.go index 13a6f59..dd3e01c 100644 --- a/model/forecastModel.go +++ b/model/forecastModel.go @@ -43,7 +43,7 @@ type forecastRes struct { func getForecastEntity(dailyForecast dailyRes) types.ForecastEntity { // Format UNIX timestamp as 'YYYY-MM-DD' utcTime := time.Unix(int64(dailyForecast.Timestamp), 0) - weatherDate := &types.ZephyrDate{Time: utcTime.UTC()} + weatherDate := &types.ZephyrDate{Date: utcTime.UTC()} // Set condition accordingly to weather description var condition string diff --git a/model/weatherModel.go b/model/weatherModel.go index 493a3cc..0fc07a1 100644 --- a/model/weatherModel.go +++ b/model/weatherModel.go @@ -86,7 +86,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) { // Format UNIX timestamp as 'YYYY-MM-DD' utcTime := time.Unix(int64(weather.Current.Timestamp), 0) - weatherDate := &types.ZephyrDate{Time: utcTime.UTC()} + weatherDate := &types.ZephyrDate{Date: utcTime.UTC()} // Set condition accordingly to weather description var condition string diff --git a/types/date.go b/types/date.go index a2e27bb..8d5a9f2 100644 --- a/types/date.go +++ b/types/date.go @@ -6,7 +6,7 @@ import ( ) type ZephyrDate struct { - time.Time + Date time.Time } func (date *ZephyrDate) UnmarshalJSON(b []byte) error { @@ -16,7 +16,7 @@ func (date *ZephyrDate) UnmarshalJSON(b []byte) error { } var err error - date.Time, err = time.Parse("Monday, 2006/01/02", s) + date.Date, err = time.Parse("Monday, 2006/01/02", s) if err != nil { return err } @@ -25,11 +25,11 @@ func (date *ZephyrDate) UnmarshalJSON(b []byte) error { } func (date *ZephyrDate) MarshalJSON() ([]byte, error) { - if date.Time.IsZero() { + if date.Date.IsZero() { return []byte("\"\""), nil } - fmtDate := date.Time.Format("Monday, 2006/01/02") + fmtDate := date.Date.Format("Monday, 2006/01/02") return []byte("\"" + fmtDate + "\""), nil }