-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add oracle’s number as a datatype
- Loading branch information
1 parent
8cdde67
commit 167ed3b
Showing
15 changed files
with
453 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import NIOCore | ||
import struct Foundation.Decimal | ||
|
||
public struct OracleNumber: | ||
CustomStringConvertible, CustomDebugStringConvertible, | ||
ExpressibleByStringLiteral, ExpressibleByIntegerLiteral, | ||
ExpressibleByFloatLiteral, Equatable, Hashable, Sendable | ||
{ | ||
internal var value: ByteBuffer | ||
|
||
public var double: Double? { | ||
var value = self.value.getSlice( | ||
at: 1, length: self.value.readableBytes - 1 | ||
)! // skip length | ||
return try? OracleNumeric.parseFloat(from: &value) | ||
} | ||
|
||
public var description: String { | ||
if let double = self.double { | ||
return "\(double)" | ||
} | ||
return "<invalid_number>" | ||
} | ||
|
||
public var debugDescription: String { | ||
String(describing: value) | ||
} | ||
|
||
public init<T: Numeric>(_ value: T) where T: LosslessStringConvertible { | ||
self.init(ascii: value.ascii) | ||
} | ||
|
||
public init(stringLiteral value: String) { | ||
self.init(ascii: value.ascii) | ||
} | ||
|
||
public init(integerLiteral value: Int) { | ||
self.init(ascii: value.ascii) | ||
} | ||
|
||
public init(floatLiteral value: Double) { | ||
self.init(ascii: value.ascii) | ||
} | ||
|
||
public init(decimal: Decimal) { | ||
self.init(ascii: decimal.description.ascii) | ||
} | ||
|
||
|
||
internal init(value: ByteBuffer) { | ||
self.value = value | ||
} | ||
|
||
internal init(ascii: [UInt8]) { | ||
var buffer = ByteBuffer() | ||
OracleNumeric.encodeNumeric(ascii, into: &buffer) | ||
self.init(value: buffer) | ||
} | ||
|
||
public static func == (lhs: Self, rhs: Self) -> Bool { | ||
lhs.double == rhs.double | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(self.double) | ||
} | ||
} | ||
|
||
extension OracleNumber: OracleDecodable { | ||
public init<JSONDecoder: OracleJSONDecoder>( | ||
from buffer: inout ByteBuffer, | ||
type: OracleDataType, | ||
context: OracleDecodingContext<JSONDecoder> | ||
) throws { | ||
var bufferWithLength = ByteBuffer(bytes: [UInt8(buffer.readableBytes)]) | ||
bufferWithLength.writeBuffer(&buffer) | ||
self.value = bufferWithLength | ||
} | ||
} | ||
|
||
extension OracleNumber: OracleEncodable { | ||
public var oracleType: DBType { .number } | ||
|
||
public func encode<JSONEncoder: OracleJSONEncoder>( | ||
into buffer: inout ByteBuffer, | ||
context: OracleEncodingContext<JSONEncoder> | ||
) { | ||
buffer.writeImmutableBuffer(self.value) | ||
} | ||
} |
Oops, something went wrong.