Skip to content

Commit 273a825

Browse files
cameroncookecodex
andcommitted
fix(example): Accept north wind direction from API
Normalize the API value 360 degrees to the domain model's zero-degree north representation before constructing WindDirection. Co-Authored-By: OpenAI Codex <noreply@openai.com>
1 parent 766f0ae commit 273a825

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

example_projects/Weather/Weather/Services/WeatherClientDTOs.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ extension WeatherLocation {
190190

191191
extension CurrentWeather {
192192
init(dto: CurrentWeatherDTO) throws {
193-
guard (0..<360).contains(dto.windDirectionDegrees) else {
193+
guard (0...360).contains(dto.windDirectionDegrees) else {
194194
throw WeatherDTOMappingError.invalidWindDirection(dto.windDirectionDegrees)
195195
}
196196

197+
let windDirectionDegrees = dto.windDirectionDegrees == 360 ? 0 : dto.windDirectionDegrees
198+
197199
self.init(
198200
id: dto.id,
199201
temperatureC: dto.temperatureC,
@@ -209,7 +211,7 @@ extension CurrentWeather {
209211
uvIndex: dto.uvIndex,
210212
uvCategory: UVIndexCategory(dto: dto.uvCategory),
211213
windKph: dto.windKph,
212-
windDirection: WindDirection(degrees: dto.windDirectionDegrees),
214+
windDirection: WindDirection(degrees: windDirectionDegrees),
213215
humidity: dto.humidity,
214216
visibilityKilometers: dto.visibilityKilometers,
215217
pressureMillibars: dto.pressureMillibars,

example_projects/Weather/WeatherTests/WeatherTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,35 @@ struct WeatherTests {
189189
}
190190
}
191191

192+
@Test func windDirection360MapsToNorth() throws {
193+
let dto = CurrentWeatherDTO(
194+
id: "north-wind",
195+
temperatureC: 10,
196+
highC: 12,
197+
lowC: 8,
198+
feelsLikeC: 9,
199+
condition: .sunny,
200+
solarProgress: SolarDayProgressDTO(kind: .daylight, daylightFraction: 0.5),
201+
sunrise: LocalClockTimeDTO(hour: 6, minute: 0),
202+
sunset: LocalClockTimeDTO(hour: 18, minute: 0),
203+
airQualityIndex: 10,
204+
airQualityCategory: .good,
205+
uvIndex: 1,
206+
uvCategory: .low,
207+
windKph: 12,
208+
windDirectionDegrees: 360,
209+
humidity: 50,
210+
visibilityKilometers: 10,
211+
pressureMillibars: 1_013,
212+
pressureTrend: .steady,
213+
precipChance: 0
214+
)
215+
216+
let current = try CurrentWeather(dto: dto)
217+
218+
#expect(current.windDirection == WindDirection(degrees: 0))
219+
}
220+
192221
@Test func weatherFixtureDecodesAsExpectedDTO() throws {
193222
let decoded: WeatherReportDTO = try decodeFixture(named: "weather-report-loc-current-san-francisco")
194223

0 commit comments

Comments
 (0)