From 86a3ffdcec0f74f74657177f87994d595a917b0f Mon Sep 17 00:00:00 2001 From: samoz83 Date: Wed, 25 Feb 2026 13:29:58 +0000 Subject: [PATCH] fix(emissions): update eMapsZonesResponse to match current Electricity Maps API The /v3/zones endpoint now returns non-string typed fields (arrays,booleans, nullable strings) which cannot be unmarshaled into map[string]map[string]string. Replace with a proper struct. --- pkg/emissions/emaps.go | 19 ++++++++++--------- pkg/emissions/emaps_test.go | 2 +- pkg/emissions/types.go | 14 +++++++++++++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/emissions/emaps.go b/pkg/emissions/emaps.go index a1981541..3c5d35a3 100644 --- a/pkg/emissions/emaps.go +++ b/pkg/emissions/emaps.go @@ -84,17 +84,18 @@ func NewEMapsProvider(logger *slog.Logger) (Provider, error) { zones := make(map[string]string, len(zoneData)) for zone, details := range zoneData { - // Check for countryName - var compoundName string + // Build compound name from countryName and zoneName + var parts []string - for _, name := range []string{"countryName", "zoneName"} { - if n, ok := details[name]; ok { - compoundName = fmt.Sprintf("%s %s", compoundName, n) - } + if details.CountryName != "" { + parts = append(parts, details.CountryName) + } + + if details.ZoneName != "" { + parts = append(parts, details.ZoneName) } - // Trim white spaces - compoundName = strings.TrimSpace(compoundName) - zones[zone] = compoundName + + zones[zone] = strings.Join(parts, " ") } e := &emapsProvider{ diff --git a/pkg/emissions/emaps_test.go b/pkg/emissions/emaps_test.go index 70587430..a1a31f55 100644 --- a/pkg/emissions/emaps_test.go +++ b/pkg/emissions/emaps_test.go @@ -106,7 +106,7 @@ func TestEMapsDataProviderError(t *testing.T) { func TestNewEMapsProvider(t *testing.T) { // Start test server - expected := eMapsZonesResponse{"FR": map[string]string{"zoneName": "France"}} + expected := eMapsZonesResponse{"FR": eMapsZoneDetails{ZoneName: "France"}} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err := json.NewEncoder(w).Encode(&expected) diff --git a/pkg/emissions/types.go b/pkg/emissions/types.go index 0357dc6a..c49e8424 100644 --- a/pkg/emissions/types.go +++ b/pkg/emissions/types.go @@ -136,8 +136,20 @@ type CountryCode struct { IsoCode []CountryCodeFields `json:"3166-1"` } +// eMapsZoneDetails represents the details of a zone from the Electricity Maps API. +type eMapsZoneDetails struct { + ZoneName string `json:"zoneName"` + CountryName string `json:"countryName"` + ZoneKey string `json:"zoneKey"` + CountryCode string `json:"countryCode"` + ZoneParentKey *string `json:"zoneParentKey"` + SubZoneKeys []string `json:"subZoneKeys"` + IsCommerciallyAvailable bool `json:"isCommerciallyAvailable"` + Tier *string `json:"tier"` +} + // Electricity Maps zones response signature. -type eMapsZonesResponse map[string]map[string]string +type eMapsZonesResponse map[string]eMapsZoneDetails // Electricity Maps carbon intensity response signature. type eMapsCarbonIntensityResponse struct {