Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Updated Weather parameter access
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaximilian committed Feb 10, 2021
1 parent a50f0ff commit f3e9c7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
13 changes: 7 additions & 6 deletions Sources/Weather/Models/Parameter+Name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//

extension Parameter {
public enum Name: String, Decodable {
public enum Name: String, CaseIterable, Decodable {
/// Air pressure
case msl

Expand Down Expand Up @@ -80,16 +80,17 @@ extension Parameter {

/// Weather symbol
case wsymb2 = "Wsymb2"

/// Unknown parameter
case unknown = ""

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

guard let name = try? Name(rawValue: container.decode(String.self)) else {
self = .unknown
return
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: container.codingPath,
debugDescription: "Unknown parameter name!"
)
)
}
self = name
}
Expand Down
28 changes: 10 additions & 18 deletions Sources/Weather/Models/Weather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import Foundation
/// An `Observation` is a collection of `Forecast` instances
public struct Weather: Decodable {
/// A timestamp for when the `Forecast` was approved
private let approvedTime: Date
public let approvedTime: Date

/// A timestamp for when the `Forecast` was created or updated
private let referenceTime: Date
public let referenceTime: Date

/// A `Geometry` represenation for where the `Forecast` is valid
private let geometry: Geometry
public let geometry: Geometry

/// An array of `Forecast` instances that reflect the overall `Forecast` over time
private let timeSeries: [Forecast]
Expand Down Expand Up @@ -70,24 +70,14 @@ extension Array {
}

extension Parameter {
fileprivate static var none: Parameter {
return Parameter(
name: .unknown,
levelType: .unknown,
level: 0,
unit: "n/a",
values: []
)
}

fileprivate var value: Double {
return values.first ?? 0
}
}

extension Forecast {
fileprivate func parameter<T>(byName name: Parameter.Name, transform: (Parameter) -> T) -> T {
return transform(parameters.first(where: \.name, name) ?? .none)
return transform(parameters.first(where: \.name, name).unsafelyUnwrapped)
}
}

Expand All @@ -106,6 +96,10 @@ extension Weather {
}
}

public func get(_ name: Parameter.Name) -> Parameter {
return forecast.parameter(byName: name) { $0 }
}

public func getAll<T>(_ keyPath: KeyPath<Parameter, T>, for name: Parameter.Name) -> [T] {
return forecasts.map {
$0.parameter(byName: name) {
Expand All @@ -114,11 +108,9 @@ extension Weather {
}
}

public func getAll<T1, T2>(label labelKeyPath: KeyPath<Parameter, T1>, value valueKeyPath: KeyPath<Parameter, T2>, for name: Parameter.Name) -> [(label: T1, value: T2)] {
public func getAll(_ name: Parameter.Name) -> [(validTime: Date, parameter: Parameter)] {
return forecasts.map {
$0.parameter(byName: name) {
($0[keyPath: labelKeyPath], $0[keyPath: valueKeyPath])
}
($0.validTime, $0.parameter(byName: name) { $0 })
}
}
}
Expand Down

0 comments on commit f3e9c7d

Please sign in to comment.