Skip to content

Commit

Permalink
Adds
Browse files Browse the repository at this point in the history
- `TKAPI.Location` is now Hashable
- `TKNamedCoordinate(_:TKAPI.Location)`
- `TKServer.hit<Input: Encodable>(...)`, without expected output
  • Loading branch information
nighthawk committed Jul 25, 2024
1 parent 14d484d commit d8acce0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Sources/TripKit/model/API/BaseAPIModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,9 @@ extension TKAPI.Location {
}

}

extension TKNamedCoordinate {
public convenience init(_ remote: TKAPI.Location) {
self.init(latitude: remote.lat, longitude: remote.lng, name: remote.name, address: remote.address)
}
}
26 changes: 26 additions & 0 deletions Sources/TripKit/server/TKServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,32 @@ extension TKServer {
}
}

public func hit<Input: Encodable>(
_ method: HTTPMethod = .POST,
path: String,
input: Input,
headers: [String: String]? = nil,
region: TKRegion? = nil,
encoderConfig: (JSONEncoder) -> Void = { _ in }
) async throws -> Response<Data?> {
let encoder = JSONEncoder()
encoderConfig(encoder)
let parameters = try encoder.encodeJSONObject(input) as? [String: Any]

return await withCheckedContinuation { continuation in
hitSkedGo(
method: method,
path: path,
parameters: parameters,
headers: headers,
region: region,
callbackOnMain: false
) { response in
continuation.resume(returning: response)
}
}
}

public func hit<Model: Decodable>(
_ type: Model.Type,
_ method: HTTPMethod = .GET,
Expand Down
15 changes: 13 additions & 2 deletions Sources/TripKit/server/TKWaypointRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ extension TKWaypointRouter {
var leaveAt: Date?
}

enum Location: Equatable {
enum Location: Hashable {
static func == (lhs: TKWaypointRouter.Location, rhs: TKWaypointRouter.Location) -> Bool {
switch (lhs, rhs) {
case let (.coordinate(left), .coordinate(right)):
Expand All @@ -369,11 +369,22 @@ extension TKWaypointRouter {
}
}

func hash(into hasher: inout Hasher) {
switch self {
case .coordinate(let coordinate):
hasher.combine(coordinate.latitude)
hasher.combine(coordinate.longitude)
case let .code(code, region):
hasher.combine(code)
hasher.combine(region)
}
}

case coordinate(CLLocationCoordinate2D)
case code(String, TKRegion)
}

public struct Segment: Equatable {
public struct Segment: Hashable {
var start: Location
var end: Location
let modes: [String]
Expand Down

0 comments on commit d8acce0

Please sign in to comment.