Skip to content

Commit

Permalink
A few Swift 6 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Jun 12, 2024
1 parent 39992f7 commit 9566dc8
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 33 deletions.
19 changes: 11 additions & 8 deletions Sources/GISTools/GeoJson/Feature.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Foundation

/// A GeoJSON `Feature`.
public struct Feature: GeoJson, Identifiable {
public struct Feature:
GeoJson,
Identifiable
{

/// A GeoJSON identifier that can either be a string or number.
///
Expand Down Expand Up @@ -104,7 +107,7 @@ public struct Feature: GeoJson, Identifiable {
public init(
_ geometry: GeoJsonGeometry,
id: Identifier? = nil,
properties: [String: Any] = [:],
properties: [String: Sendable] = [:],
calculateBoundingBox: Bool = false)
{
self.geometry = geometry
Expand All @@ -121,14 +124,14 @@ public struct Feature: GeoJson, Identifiable {
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
Feature.isValid(geoJson: geoJson),
let geometry: GeoJsonGeometry = Feature.tryCreateGeometry(json: geoJson["geometry"])
else { return nil }

self.geometry = geometry
self.id = Identifier(value: geoJson["id"])
self.properties = (geoJson["properties"] as? [String: Any]) ?? [:]
self.properties = (geoJson["properties"] as? [String: Sendable]) ?? [:]
self.boundingBox = Feature.tryCreate(json: geoJson["bbox"])

if calculateBoundingBox, self.boundingBox == nil {
Expand Down Expand Up @@ -223,30 +226,30 @@ extension Feature {
extension Feature {

/// Returns a property by key.
public func property<T>(for key: String) -> T? {
public func property<T: Sendable>(for key: String) -> T? {
return properties[key] as? T
}

/// Set a property key/value pair.
///
/// - important: `value` must be a valid JSON object or serialization will fail.
public mutating func setProperty(_ value: Any?, for key: String) {
public mutating func setProperty(_ value: Sendable?, for key: String) {
var updatedProperties = properties
updatedProperties[key] = value
properties = updatedProperties
}

/// Remove a property from the Feature.
@discardableResult
public mutating func removeProperty(for key: String) -> Any? {
public mutating func removeProperty(for key: String) -> Sendable? {
var updatedProperties = properties
let previous = updatedProperties.removeValue(forKey: key)
properties = updatedProperties
return previous
}

/// Returns a property by key.
public subscript<T>(key: String) -> T? {
public subscript<T: Sendable>(key: String) -> T? {
get {
return property(for: key)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/FeatureCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public struct FeatureCollection:
}

// To prevent an infinite recursion.
init?(geoJson: [String: Any], calculateBoundingBox: Bool = false) {
init?(geoJson: [String: Sendable], calculateBoundingBox: Bool = false) {
guard FeatureCollection.isValid(geoJson: geoJson),
let features: [Feature] = FeatureCollection.tryCreate(json: geoJson["features"])
else { return nil }
Expand Down
12 changes: 6 additions & 6 deletions Sources/GISTools/GeoJson/GeoJson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,30 @@ extension GeoJson {
extension GeoJson {

/// Any foreign member by key.
public func foreignMember<T>(for key: String) -> T? {
public func foreignMember<T: Sendable>(for key: String) -> T? {
return foreignMembers[key] as? T
}

/// Set a foreign member key/value pair.
///
/// - important: `value` must be a valid JSON object or serialization will fail.
public mutating func setForeignMember(_ value: Any?, for key: String) {
public mutating func setForeignMember(_ value: Sendable?, for key: String) {
var updatedProperties = foreignMembers
updatedProperties[key] = value
foreignMembers = updatedProperties
}

/// Remove a foreign member from the receiver.
@discardableResult
public mutating func removeForeignMember(for key: String) -> Any? {
public mutating func removeForeignMember(for key: String) -> Sendable? {
var updatedProperties = foreignMembers
let previous = updatedProperties.removeValue(forKey: key)
foreignMembers = updatedProperties
return previous
}

/// Any foreign member by subscript.
public subscript<T>(foreignMember key: String) -> T? {
public subscript<T: Sendable>(foreignMember key: String) -> T? {
get {
return foreignMember(for: key)
}
Expand Down Expand Up @@ -228,7 +228,7 @@ extension GeoJson {

/// Try to create a GeoJSON object from any JSON object.
public static func tryCreate(json: Any?) -> GeoJson? {
if let geoJson = json as? [String: Any],
if let geoJson = json as? [String: Sendable],
let typeString = geoJson["type"] as? String,
let type = GeoJsonType(rawValue: typeString),
type != .invalid
Expand All @@ -251,7 +251,7 @@ extension GeoJson {

/// Try to create a GeoJSON geometry from any JSON object.
public static func tryCreateGeometry(json: Any?) -> GeoJsonGeometry? {
if let geoJson = json as? [String: Any],
if let geoJson = json as? [String: Sendable],
let typeString = geoJson["type"] as? String,
let type = GeoJsonType(rawValue: typeString),
type != .invalid
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/GeometryCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct GeometryCollection: GeoJsonGeometry {
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
GeometryCollection.isValid(geoJson: geoJson),
let geometries: [GeoJsonGeometry] = GeometryCollection.tryCreate(json: geoJson["geometries"])
else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/LineString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public struct LineString:
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
LineString.isValid(geoJson: geoJson),
let coordinates: [Coordinate3D] = LineString.tryCreate(json: geoJson["coordinates"])
else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/MultiLineString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public struct MultiLineString:
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
MultiLineString.isValid(geoJson: geoJson),
let coordinates: [[Coordinate3D]] = MultiLineString.tryCreate(json: geoJson["coordinates"])
else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/MultiPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public struct MultiPoint:
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
MultiPoint.isValid(geoJson: geoJson),
let coordinates: [Coordinate3D] = MultiPoint.tryCreate(json: geoJson["coordinates"])
else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/MultiPolygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public struct MultiPolygon:
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
MultiPolygon.isValid(geoJson: geoJson),
let coordinates: [[[Coordinate3D]]] = MultiPolygon.tryCreate(json: geoJson["coordinates"])
else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/Point.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct Point: PointGeometry {
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
Point.isValid(geoJson: geoJson),
let coordinate: Coordinate3D = Point.tryCreate(json: geoJson["coordinates"])
else { return nil }
Expand Down
2 changes: 1 addition & 1 deletion Sources/GISTools/GeoJson/Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public struct Polygon:
}

public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Any],
guard let geoJson = json as? [String: Sendable],
Polygon.isValid(geoJson: geoJson),
let coordinates: [[Coordinate3D]] = Polygon.tryCreate(json: geoJson["coordinates"])
else { return nil }
Expand Down
8 changes: 4 additions & 4 deletions Sources/GISTools/GeoJson/WKBCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension Feature {
sourceSrid: Int?,
targetProjection: Projection = .epsg4326,
id: Identifier? = nil,
properties: [String: Any] = [:],
properties: [String: Sendable] = [:],
calculateBoundingBox: Bool = false)
{
guard let geometry = try? WKBCoder.decode(wkb: wkb, sourceSrid: sourceSrid, targetProjection: targetProjection) else { return nil }
Expand All @@ -88,7 +88,7 @@ extension Feature {
sourceProjection: Projection,
targetProjection: Projection = .epsg4326,
id: Identifier? = nil,
properties: [String: Any] = [:],
properties: [String: Sendable] = [:],
calculateBoundingBox: Bool = false)
{
guard let geometry = try? WKBCoder.decode(wkb: wkb, sourceProjection: sourceProjection, targetProjection: targetProjection) else { return nil }
Expand Down Expand Up @@ -172,7 +172,7 @@ extension Data {
sourceSrid: Int?,
targetProjection: Projection = .epsg4326,
id: Feature.Identifier? = nil,
properties: [String: Any] = [:])
properties: [String: Sendable] = [:])
-> Feature?
{
Feature(wkb: self, sourceSrid: sourceSrid, targetProjection: targetProjection, id: id, properties: properties)
Expand All @@ -185,7 +185,7 @@ extension Data {
sourceProjection: Projection,
targetProjection: Projection = .epsg4326,
id: Feature.Identifier? = nil,
properties: [String: Any] = [:])
properties: [String: Sendable] = [:])
-> Feature?
{
Feature(wkb: self, sourceProjection: sourceProjection, targetProjection: targetProjection, id: id, properties: properties)
Expand Down
8 changes: 4 additions & 4 deletions Sources/GISTools/GeoJson/WKTCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension Feature {
sourceSrid: Int?,
targetProjection: Projection = .epsg4326,
id: Identifier? = nil,
properties: [String: Any] = [:],
properties: [String: Sendable] = [:],
calculateBoundingBox: Bool = false)
{
guard let geometry = try? WKTCoder.decode(wkt: wkt, sourceSrid: sourceSrid, targetProjection: targetProjection) else { return nil }
Expand All @@ -88,7 +88,7 @@ extension Feature {
sourceProjection: Projection,
targetProjection: Projection = .epsg4326,
id: Identifier? = nil,
properties: [String: Any] = [:],
properties: [String: Sendable] = [:],
calculateBoundingBox: Bool = false)
{
guard let geometry = try? WKTCoder.decode(wkt: wkt, sourceProjection: sourceProjection, targetProjection: targetProjection) else { return nil }
Expand Down Expand Up @@ -182,7 +182,7 @@ extension String {
sourceSrid: Int?,
targetProjection: Projection = .epsg4326,
id: Feature.Identifier? = nil,
properties: [String: Any] = [:])
properties: [String: Sendable] = [:])
-> Feature?
{
Feature(wkt: self, sourceSrid: sourceSrid, targetProjection: targetProjection, id: id, properties: properties)
Expand All @@ -195,7 +195,7 @@ extension String {
sourceProjection: Projection,
targetProjection: Projection = .epsg4326,
id: Feature.Identifier? = nil,
properties: [String: Any] = [:])
properties: [String: Sendable] = [:])
-> Feature?
{
Feature(wkt: self, sourceProjection: sourceProjection, targetProjection: targetProjection, id: id, properties: properties)
Expand Down
6 changes: 3 additions & 3 deletions Tests/GISToolsTests/Helpers/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TestData {
// MARK: -

class func stringFromFile(package: String, name: String) -> String {
let path = URL(fileURLWithPath: #file)
let path = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("TestData")
Expand All @@ -65,13 +65,13 @@ class TestData {
}

class func dataFromFile(package: String, name: String) -> Data {
let path = URL(fileURLWithPath: #file)
let path = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("TestData")
.appendingPathComponent(package)
.appendingPathComponent(name)
.appendingPathExtension("geojson")
.appendingPathExtension("geojson")

do {
if !(try path.checkResourceIsReachable()) {
Expand Down

0 comments on commit 9566dc8

Please sign in to comment.