Skip to content

Commit ed46fda

Browse files
authored
Fix additional fields on Linux (#3)
* Fix additional fields on Linux * Also test on Linux, clean-up GHA Closes #2
1 parent a6c4fd7 commit ed46fda

5 files changed

Lines changed: 57 additions & 61 deletions

File tree

.github/workflows/swift.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ on:
77
branches: [ main ]
88

99
jobs:
10-
build:
11-
10+
macos:
1211
runs-on: macos-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Build and Test
15+
run: swift test
1316

17+
linux:
18+
runs-on: ubuntu-latest
19+
container:
20+
image: swift:5.4
1421
steps:
1522
- uses: actions/checkout@v2
16-
- name: Build
17-
run: swift build -v
18-
- name: Run tests
19-
run: swift test -v
23+
- name: Build and Test
24+
run: swift test

Sources/GeoJSONKit/GeoJSON.swift

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public struct GeoJSON: Hashable {
286286
}
287287

288288
geometry = try GeometryObject(dict: geometryDict)
289-
properties = dict["properties"] as? [String: AnyHashable]
289+
properties = (dict["properties"] as? [String: Any])?.compactMapValues(Adjuster.hashable(_:))
290290
id = dict["id"] as? AnyHashable
291291
}
292292

@@ -454,7 +454,7 @@ public struct GeoJSON: Hashable {
454454
let knownRootFields = ["type", "features", "bbox", "id", "geometry", "geometries", "properties", "coordinates"]
455455
additionalFields = dict.filter { key, _ in
456456
!knownRootFields.contains(key)
457-
}.compactMapValues { $0 as? AnyHashable}
457+
}.compactMapValues(Adjuster.hashable(_:))
458458
}
459459

460460
public init(geoJSONString string: String) throws {
@@ -503,30 +503,42 @@ fileprivate extension Array where Element == GeoJSON.Degrees {
503503
}
504504
}
505505

506-
fileprivate extension Dictionary {
507-
var prune: [String: Any] {
508-
if let compatible = pruneWorker as? [String: Any] {
509-
return compatible
506+
fileprivate enum Adjuster {
507+
static func prune(_ value: Any) -> Any {
508+
if let dict = value as? [String: Any] {
509+
return dict.mapValues(prune(_:))
510+
} else if value is Int || value is [Int] || value is [[Int]] || value is Bool || value is [Bool] || value is [[Bool]] {
511+
return value
512+
} else if let doubles = value as? [Double] {
513+
return doubles.prune
514+
} else if let doubless = value as? [[Double]] {
515+
return doubless.map(\.prune)
516+
} else if let double = value as? Double {
517+
return Decimal(double)
518+
} else if let array = value as? [Any] {
519+
return array.map(prune(_:))
510520
} else {
511-
preconditionFailure()
521+
return value
512522
}
513523
}
514524

515-
private var pruneWorker: [Key: Any] {
516-
return mapValues { value in
517-
if let dict = value as? [String: Any] {
518-
return dict.prune
519-
} else if value is Int || value is [Int] || value is [[Int]] || value is Bool || value is [Bool] || value is [[Bool]] {
520-
return value
521-
} else if let doubles = value as? [Double] {
522-
return doubles.prune
523-
} else if let doubless = value as? [[Double]] {
524-
return doubless.map { $0.prune }
525-
} else if let double = value as? Double {
526-
return Decimal(double)
527-
} else {
528-
return value
529-
}
525+
static func hashable(_ value: Any) -> AnyHashable? {
526+
if let dict = value as? [String: Any] {
527+
return dict.mapValues(hashable(_:))
528+
} else if let array = value as? [Any] {
529+
return array.map(hashable(_:))
530+
} else if let hashable = value as? AnyHashable {
531+
return hashable
532+
} else {
533+
assertionFailure("Unexpected non-hashable: \(value) -- \(type(of: value))")
534+
return nil
530535
}
531536
}
532537
}
538+
539+
540+
fileprivate extension Dictionary {
541+
var prune: [Key: Any] {
542+
mapValues(Adjuster.prune(_:))
543+
}
544+
}

Tests/GeoJSONKitTests/GeoJSONParserTest.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,17 @@ final class GeoJSONParserTest: XCTestCase {
221221
XCTAssertNotNil(parsed)
222222
}
223223

224-
static var allTests = [
225-
("testPoint", testPoint),
226-
("testMultiPoint", testMultiPoint),
227-
("testLineString", testLineString),
228-
("testMultiLineString", testMultiLineString),
229-
("testPolygon", testPolygon),
230-
("testPolygonWithHole", testPolygonWithHole),
231-
("testMultiPolygon", testMultiPolygon),
232-
("testFeatureCollection", testFeatureCollection),
233-
("testGeometryCollection", testGeometryCollection),
234-
("testFeatureCollectionFromDict", testFeatureCollectionFromDict),
235-
("testTripGo", testTripGo),
236-
("testWorld", testWorld),
237-
("testNuremberg", testNuremberg),
238-
("testNSWFires", testNSWFires),
239-
]
224+
func testAdditionalFields() throws {
225+
let data = try XCTestCase.loadData(filename: "nuremberg")
226+
let parsed = try GeoJSON(data: data)
227+
XCTAssertNotNil(parsed)
228+
let geocoding = try XCTUnwrap(parsed.additionalFields["geocoding"])
229+
let geocodingDict = try XCTUnwrap(geocoding as? [String: Any])
230+
XCTAssertEqual(geocodingDict["version"] as? String, "0.2")
231+
XCTAssertEqual(geocodingDict["attribution"] as? String, "http://pelias.mapzen.com/v1/attribution")
232+
XCTAssertNotNil(geocodingDict["query"])
233+
XCTAssertNotNil(geocodingDict["engine"])
234+
XCTAssertEqual(geocodingDict["timestamp"] as? Int, 1505232719417)
235+
}
240236

241237
}

Tests/GeoJSONKitTests/XCTestManifests.swift

Lines changed: 0 additions & 10 deletions
This file was deleted.

Tests/LinuxMain.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)