Added moon route and changed Date fields from pointers to values
This commit is contained in:
@@ -4,19 +4,19 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// CacheType, representing the abstract value of a CacheEntity
|
||||
type CacheType interface {
|
||||
Weather | Metrics | Wind | Forecast
|
||||
// cacheType, representing the abstract value of a CacheEntity
|
||||
type cacheType interface {
|
||||
Weather | Metrics | Wind | Forecast | Moon
|
||||
}
|
||||
|
||||
// CacheEntity, representing the value of the cache
|
||||
type CacheEntity[T CacheType] struct {
|
||||
Element T
|
||||
Timestamp time.Time
|
||||
type CacheEntity[T cacheType] struct {
|
||||
element T
|
||||
timestamp time.Time
|
||||
}
|
||||
|
||||
// Cache, representing a mapping between a key(str) and a CacheEntity
|
||||
type Cache[T CacheType] struct {
|
||||
type Cache[T cacheType] struct {
|
||||
Data map[string]CacheEntity[T]
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ type Caches struct {
|
||||
MetricsCache Cache[Metrics]
|
||||
WindCache Cache[Wind]
|
||||
ForecastCache Cache[Forecast]
|
||||
MoonCache CacheEntity[Moon]
|
||||
}
|
||||
|
||||
func InitCache() *Caches {
|
||||
@@ -34,6 +35,7 @@ func InitCache() *Caches {
|
||||
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{}},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,25 +44,50 @@ func (cache *Cache[T]) GetEntry(key string, ttl int8) (T, bool) {
|
||||
|
||||
// If key is not present, return a zero value
|
||||
if !isPresent {
|
||||
return val.Element, false
|
||||
return val.element, false
|
||||
}
|
||||
|
||||
// Otherwise check whether cache element is expired
|
||||
currentTime := time.Now()
|
||||
expired := currentTime.Sub(val.Timestamp) > (time.Duration(ttl) * time.Hour)
|
||||
expired := currentTime.Sub(val.timestamp) > (time.Duration(ttl) * time.Hour)
|
||||
if expired {
|
||||
return val.Element, false
|
||||
return val.element, false
|
||||
}
|
||||
|
||||
return val.Element, true
|
||||
return val.element, true
|
||||
}
|
||||
|
||||
func (cache *Cache[T]) AddEntry(entry T, cityName string) {
|
||||
currentTime := time.Now()
|
||||
|
||||
cache.Data[cityName] = CacheEntity[T]{
|
||||
Element: entry,
|
||||
Timestamp: currentTime,
|
||||
element: entry,
|
||||
timestamp: currentTime,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (moon *CacheEntity[Moon]) GetEntry(ttl int8) (Moon, bool) {
|
||||
var zeroMoon Moon
|
||||
|
||||
// If moon data is not present, return a zero value
|
||||
if moon == nil {
|
||||
return zeroMoon, false
|
||||
}
|
||||
|
||||
// Otherwise check whether the element is expired
|
||||
currentTime := time.Now()
|
||||
expired := currentTime.Sub(moon.timestamp) > (time.Duration(ttl) * time.Hour)
|
||||
if expired {
|
||||
return zeroMoon, false
|
||||
}
|
||||
|
||||
return moon.element, true
|
||||
}
|
||||
|
||||
func (cache *CacheEntity[Moon]) AddEntry(entry Moon) {
|
||||
currentTime := time.Now()
|
||||
|
||||
cache.element = entry
|
||||
cache.timestamp = currentTime
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (date *ZephyrDate) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (date *ZephyrDate) MarshalJSON() ([]byte, error) {
|
||||
func (date ZephyrDate) MarshalJSON() ([]byte, error) {
|
||||
if date.Date.IsZero() {
|
||||
return []byte("\"\""), nil
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package types
|
||||
// The ForecastEntity data type, representing the weather forecast
|
||||
// of a single day
|
||||
type ForecastEntity struct {
|
||||
Date *ZephyrDate `json:"date"`
|
||||
Min string `json:"min"`
|
||||
Max string `json:"max"`
|
||||
Condition string `json:"condition"`
|
||||
Emoji string `json:"emoji"`
|
||||
FeelsLike string `json:"feelsLike"`
|
||||
Wind Wind `json:"wind"`
|
||||
Date ZephyrDate `json:"date"`
|
||||
Min string `json:"min"`
|
||||
Max string `json:"max"`
|
||||
Condition string `json:"condition"`
|
||||
Emoji string `json:"emoji"`
|
||||
FeelsLike string `json:"feelsLike"`
|
||||
Wind Wind `json:"wind"`
|
||||
}
|
||||
|
||||
// The Forecast data type, representing the an set
|
||||
|
||||
9
types/moon.go
Normal file
9
types/moon.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package types
|
||||
|
||||
// The Moon data type, representing the moon phase,
|
||||
// the moon phase icon and the moon progress(%).
|
||||
type Moon struct {
|
||||
Icon string `json:"icon"`
|
||||
Phase string `json:"phase"`
|
||||
Percentage string `json:"percentage"`
|
||||
}
|
||||
@@ -2,9 +2,9 @@ package types
|
||||
|
||||
// The Weather data type, representing the weather of a certain location
|
||||
type Weather struct {
|
||||
Date *ZephyrDate `json:"date"`
|
||||
Temperature string `json:"temperature"`
|
||||
Condition string `json:"condition"`
|
||||
FeelsLike string `json:"feelsLike"`
|
||||
Emoji string `json:"emoji"`
|
||||
Date ZephyrDate `json:"date"`
|
||||
Temperature string `json:"temperature"`
|
||||
Condition string `json:"condition"`
|
||||
FeelsLike string `json:"feelsLike"`
|
||||
Emoji string `json:"emoji"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user