Skip to content

Commit 4eea226

Browse files
authored
Merge pull request #16 from swhitty/windows
Fix Windows Builds
2 parents eb07770 + 9cbe3b6 commit 4eea226

File tree

3 files changed

+98
-17
lines changed

3 files changed

+98
-17
lines changed

.github/workflows/build.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,20 @@ jobs:
125125
run: swift build --build-tests
126126
- name: Test
127127
run: swift test --skip-build
128+
129+
windows_swift_6_1:
130+
runs-on: windows-latest
131+
steps:
132+
- name: Checkout
133+
uses: actions/checkout@v4
134+
- name: Install Swift
135+
uses: SwiftyLab/setup-swift@latest
136+
with:
137+
swift-version: "6.1.2"
138+
visual-studio-components: Microsoft.VisualStudio.Component.Windows11SDK.22621
139+
- name: Version
140+
run: swift --version
141+
- name: Build
142+
run: swift build --build-tests
143+
- name: Test
144+
run: swift test --skip-build

Sources/KeyValueDecoder.swift

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
// SOFTWARE.
3030
//
3131

32-
import CoreFoundation
3332
import Foundation
3433

3534
/// Top level encoder that converts `[String: Any]`, `[Any]` or `Any` into `Codable` types.
@@ -779,22 +778,44 @@ extension NSNumber {
779778
}
780779
}
781780

782-
func getNumberTypeID() -> CFNumberType? {
783-
guard CFGetTypeID(self as CFTypeRef) == CFNumberGetTypeID() else { return nil }
784-
#if canImport(Darwin)
785-
return CFNumberGetType(self as CFNumber)
786-
#else
787-
guard type(of: self) != type(of: NSNumber(true)) else { return nil }
781+
enum NumberTypeID: Int, Sendable {
782+
case sInt8Type = 1
783+
case sInt16Type = 2
784+
case sInt32Type = 3
785+
case sInt64Type = 4
786+
case float32Type = 5
787+
case float64Type = 6
788+
case charType = 7
789+
case shortType = 8
790+
case intType = 9
791+
case longType = 10
792+
case longLongType = 11
793+
case floatType = 12
794+
case doubleType = 13
795+
case cfIndexType = 14
796+
case nsIntegerType = 15
797+
case cgFloatType = 16
798+
}
799+
800+
func getNumberTypeID() -> NumberTypeID? {
801+
// Prevent misclassifying Bool as charType
802+
if type(of: self) == type(of: NSNumber(value: true)) { return nil }
803+
788804
switch String(cString: objCType) {
789-
case "c": return CFNumberType.charType
790-
case "s": return CFNumberType.shortType
791-
case "i": return CFNumberType.intType
792-
case "q": return CFNumberType.longLongType
793-
case "d": return CFNumberType.doubleType
794-
case "f": return CFNumberType.floatType
795-
case "Q": return CFNumberType.longLongType
796-
default: return nil
805+
case "c": return .charType
806+
case "C": return .sInt8Type
807+
case "s": return .shortType
808+
case "S": return .sInt16Type
809+
case "i": return .intType
810+
case "I": return .sInt32Type
811+
case "l": return .longType
812+
case "L": return .sInt32Type
813+
case "q": return .longLongType
814+
case "Q": return .sInt64Type
815+
case "f": return .floatType
816+
case "d": return .doubleType
817+
case "B": return nil
818+
default: return nil
797819
}
798-
#endif
799820
}
800821
}

Tests/KeyValueDecoderTests.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,47 @@ struct KeyValueDecoderTests {
892892
#expect((true as NSNumber).getDoubleValue() == nil)
893893
}
894894

895-
@Test
895+
#if compiler(>=6.1)
896+
@Test()
897+
func decodingErrors() throws {
898+
899+
var error = #expect(throws: DecodingError.self) {
900+
try KeyValueDecoder.decode(Seafood.self, from: 10)
901+
}
902+
#expect(error?.debugDescription == "Expected String at SELF, found Int")
903+
904+
error = #expect(throws: DecodingError.self) {
905+
try KeyValueDecoder.decode(Seafood.self, from: 10)
906+
}
907+
#expect(error?.debugDescription == "Expected String at SELF, found Int")
908+
909+
error = #expect(throws: DecodingError.self) {
910+
try KeyValueDecoder.decode(Int.self, from: NSNull())
911+
}
912+
#expect(error?.debugDescription == "Expected BinaryInteger at SELF, found NSNull")
913+
914+
error = #expect(throws: DecodingError.self) {
915+
try KeyValueDecoder.decode(Int.self, from: Optional<Int>.none)
916+
}
917+
#expect(error?.debugDescription == "Expected BinaryInteger at SELF, found nil")
918+
919+
error = #expect(throws: DecodingError.self) {
920+
try KeyValueDecoder.decode([Int].self, from: [0, 1, true] as [Any])
921+
}
922+
#expect(error?.debugDescription == "Expected BinaryInteger at SELF[2], found Bool")
923+
924+
error = #expect(throws: DecodingError.self) {
925+
try KeyValueDecoder.decode(AllTypes.self, from: ["tArray": [["tString": 0]]] as [String: Any])
926+
}
927+
#expect(error?.debugDescription == "Expected String at SELF.tArray[0].tString, found Int")
928+
929+
error = #expect(throws: DecodingError.self) {
930+
try KeyValueDecoder.decode(AllTypes.self, from: ["tArray": [["tString": 0]]] as [String: Any])
931+
}
932+
#expect(error?.debugDescription == "Expected String at SELF.tArray[0].tString, found Int")
933+
}
934+
#else
935+
@Test()
896936
func decodingErrors() throws {
897937
expectDecodingError(try KeyValueDecoder.decode(Seafood.self, from: 10)) { error in
898938
#expect(error.debugDescription == "Expected String at SELF, found Int")
@@ -913,6 +953,7 @@ struct KeyValueDecoderTests {
913953
#expect(error.debugDescription == "Expected String at SELF.tArray[0].tString, found Int")
914954
}
915955
}
956+
#endif
916957

917958
@Test
918959
func int_ClampsDoubles() {
@@ -997,6 +1038,7 @@ struct KeyValueDecoderTests {
9971038
#endif
9981039
}
9991040

1041+
#if compiler(<6.1)
10001042
func expectDecodingError<T>(_ expression: @autoclosure () throws -> T,
10011043
file: String = #filePath,
10021044
line: Int = #line,
@@ -1012,6 +1054,7 @@ func expectDecodingError<T>(_ expression: @autoclosure () throws -> T,
10121054
Issue.record(error, "Expected DecodingError", sourceLocation: location)
10131055
}
10141056
}
1057+
#endif
10151058

10161059
extension DecodingError {
10171060

0 commit comments

Comments
 (0)