diff --git a/Sources/GISTools/GeoJson/Feature.swift b/Sources/GISTools/GeoJson/Feature.swift index cf71cb3..f94ec61 100644 --- a/Sources/GISTools/GeoJson/Feature.swift +++ b/Sources/GISTools/GeoJson/Feature.swift @@ -19,10 +19,22 @@ public struct Feature: GeoJson { case uint(UIntId) case double(Double) + /// Note: This will prefer `Int` over `UInt` if possible. public init?(value: Any?) { guard let value else { return nil } switch value { + case let binaryInt as (any BinaryInteger): + if let int = IntId(exactly: binaryInt) { + self = .int(int) + } + else if let uint = UIntId(exactly: binaryInt) { + self = .uint(uint) + } + else { + return nil + } + case let int as IntId: self = .int(int) @@ -35,17 +47,6 @@ public struct Feature: GeoJson { case let double as Double: self = .double(double) - case let binaryInt as (any BinaryInteger): - if let int = IntId(exactly: binaryInt) { - self = .int(int) - } - else if let uint = UIntId(exactly: binaryInt) { - self = .uint(uint) - } - else { - return nil - } - default: return nil } diff --git a/Tests/GISToolsTests/GeoJson/FeatureTests.swift b/Tests/GISToolsTests/GeoJson/FeatureTests.swift index 4dacd1d..07aa882 100644 --- a/Tests/GISToolsTests/GeoJson/FeatureTests.swift +++ b/Tests/GISToolsTests/GeoJson/FeatureTests.swift @@ -110,6 +110,9 @@ final class FeatureTests: XCTestCase { XCTAssertEqual(Feature.Identifier(value: Int8(32))?.int64Value, 32) XCTAssertEqual(Feature.Identifier(value: Int8(32))?.uint64Value, 32) + // UInt -> Int + XCTAssertEqual(Feature.Identifier(value: UInt64(32)), .int(32)) + XCTAssertEqual(Feature.Identifier(value: Int8(-32)), .int(-32)) XCTAssertEqual(Feature.Identifier(value: Int8(-32))?.int64Value, -32) XCTAssertNil(Feature.Identifier(value: Int8(-32))?.uint64Value)