Skip to content

Commit

Permalink
Prefer Int
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Mar 19, 2024
1 parent b1c710a commit 4e74243
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
23 changes: 12 additions & 11 deletions Sources/GISTools/GeoJson/Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/GISToolsTests/GeoJson/FeatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4e74243

Please sign in to comment.