Fixed bugs and added hourly forecast
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
// cacheType, representing the abstract value of a CacheEntity
|
||||
type cacheType interface {
|
||||
Weather | Metrics | Wind | Forecast | Moon
|
||||
Weather | Metrics | Wind | DailyForecast | HourlyForecast | Moon
|
||||
}
|
||||
|
||||
// CacheEntity, representing the value of the cache
|
||||
@@ -23,20 +23,22 @@ type Cache[T cacheType] struct {
|
||||
|
||||
// Caches, representing a grouping of the various caches
|
||||
type Caches struct {
|
||||
WeatherCache Cache[Weather]
|
||||
MetricsCache Cache[Metrics]
|
||||
WindCache Cache[Wind]
|
||||
ForecastCache Cache[Forecast]
|
||||
MoonCache CacheEntity[Moon]
|
||||
WeatherCache Cache[Weather]
|
||||
MetricsCache Cache[Metrics]
|
||||
WindCache Cache[Wind]
|
||||
DailyForecastCache Cache[DailyForecast]
|
||||
HourlyForecastCache Cache[HourlyForecast]
|
||||
MoonCache CacheEntity[Moon]
|
||||
}
|
||||
|
||||
func InitCache() *Caches {
|
||||
return &Caches{
|
||||
WeatherCache: Cache[Weather]{Data: make(map[string]CacheEntity[Weather])},
|
||||
MetricsCache: Cache[Metrics]{Data: make(map[string]CacheEntity[Metrics])},
|
||||
WindCache: Cache[Wind]{Data: make(map[string]CacheEntity[Wind])},
|
||||
ForecastCache: Cache[Forecast]{Data: make(map[string]CacheEntity[Forecast])},
|
||||
MoonCache: CacheEntity[Moon]{element: Moon{}, timestamp: time.Time{}},
|
||||
WeatherCache: Cache[Weather]{Data: make(map[string]CacheEntity[Weather])},
|
||||
MetricsCache: Cache[Metrics]{Data: make(map[string]CacheEntity[Metrics])},
|
||||
WindCache: Cache[Wind]{Data: make(map[string]CacheEntity[Wind])},
|
||||
DailyForecastCache: Cache[DailyForecast]{Data: make(map[string]CacheEntity[DailyForecast])},
|
||||
HourlyForecastCache: Cache[HourlyForecast]{Data: make(map[string]CacheEntity[HourlyForecast])},
|
||||
MoonCache: CacheEntity[Moon]{element: Moon{}, timestamp: time.Time{}},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,3 +33,32 @@ func (date ZephyrDate) MarshalJSON() ([]byte, error) {
|
||||
|
||||
return []byte("\"" + fmtDate + "\""), nil
|
||||
}
|
||||
|
||||
type ZephyrTime struct {
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
func (t *ZephyrTime) UnmarshalJSON(b []byte) error {
|
||||
s := strings.Trim(string(b), "\"")
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
t.Time, err = time.Parse("15:04", s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t ZephyrTime) MarshalJSON() ([]byte, error) {
|
||||
if t.Time.IsZero() {
|
||||
return []byte("\"\""), nil
|
||||
}
|
||||
|
||||
fmtTime := t.Time.Format("3:04 PM")
|
||||
|
||||
return []byte("\"" + fmtTime + "\""), nil
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package types
|
||||
|
||||
// The ForecastEntity data type, representing the weather forecast
|
||||
// The DailyForecastEntity data type, representing the weather forecast
|
||||
// of a single day
|
||||
type ForecastEntity struct {
|
||||
type DailyForecastEntity struct {
|
||||
Date ZephyrDate `json:"date"`
|
||||
Min string `json:"min"`
|
||||
Max string `json:"max"`
|
||||
@@ -10,9 +10,26 @@ type ForecastEntity struct {
|
||||
Emoji string `json:"emoji"`
|
||||
FeelsLike string `json:"feelsLike"`
|
||||
Wind Wind `json:"wind"`
|
||||
RainProb string `json:"rainProbability"`
|
||||
}
|
||||
|
||||
// The Forecast data type, representing a set of ForecastEntity
|
||||
type Forecast struct {
|
||||
Forecast []ForecastEntity `json:"forecast"`
|
||||
// The DailyForecast data type, representing a set of DailyForecastEntity
|
||||
type DailyForecast struct {
|
||||
Forecast []DailyForecastEntity `json:"forecast"`
|
||||
}
|
||||
|
||||
// The HourlyForecastEntity data type, representing the weather forecast
|
||||
// of a single hour
|
||||
type HourlyForecastEntity struct {
|
||||
Time ZephyrTime `json:"time"`
|
||||
Temperature string `json:"temperature"`
|
||||
Condition string `json:"condition"`
|
||||
Emoji string `json:"emoji"`
|
||||
Wind Wind `json:"wind"`
|
||||
RainProb string `json:"rainProbability"`
|
||||
}
|
||||
|
||||
// The HourlyForecast data type, representing a set of HourlyForecastEntity
|
||||
type HourlyForecast struct {
|
||||
Forecast []HourlyForecastEntity `json:"forecast"`
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ package types
|
||||
type Weather struct {
|
||||
Date ZephyrDate `json:"date"`
|
||||
Temperature string `json:"temperature"`
|
||||
Min string `json:"min"`
|
||||
Max string `json:"max"`
|
||||
Condition string `json:"condition"`
|
||||
FeelsLike string `json:"feelsLike"`
|
||||
Emoji string `json:"emoji"`
|
||||
|
||||
Reference in New Issue
Block a user