Skip to content

Commit

Permalink
ByteBuffer extension, prevent naming conflicts (#204)
Browse files Browse the repository at this point in the history
### Motivation

Because of https://bugs.swift.org/browse/SR-15517, we might run into naming conflicts with SwiftNIO, once apple/swift-nio#1990 lands.

### Changes

- Prefix all ByteBuffer utility methods

### Result

Chances of breaking code reduced.
  • Loading branch information
fabianfett authored Nov 23, 2021
1 parent 87cfca5 commit 3931a06
Show file tree
Hide file tree
Showing 52 changed files with 133 additions and 133 deletions.
6 changes: 3 additions & 3 deletions Sources/PostgresNIO/Data/PostgresData+Double.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIOCore
extension PostgresData {
public init(double: Double) {
var buffer = ByteBufferAllocator().buffer(capacity: 0)
buffer.writeDouble(double)
buffer.psqlWriteDouble(double)
self.init(type: .float8, formatCode: .binary, value: buffer)
}

Expand All @@ -16,10 +16,10 @@ extension PostgresData {
case .binary:
switch self.type {
case .float4:
return value.readFloat()
return value.psqlReadFloat()
.flatMap { Double($0) }
case .float8:
return value.readDouble()
return value.psqlReadDouble()
case .numeric:
return self.numeric?.double
default:
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgresNIO/Data/PostgresData+Float.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ extension PostgresData {
case .binary:
switch self.type {
case .float4:
return value.readFloat()
return value.psqlReadFloat()
case .float8:
return value.readDouble()
return value.psqlReadDouble()
.flatMap { Float($0) }
default:
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension PostgresMessage {
case 10:
var mechanisms: [String] = []
while buffer.readableBytes > 0 {
guard let nextString = buffer.readNullTerminatedString() else {
guard let nextString = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not parse SASL mechanisms from authentication message")
}
if nextString.isEmpty {
Expand Down Expand Up @@ -68,7 +68,7 @@ extension PostgresMessage {
case .saslMechanisms(let mechanisms):
buffer.writeInteger(10, as: Int32.self)
mechanisms.forEach {
buffer.writeNullTerminatedString($0)
buffer.psqlWriteNullTerminatedString($0)
}
case .saslContinue(let challenge):
buffer.writeInteger(11, as: Int32.self)
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgresNIO/Message/PostgresMessage+Bind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ extension PostgresMessage {

/// Serializes this message into a byte buffer.
public func serialize(into buffer: inout ByteBuffer) {
buffer.writeNullTerminatedString(self.portalName)
buffer.writeNullTerminatedString(self.statementName)
buffer.psqlWriteNullTerminatedString(self.portalName)
buffer.psqlWriteNullTerminatedString(self.statementName)

buffer.write(array: self.parameterFormatCodes)
buffer.write(array: self.parameters) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Message/PostgresMessage+Close.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension PostgresMessage {
/// Serializes this message into a byte buffer.
public func serialize(into buffer: inout ByteBuffer) throws {
buffer.writeInteger(target.rawValue)
buffer.writeNullTerminatedString(name)
buffer.psqlWriteNullTerminatedString(name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension PostgresMessage {
public struct CommandComplete: PostgresMessageType {
/// Parses an instance of this message type from a byte buffer.
public static func parse(from buffer: inout ByteBuffer) throws -> CommandComplete {
guard let string = buffer.readNullTerminatedString() else {
guard let string = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not parse close response message")
}
return .init(tag: string)
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Message/PostgresMessage+Describe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension PostgresMessage {
/// Serializes this message into a byte buffer.
public func serialize(into buffer: inout ByteBuffer) {
buffer.writeInteger(command.rawValue)
buffer.writeNullTerminatedString(name)
buffer.psqlWriteNullTerminatedString(name)
}
}
}
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Message/PostgresMessage+Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension PostgresMessage {
public static func parse(from buffer: inout ByteBuffer) throws -> Error {
var fields: [Field: String] = [:]
while let field = buffer.readInteger(as: Field.self) {
guard let string = buffer.readNullTerminatedString() else {
guard let string = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not read error response string.")
}
fields[field] = string
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Message/PostgresMessage+Execute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension PostgresMessage {

/// Serializes this message into a byte buffer.
public func serialize(into buffer: inout ByteBuffer) {
buffer.writeNullTerminatedString(portalName)
buffer.psqlWriteNullTerminatedString(portalName)
buffer.writeInteger(self.maxRows)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ extension PostgresMessage {
guard let backendPID: Int32 = buffer.readInteger() else {
throw PostgresError.protocol("Invalid NotificationResponse message: unable to read backend PID")
}
guard let channel = buffer.readNullTerminatedString() else {
guard let channel = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Invalid NotificationResponse message: unable to read channel")
}
guard let payload = buffer.readNullTerminatedString() else {
guard let payload = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Invalid NotificationResponse message: unable to read payload")
}
return .init(backendPID: backendPID, channel: channel, payload: payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ extension PostgresMessage {
public struct ParameterStatus: PostgresMessageType, CustomStringConvertible {
/// Parses an instance of this message type from a byte buffer.
public static func parse(from buffer: inout ByteBuffer) throws -> ParameterStatus {
guard let parameter = buffer.readNullTerminatedString() else {
guard let parameter = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not read parameter from parameter status message")
}
guard let value = buffer.readNullTerminatedString() else {
guard let value = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not read value from parameter status message")
}
return .init(parameter: parameter, value: value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension PostgresMessage {
/// Describes a single field returns in a `RowDescription` message.
public struct Field: CustomStringConvertible {
static func parse(from buffer: inout ByteBuffer) throws -> Field {
guard let name = buffer.readNullTerminatedString() else {
guard let name = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not read row description field name")
}
guard let tableOID = buffer.readInteger(as: UInt32.self) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension PostgresMessage {
public let initialData: [UInt8]

public static func parse(from buffer: inout ByteBuffer) throws -> PostgresMessage.SASLInitialResponse {
guard let mechanism = buffer.readNullTerminatedString() else {
guard let mechanism = buffer.psqlReadNullTerminatedString() else {
throw PostgresError.protocol("Could not parse SASL mechanism from initial response message")
}
guard let dataLength = buffer.readInteger(as: Int32.self) else {
Expand All @@ -57,7 +57,7 @@ extension PostgresMessage {
}

public func serialize(into buffer: inout ByteBuffer) throws {
buffer.writeNullTerminatedString(mechanism)
buffer.psqlWriteNullTerminatedString(mechanism)
if initialData.count > 0 {
buffer.writeInteger(Int32(initialData.count), as: Int32.self) // write(array:) writes Int16, which is incorrect here
buffer.writeBytes(initialData)
Expand Down
12 changes: 6 additions & 6 deletions Sources/PostgresNIO/New/Data/Float+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ extension Float: PSQLCodable {
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 {
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return float
case (.binary, .float8):
guard buffer.readableBytes == 8, let double = buffer.readDouble() else {
guard buffer.readableBytes == 8, let double = buffer.psqlReadDouble() else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return Float(double)
Expand All @@ -32,7 +32,7 @@ extension Float: PSQLCodable {
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
byteBuffer.writeFloat(self)
byteBuffer.psqlWriteFloat(self)
}
}

Expand All @@ -48,12 +48,12 @@ extension Double: PSQLCodable {
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 {
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return Double(float)
case (.binary, .float8):
guard buffer.readableBytes == 8, let double = buffer.readDouble() else {
guard buffer.readableBytes == 8, let double = buffer.psqlReadDouble() else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return double
Expand All @@ -68,7 +68,7 @@ extension Double: PSQLCodable {
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
byteBuffer.writeDouble(self)
byteBuffer.psqlWriteDouble(self)
}
}

16 changes: 8 additions & 8 deletions Sources/PostgresNIO/New/Extensions/ByteBuffer+PSQL.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import NIOCore

internal extension ByteBuffer {
mutating func writeNullTerminatedString(_ string: String) {
mutating func psqlWriteNullTerminatedString(_ string: String) {
self.writeString(string)
self.writeInteger(0, as: UInt8.self)
}

mutating func readNullTerminatedString() -> String? {
mutating func psqlReadNullTerminatedString() -> String? {
guard let nullIndex = readableBytesView.firstIndex(of: 0) else {
return nil
}
Expand All @@ -15,27 +15,27 @@ internal extension ByteBuffer {
return readString(length: nullIndex - readerIndex)
}

mutating func writeBackendMessageID(_ messageID: PSQLBackendMessage.ID) {
mutating func psqlWriteBackendMessageID(_ messageID: PSQLBackendMessage.ID) {
self.writeInteger(messageID.rawValue)
}

mutating func writeFrontendMessageID(_ messageID: PSQLFrontendMessage.ID) {
mutating func psqlWriteFrontendMessageID(_ messageID: PSQLFrontendMessage.ID) {
self.writeInteger(messageID.rawValue)
}

mutating func readFloat() -> Float? {
mutating func psqlReadFloat() -> Float? {
return self.readInteger(as: UInt32.self).map { Float(bitPattern: $0) }
}

mutating func readDouble() -> Double? {
mutating func psqlReadDouble() -> Double? {
return self.readInteger(as: UInt64.self).map { Double(bitPattern: $0) }
}

mutating func writeFloat(_ float: Float) {
mutating func psqlWriteFloat(_ float: Float) {
self.writeInteger(float.bitPattern)
}

mutating func writeDouble(_ double: Double) {
mutating func psqlWriteDouble(_ double: Double) {
self.writeInteger(double.bitPattern)
}
}
6 changes: 3 additions & 3 deletions Sources/PostgresNIO/New/Messages/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension PSQLBackendMessage {
case saslFinal(data: ByteBuffer)

static func decode(from buffer: inout ByteBuffer) throws -> Self {
try buffer.ensureAtLeastNBytesRemaining(2)
try buffer.psqlEnsureAtLeastNBytesRemaining(2)

// we have at least two bytes remaining, therefore we can force unwrap this read.
let authID = buffer.readInteger(as: Int32.self)!
Expand All @@ -29,7 +29,7 @@ extension PSQLBackendMessage {
case 3:
return .plaintext
case 5:
try buffer.ensureExactNBytesRemaining(4)
try buffer.psqlEnsureExactNBytesRemaining(4)
let salt1 = buffer.readInteger(as: UInt8.self)!
let salt2 = buffer.readInteger(as: UInt8.self)!
let salt3 = buffer.readInteger(as: UInt8.self)!
Expand All @@ -47,7 +47,7 @@ extension PSQLBackendMessage {
case 10:
var names = [String]()
let endIndex = buffer.readerIndex + buffer.readableBytes
while buffer.readerIndex < endIndex, let next = buffer.readNullTerminatedString() {
while buffer.readerIndex < endIndex, let next = buffer.psqlReadNullTerminatedString() {
names.append(next)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/New/Messages/BackendKeyData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension PSQLBackendMessage {
let secretKey: Int32

static func decode(from buffer: inout ByteBuffer) throws -> Self {
try buffer.ensureExactNBytesRemaining(8)
try buffer.psqlEnsureExactNBytesRemaining(8)

// We have verified the correct length before, this means we have exactly eight bytes
// to read. If we have enough readable bytes, a read of Int32 should always succeed.
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgresNIO/New/Messages/Bind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extension PSQLFrontendMessage {
var parameters: [PSQLEncodable]

func encode(into buffer: inout ByteBuffer, using jsonEncoder: PSQLJSONEncoder) throws {
buffer.writeNullTerminatedString(self.portalName)
buffer.writeNullTerminatedString(self.preparedStatementName)
buffer.psqlWriteNullTerminatedString(self.portalName)
buffer.psqlWriteNullTerminatedString(self.preparedStatementName)

// The number of parameter format codes that follow (denoted C below). This can be
// zero to indicate that there are no parameters or that the parameters all use the
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgresNIO/New/Messages/Close.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ extension PSQLFrontendMessage {
switch self {
case .preparedStatement(let name):
buffer.writeInteger(UInt8(ascii: "S"))
buffer.writeNullTerminatedString(name)
buffer.psqlWriteNullTerminatedString(name)
case .portal(let name):
buffer.writeInteger(UInt8(ascii: "P"))
buffer.writeNullTerminatedString(name)
buffer.psqlWriteNullTerminatedString(name)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/PostgresNIO/New/Messages/DataRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ struct DataRow: PSQLBackendMessage.PayloadDecodable, Equatable {
var bytes: ByteBuffer

static func decode(from buffer: inout ByteBuffer) throws -> Self {
try buffer.ensureAtLeastNBytesRemaining(2)
try buffer.psqlEnsureAtLeastNBytesRemaining(2)
let columnCount = buffer.readInteger(as: Int16.self)!
let firstColumnIndex = buffer.readerIndex

for _ in 0..<columnCount {
try buffer.ensureAtLeastNBytesRemaining(2)
try buffer.psqlEnsureAtLeastNBytesRemaining(2)
let bufferLength = Int(buffer.readInteger(as: Int32.self)!)

guard bufferLength >= 0 else {
// if buffer length is negative, this means that the value is null
continue
}

try buffer.ensureAtLeastNBytesRemaining(bufferLength)
try buffer.psqlEnsureAtLeastNBytesRemaining(bufferLength)
buffer.moveReaderIndex(forwardBy: bufferLength)
}

try buffer.ensureExactNBytesRemaining(0)
try buffer.psqlEnsureExactNBytesRemaining(0)

buffer.moveReaderIndex(to: firstColumnIndex)
let columnSlice = buffer.readSlice(length: buffer.readableBytes)!
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgresNIO/New/Messages/Describe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ extension PSQLFrontendMessage {
switch self {
case .preparedStatement(let name):
buffer.writeInteger(UInt8(ascii: "S"))
buffer.writeNullTerminatedString(name)
buffer.psqlWriteNullTerminatedString(name)
case .portal(let name):
buffer.writeInteger(UInt8(ascii: "P"))
buffer.writeNullTerminatedString(name)
buffer.psqlWriteNullTerminatedString(name)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/New/Messages/ErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension PSQLBackendMessage.PayloadDecodable where Self: PSQLMessageNotice {
asType: PSQLBackendMessage.Field.self)
}

guard let string = buffer.readNullTerminatedString() else {
guard let string = buffer.psqlReadNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
fields[field] = string
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/New/Messages/Execute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension PSQLFrontendMessage {
}

func encode(into buffer: inout ByteBuffer) {
buffer.writeNullTerminatedString(self.portalName)
buffer.psqlWriteNullTerminatedString(self.portalName)
buffer.writeInteger(self.maxNumberOfRows)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/PostgresNIO/New/Messages/NotificationResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ extension PSQLBackendMessage {
let payload: String

static func decode(from buffer: inout ByteBuffer) throws -> PSQLBackendMessage.NotificationResponse {
try buffer.ensureAtLeastNBytesRemaining(6)
try buffer.psqlEnsureAtLeastNBytesRemaining(6)
let backendPID = buffer.readInteger(as: Int32.self)!

guard let channel = buffer.readNullTerminatedString() else {
guard let channel = buffer.psqlReadNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
guard let payload = buffer.readNullTerminatedString() else {
guard let payload = buffer.psqlReadNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}

Expand Down
Loading

0 comments on commit 3931a06

Please sign in to comment.