Skip to content

Commit 8205368

Browse files
authored
Rename the new helpers to encode/decode feature properties to use model rather than properties to avoid name clashes (#11)
Also add tests for it
1 parent 66d6376 commit 8205368

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

Sources/GeoJSONKit/GeoJSON+Codable.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,26 @@ extension GeoJSON.GeometryObject: Codable {
108108

109109
extension GeoJSON.Feature {
110110

111-
/// Create a feature with the provided `Encodable` as the properties
111+
/// Create a feature with the provided `Encodable` model as the properties
112112
/// - Parameters:
113113
/// - geometry: Geometry
114114
/// - properties: Known structure to use for the properties
115115
/// - id: GeoJSON-compatible ID, i.e., an Integer or a String
116116
/// - configure: Optional handler to configure how to encode the `Encodable`
117-
public init<P: Encodable>(geometry: GeoJSON.GeometryObject, properties: P, id: AnyHashable? = nil, configure: (inout JSONEncoder) -> Void = { _ in }) throws {
117+
public init<P: Encodable>(geometry: GeoJSON.GeometryObject, model: P, id: AnyHashable? = nil, configure: (inout JSONEncoder) -> Void = { _ in }) throws {
118118
var encoder = JSONEncoder()
119119
configure(&encoder)
120-
let data = try encoder.encode(properties)
120+
let data = try encoder.encode(model)
121121
let asDict = try JSONSerialization.jsonObject(with: data) as? [String: AnyHashable]
122122
self.init(geometry: geometry, properties: asDict, id: id)
123123
}
124124

125-
126-
/// Parses the properties as the provided `Decodable`
125+
/// Parses the properties as the provided `Decodable` model
127126
/// - Parameters:
128127
/// - type: Known structure to decode the feature's properties again
129128
/// - configure: Optional handler to configure how to decode the `Decodable`
130129
/// - Returns: Properties decoded as the provided `Decodable`
131-
public func properties<P: Decodable>(as type: P.Type, configure: (inout JSONDecoder) -> Void = { _ in }) throws -> P {
130+
public func model<P: Decodable>(as type: P.Type, configure: (inout JSONDecoder) -> Void = { _ in }) throws -> P {
132131
let asData = try JSONSerialization.data(withJSONObject: properties ?? [:])
133132
var decoder = JSONDecoder()
134133
configure(&decoder)

Tests/GeoJSONKitTests/GeoJSONCodableTest.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,27 @@ final class GeoJSONCodableTest: XCTestCase {
194194
let data = try XCTestCase.loadData(filename: "featurecollection")
195195
XCTAssertThrowsError(try JSONDecoder().decode(GeoJSON.GeometryObject.self, from: data))
196196
}
197-
197+
198+
func testNonTypedInitializerDoesNotThrow() throws {
199+
let point = GeoJSON.GeometryObject.single(.point(.init(latitude: -33.9451, longitude: 151.2581)))
200+
let feature = GeoJSON.Feature(geometry: point, properties: ["key": "value"])
201+
XCTAssertNotNil(feature)
202+
}
203+
204+
func testTypedInitializerCanThrow() throws {
205+
let point = GeoJSON.GeometryObject.single(.point(.init(latitude: -33.9451, longitude: 151.2581)))
206+
let model = Model(name: "Test", number: 3517, date: Date())
207+
let feature = try GeoJSON.Feature(geometry: point, model: model)
208+
XCTAssertNotNil(feature)
209+
210+
let restored = try feature.model(as: Model.self)
211+
XCTAssertEqual(model, restored)
212+
}
213+
214+
}
215+
216+
fileprivate struct Model: Codable, Equatable {
217+
let name: String
218+
let number: Int
219+
let date: Date
198220
}

Tests/GeoJSONKitTests/SerializationTest.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ class SerializationTest: XCTestCase {
3232
let fromDict = try GeoJSON.GeometryObject(dict: asDict)
3333
XCTAssertEqual(geometry, fromDict)
3434
}
35-
3635
}
37-
3836
}

0 commit comments

Comments
 (0)