66// Copyright © 2017-2026 ZeeZide GmbH. All rights reserved.
77//
88
9+ /**
10+ * A helper protocol that is used to convert ``AdaptorRecord`` columns into
11+ * Swift typed values.
12+ */
913public protocol AdaptorQueryColumnRepresentable {
1014
1115 static func fromAdaptorQueryValue ( _ value : Any ? ) throws -> Self
12-
1316}
1417
1518public enum AdaptorQueryTypeError : Swift . Error {
@@ -21,8 +24,7 @@ extension String : AdaptorQueryColumnRepresentable {
2124
2225 @ inlinable
2326 public static func fromAdaptorQueryValue ( _ value : Any ? ) throws -> String {
24- guard let value = value
25- else {
27+ guard let value = value else {
2628 throw AdaptorQueryTypeError . nullInNonOptionalType ( String . self )
2729 }
2830
@@ -31,24 +33,80 @@ extension String : AdaptorQueryColumnRepresentable {
3133 }
3234
3335}
34- extension Int : AdaptorQueryColumnRepresentable {
36+
37+ extension Int : AdaptorQueryColumnRepresentable { }
38+ extension Int16 : AdaptorQueryColumnRepresentable { }
39+ extension Int32 : AdaptorQueryColumnRepresentable { }
40+ extension Int64 : AdaptorQueryColumnRepresentable { }
41+ @ available ( macOS 15 , iOS 13 , * )
42+ extension Int128 : AdaptorQueryColumnRepresentable { }
43+ extension UInt : AdaptorQueryColumnRepresentable { }
44+ extension UInt16 : AdaptorQueryColumnRepresentable { }
45+ extension UInt32 : AdaptorQueryColumnRepresentable { }
46+ extension UInt64 : AdaptorQueryColumnRepresentable { }
47+ @ available ( macOS 15 , iOS 13 , * )
48+ extension UInt128 : AdaptorQueryColumnRepresentable { }
49+
50+ extension BinaryInteger {
3551
3652 @ inlinable
37- public static func fromAdaptorQueryValue ( _ value : Any ? ) throws -> Int {
38- guard let value = value
39- else {
53+ public static func fromAdaptorQueryValue ( _ value : Any ? ) throws -> Self {
54+ guard let value = value else {
55+ throw AdaptorQueryTypeError . nullInNonOptionalType ( Int . self )
56+ }
57+
58+ if let value = value as? Self { return value }
59+
60+ if #available( macOS 15 , iOS 13 , * ) {
61+ if let value = value as? any BinaryInteger { return Self ( value ) }
62+ }
63+
64+ switch value {
65+ case let typedValue as Int : return Self ( typedValue )
66+ case let typedValue as Int64 : return Self ( typedValue )
67+ case let typedValue as Int32 : return Self ( typedValue )
68+ case let typedValue as String :
69+ guard let i = Int ( typedValue ) else {
70+ throw AdaptorQueryTypeError . cannotConvertValue ( Int . self , value )
71+ }
72+ return Self ( i )
73+ default :
74+ // ERROR: VALUE: 9999 Int32
75+ globalZeeQLLogger . error ( " VALUE: \( value ) \( type ( of : value ) ) " )
76+ throw AdaptorQueryTypeError . cannotConvertValue ( Int . self , value )
77+ }
78+ }
79+ }
80+
81+ extension Float : AdaptorQueryColumnRepresentable { }
82+ extension Double : AdaptorQueryColumnRepresentable { }
83+
84+ extension BinaryFloatingPoint {
85+
86+ @ inlinable
87+ public static func fromAdaptorQueryValue ( _ value : Any ? ) throws -> Self {
88+ guard let value = value else {
4089 throw AdaptorQueryTypeError . nullInNonOptionalType ( Int . self )
4190 }
91+
92+ if let value = value as? Self { return value }
93+
94+ if #available( macOS 15 , iOS 13 , * ) {
95+ if let value = value as? any BinaryFloatingPoint { return Self ( value ) }
96+ if let value = value as? any BinaryInteger { return Self ( value ) }
97+ }
98+
4299 switch value {
43- case let typedValue as Int : return typedValue
44- case let typedValue as Int64 : return Int ( typedValue )
45- case let typedValue as Int32 : return Int ( typedValue )
100+ case let typedValue as Double : return Self ( typedValue )
101+ case let typedValue as Float : return Self ( typedValue )
102+ case let typedValue as Int : return Self ( typedValue )
103+ case let typedValue as Int64 : return Self ( typedValue )
104+ case let typedValue as Int32 : return Self ( typedValue )
46105 case let typedValue as String :
47- guard let i = Int ( typedValue )
48- else {
106+ guard let i = Double ( typedValue ) else {
49107 throw AdaptorQueryTypeError . cannotConvertValue ( Int . self , value )
50108 }
51- return i
109+ return Self ( i )
52110 default :
53111 // ERROR: VALUE: 9999 Int32
54112 globalZeeQLLogger . error ( " VALUE: \( value ) \( type ( of : value ) ) " )
0 commit comments