-
-
Notifications
You must be signed in to change notification settings - Fork 82
Use PSQLFormat when encoding and decoding #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use PSQLFormat when encoding and decoding #158
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
5a18b1c
to
8029bcd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I left some questions inline.
Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift
Outdated
Show resolved
Hide resolved
case (.binary, .float8): | ||
guard buffer.readableBytes == 8, let double = buffer.readDouble() else { | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
return Float(double) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we okay with the float-to-double conversion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation I inherited did this, so I guess it's okay for now. Maybe change with the next major release?
self.parameters.forEach { | ||
buffer.writeInteger($0.psqlFormat.rawValue) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should reserve capacity in the buffer to minimise intermediate allocs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although we have to encode the actual parameters as well which make it harder to know how much capacity to reserve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe in another pr at a later point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine by me.
/// In a `RowDescription` returned from the statement variant of `Describe`, | ||
/// the format is not yet known and will always be `.text`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this, does it mean that we assume it to be .text
until we know otherwise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the sentence in question. this is explained in ExtendedQueryStateMachine much better. And is really only relevant there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6df8e99
to
82d57b3
Compare
/// Decode an entity from the `byteBuffer` in postgres binary format | ||
/// | ||
/// - Parameters: | ||
/// - byteBuffer: A `ByteBuffer` to decode. The byteBuffer is sliced in such a way that it is expected | ||
/// that the complete buffer is consumed for decoding | ||
/// - type: The postgres data type. Depending on this type the `byteBuffer`'s bytes need to be interpreted | ||
/// in different ways. | ||
/// - format: The postgres wire format. Can be `.text` or `.binary` | ||
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder` | ||
/// to use when decoding json and metadata to create better erros. | ||
/// - Returns: A decoded object | ||
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@glbrntt Does that make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly! Aren't these are in conflict?
/// Decode an entity from the `byteBuffer` in postgres binary format
/// - format: The postgres wire format. Can be `.text` or `.binary`
/// in different ways. | ||
/// - format: The postgres wire format. Can be `.text` or `.binary` | ||
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder` | ||
/// to use when decoding json and metadata to create better erros. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// to use when decoding json and metadata to create better erros. | |
/// to use when decoding json and metadata to create better errors. |
/// Decode an entity from the `byteBuffer` in postgres binary format | ||
/// | ||
/// - Parameters: | ||
/// - byteBuffer: A `ByteBuffer` to decode. The byteBuffer is sliced in such a way that it is expected | ||
/// that the complete buffer is consumed for decoding | ||
/// - type: The postgres data type. Depending on this type the `byteBuffer`'s bytes need to be interpreted | ||
/// in different ways. | ||
/// - format: The postgres wire format. Can be `.text` or `.binary` | ||
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder` | ||
/// to use when decoding json and metadata to create better erros. | ||
/// - Returns: A decoded object | ||
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly! Aren't these are in conflict?
/// Decode an entity from the `byteBuffer` in postgres binary format
/// - format: The postgres wire format. Can be `.text` or `.binary`
Co-authored-by: George Barnett <[email protected]>
82d57b3
to
21fb161
Compare
Postgres supports two encodings on the wire text (0) and binary (1). Up until now the new
PSQLDecodable
only supported binary encoding.PSQLFormatCode
toPSQLFormat
PSQLFormat
as parameter to the decode function.psqlFormat: PSQLFormat
as protocol requirement forPSQLEncodable
.binary
format only (we can change this with follow up PRs)