Skip to content

Commit 4db3fb8

Browse files
samoz83mahendrapaipuri
authored andcommitted
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.
1 parent fe64f78 commit 4db3fb8

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

pkg/emissions/emaps.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,18 @@ func NewEMapsProvider(logger *slog.Logger) (Provider, error) {
8484
zones := make(map[string]string, len(zoneData))
8585

8686
for zone, details := range zoneData {
87-
// Check for countryName
88-
var compoundName string
87+
// Build compound name from countryName and zoneName
88+
var parts []string
8989

90-
for _, name := range []string{"countryName", "zoneName"} {
91-
if n, ok := details[name]; ok {
92-
compoundName = fmt.Sprintf("%s %s", compoundName, n)
93-
}
90+
if details.CountryName != "" {
91+
parts = append(parts, details.CountryName)
92+
}
93+
94+
if details.ZoneName != "" {
95+
parts = append(parts, details.ZoneName)
9496
}
95-
// Trim white spaces
96-
compoundName = strings.TrimSpace(compoundName)
97-
zones[zone] = compoundName
97+
98+
zones[zone] = strings.Join(parts, " ")
9899
}
99100

100101
e := &emapsProvider{

pkg/emissions/emaps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestEMapsDataProviderError(t *testing.T) {
106106

107107
func TestNewEMapsProvider(t *testing.T) {
108108
// Start test server
109-
expected := eMapsZonesResponse{"FR": map[string]string{"zoneName": "France"}}
109+
expected := eMapsZonesResponse{"FR": eMapsZoneDetails{ZoneName: "France"}}
110110

111111
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
112112
err := json.NewEncoder(w).Encode(&expected)

pkg/emissions/types.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,20 @@ type CountryCode struct {
136136
IsoCode []CountryCodeFields `json:"3166-1"`
137137
}
138138

139+
// eMapsZoneDetails represents the details of a zone from the Electricity Maps API.
140+
type eMapsZoneDetails struct {
141+
ZoneName string `json:"zoneName"`
142+
CountryName string `json:"countryName"`
143+
ZoneKey string `json:"zoneKey"`
144+
CountryCode string `json:"countryCode"`
145+
ZoneParentKey *string `json:"zoneParentKey"`
146+
SubZoneKeys []string `json:"subZoneKeys"`
147+
IsCommerciallyAvailable bool `json:"isCommerciallyAvailable"`
148+
Tier *string `json:"tier"`
149+
}
150+
139151
// Electricity Maps zones response signature.
140-
type eMapsZonesResponse map[string]map[string]string
152+
type eMapsZonesResponse map[string]eMapsZoneDetails
141153

142154
// Electricity Maps carbon intensity response signature.
143155
type eMapsCarbonIntensityResponse struct {

0 commit comments

Comments
 (0)