Skip to content

Commit

Permalink
update: move custom decoding of geo & osm into their own types
Browse files Browse the repository at this point in the history
  • Loading branch information
devahmedshendy committed Sep 27, 2023
1 parent ee0efbd commit ec1f3c0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,50 @@ extension PodcastLocation {
self.height = height
self.accuracy = accuracy

Check warning on line 18 in Sources/SyndiKit/Formats/Media/Podcast/PodcastLocation+GeoURI.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastLocation+GeoURI.swift#L14-L18

Added lines #L14 - L18 were not covered by tests
}

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

guard
let geoScheme = geoStr.split(separator: ":")[safe: 0],
geoScheme == "geo" else {
throw DecodingError.dataCorrupted(
.init(
codingPath: [PodcastLocation.CodingKeys.geo],
debugDescription: "Invalid prefix for geo attribute: \(geoStr)"
)
)
}
guard let geoPath = geoStr.split(separator: ":")[safe: 1] else {
throw DecodingError.dataCorrupted(
.init(
codingPath: [PodcastLocation.CodingKeys.geo],
debugDescription: "Invalid path for geo attribute: \(geoStr)"
)
)
}
guard
let geoCoords = geoPath.split(separator: ";")[safe: 0],
let latitude = geoCoords.split(separator: ",")[safe: 0]?.asDouble(),
let longitude = geoCoords.split(separator: ",")[safe: 1]?.asDouble()
else {
throw DecodingError.dataCorrupted(
.init(
codingPath: [PodcastLocation.CodingKeys.geo],
debugDescription: "Invalid coordinates for geo attribute: \(geoStr)"
)
)
}
let height = geoCoords.split(separator: ",")[safe: 2]?.asExactInt()
let accuracy = geoPath.split(separator: ";")[safe: 1]?
.split(separator: "=")[safe: 1]?
.asDouble()

self.latitude = latitude
self.longitude = longitude
self.height = height
self.accuracy = accuracy
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,33 @@ extension PodcastLocation {
let id: Int
let type: OsmType
let revision: Int?

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

var osmStr = try container.decode(String.self)

guard let osmType = osmStr.removeFirst().asOsmType() else {
throw DecodingError.dataCorrupted(
.init(
codingPath: [PodcastLocation.CodingKeys.osm],
debugDescription: "Invalid type for osm attribute: \(osmStr)"
)
)
}
guard let osmID = osmStr.split(separator: "#")[safe: 0]?.asExactInt() else {
throw DecodingError.dataCorrupted(
.init(
codingPath: [PodcastLocation.CodingKeys.osm],
debugDescription: "Invalid id of type Int for osm attribute: \(osmStr)"
)
)
}
let osmRevision = osmStr.split(separator: "#")[safe: 1]?.asInt()

self.id = osmID
self.type = osmType
self.revision = osmRevision
}
}
}
58 changes: 2 additions & 56 deletions Sources/SyndiKit/Formats/Media/Podcast/PodcastLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,8 @@ public struct PodcastLocation: Codable, Equatable {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.geo = try container.decode(GeoURI.self, forKey: .geo)
self.osm = try container.decode(OsmQuery.self, forKey: .osm)
self.name = try container.decode(String.self, forKey: .name)


let geoStr = try container.decode(String.self, forKey: .geo)

guard
let geoScheme = geoStr.split(separator: ":")[safe: 0],
geoScheme == "geo" else {
throw DecodingError.dataCorruptedError(
forKey: .geo,
in: container,
debugDescription: "Invalid prefix for geo attribute: \(geoStr)"
)
}
guard let geoPath = geoStr.split(separator: ":")[safe: 1] else {
throw DecodingError.dataCorruptedError(
forKey: .geo,
in: container,
debugDescription: "Invalid path for geo attribute: \(geoStr)"
)
}
guard
let geoCoords = geoPath.split(separator: ";")[safe: 0],
let latitude = geoCoords.split(separator: ",")[safe: 0]?.asDouble(),
let longitude = geoCoords.split(separator: ",")[safe: 1]?.asDouble()
else {
throw DecodingError.dataCorruptedError(
forKey: .geo,
in: container,
debugDescription: "Invalid coordinates for geo attribute: \(geoStr)"
)
}
let height = geoCoords.split(separator: ",")[safe: 2]?.asExactInt()
let accuracy = geoPath.split(separator: ";")[safe: 1]?
.split(separator: "=")[safe: 1]?
.asDouble()
self.geo = .init(latitude: latitude, longitude: longitude, height: height, accuracy: accuracy)


var osmStr = try container.decode(String.self, forKey: .osm)

guard let osmType = osmStr.removeFirst().asOsmType() else {
throw DecodingError.dataCorruptedError(
forKey: .osm,
in: container,
debugDescription: "Invalid type for osm attribute: \(osmStr)"
)
}
guard let osmID = osmStr.split(separator: "#")[safe: 0]?.asExactInt() else {
throw DecodingError.dataCorruptedError(
forKey: .osm,
in: container,
debugDescription: "Invalid id of type Int for osm attribute: \(osmStr)"
)
}
let osmRevision = osmStr.split(separator: "#")[safe: 1]?.asInt()
self.osm = .init(id: osmID, type: osmType, revision: osmRevision)
}
}

0 comments on commit ec1f3c0

Please sign in to comment.