-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,27 @@ extension Float: PSQLCodable { | |
.float4 | ||
} | ||
|
||
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Float { | ||
switch type { | ||
case .float4: | ||
var psqlFormat: PSQLFormat { | ||
.binary | ||
} | ||
|
||
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Float { | ||
switch (format, type) { | ||
case (.binary, .float4): | ||
guard buffer.readableBytes == 4, let float = buffer.readFloat() else { | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
return float | ||
case .float8: | ||
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) | ||
Comment on lines
+17
to
21
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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? |
||
case (.text, .float4), (.text, .float8): | ||
guard let string = buffer.readString(length: buffer.readableBytes), let value = Float(string) else { | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
return value | ||
default: | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
|
@@ -30,18 +39,27 @@ extension Double: PSQLCodable { | |
.float8 | ||
} | ||
|
||
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Double { | ||
switch type { | ||
case .float4: | ||
var psqlFormat: PSQLFormat { | ||
.binary | ||
} | ||
|
||
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Double { | ||
switch (format, type) { | ||
case (.binary, .float4): | ||
guard buffer.readableBytes == 4, let float = buffer.readFloat() else { | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
return Double(float) | ||
case .float8: | ||
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 double | ||
case (.text, .float4), (.text, .float8): | ||
guard let string = buffer.readString(length: buffer.readableBytes), let value = Double(string) else { | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
return value | ||
default: | ||
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.