Skip to content

Commit fdc1971

Browse files
committed
- revert some Decimal related removal to retain compatibility
1 parent a16dbcc commit fdc1971

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Sources/PostgresNIO/Data/PostgresData+Numeric.swift

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NIOCore
2+
import Foundation.Decimal
23

34
public struct PostgresNumeric: CustomStringConvertible, CustomDebugStringConvertible, ExpressibleByStringLiteral {
45
/// The number of digits after this metadata
@@ -36,6 +37,11 @@ public struct PostgresNumeric: CustomStringConvertible, CustomDebugStringConvert
3637
return Double(self.string)
3738
}
3839

40+
41+
public init(decimal: Decimal) {
42+
self.init(decimalString: decimal.description)
43+
}
44+
3945
public init?(string: String) {
4046
// validate string contents are decimal
4147
// TODO: this won't work for all Big decimals
@@ -115,6 +121,13 @@ public struct PostgresNumeric: CustomStringConvertible, CustomDebugStringConvert
115121
self.value = buffer
116122
}
117123

124+
125+
public var decimal: Decimal {
126+
// force cast should always succeed since we know
127+
// string returns a valid decimal
128+
return Decimal(string: self.string)!
129+
}
130+
118131
public var string: String {
119132
guard self.ndigits > 0 else {
120133
return "0"

0 commit comments

Comments
 (0)