Skip to content

Commit 9144e0d

Browse files
committed
wip serializers
1 parent f5c8c49 commit 9144e0d

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/.build
33
/Packages
44
/*.xcodeproj
5+
Package.resolved
6+

Package.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
// swift-tools-version:4.0
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
3-
42
import PackageDescription
53

64
let package = Package(
7-
name: "postgresql",
5+
name: "PostgreSQL",
86
products: [
9-
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10-
.library(
11-
name: "postgresql",
12-
targets: ["postgresql"]),
7+
.library(name: "PostgreSQL", targets: ["PostgreSQL"]),
138
],
149
dependencies: [
15-
// Dependencies declare other packages that this package depends on.
16-
// .package(url: /* package url */, from: "1.0.0"),
10+
// Swift Promises, Futures, and Streams.
11+
.package(url: "https://github.com/vapor/async.git", .branch("beta")),
12+
13+
// Core extensions, type-aliases, and functions that facilitate common tasks.
14+
.package(url: "https://github.com/vapor/core.git", .branch("beta")),
1715
],
1816
targets: [
19-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20-
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
21-
.target(
22-
name: "postgresql",
23-
dependencies: []),
24-
.testTarget(
25-
name: "postgresqlTests",
26-
dependencies: ["postgresql"]),
17+
.target(name: "PostgreSQL", dependencies: ["Async", "Bits"]),
18+
.testTarget(name: "PostgreSQLTests", dependencies: ["PostgreSQL"]),
2719
]
2820
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Bits
2+
3+
enum PostgresMessage {
4+
case startupMessage(
5+
/// The protocol version number. The most significant 16 bits are the major
6+
/// version number (3 for the protocol described here). The least significant
7+
/// 16 bits are the minor version number (0 for the protocol described here).
8+
protocolVersion: Int32,
9+
/// The protocol version number is followed by one or more pairs of parameter
10+
/// name and value strings. A zero byte is required as a terminator after
11+
/// the last name/value pair. Parameters can appear in any order. user is required,
12+
/// others are optional. Each parameter is specified as:
13+
parameters: [String: String]
14+
)
15+
16+
17+
/// The first byte of a message identifies the message type
18+
var identifier: Byte? {
19+
switch self {
20+
case .startupMessage: return nil
21+
}
22+
}
23+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Async
2+
import Bits
3+
import Foundation
4+
5+
struct PostgresSerializerState {
6+
var remaining: Data
7+
}
8+
9+
final class PostgresSerializer: ByteSerializer {
10+
var state: ByteSerializerState<PostgresSerializer>
11+
12+
typealias Input = PostgresMessage
13+
typealias Output = ByteBuffer
14+
15+
let buffer: MutableByteBuffer
16+
17+
init() {
18+
buffer = MutableByteBuffer(start: .allocate(capacity: 4096), count: 4096)
19+
state = .init()
20+
}
21+
22+
func serialize(
23+
_ message: PostgresMessage,
24+
state: PostgresSerializerState?
25+
) throws -> ByteSerializerResult<PostgresSerializer> {
26+
if let state = state {
27+
return serialize(data: state.remaining)
28+
}
29+
30+
switch message {
31+
case .startupMessage(let protocolVersion, let parameters):
32+
var data = Data([0, 0, 0, 0])
33+
for word in protocolVersion.words {
34+
data.append(Byte(word))
35+
}
36+
for (key, val) in parameters {
37+
data.append(contentsOf: key.data(using: .ascii)!)
38+
data.append(contentsOf: [0])
39+
data.append(contentsOf: val.data(using: .ascii)!)
40+
data.append(contentsOf: [0])
41+
}
42+
data.append(contentsOf: [0])
43+
return serialize(data: data)
44+
}
45+
}
46+
47+
func serialize(data: Data) -> ByteSerializerResult<PostgresSerializer> {
48+
let count = data.copyBytes(to: buffer)
49+
let view = ByteBuffer(start: buffer.baseAddress, count: count)
50+
if data.count > count {
51+
return .incomplete(view, state: .init(remaining: data[count..<data.count]))
52+
} else {
53+
return .complete(view)
54+
}
55+
}
56+
57+
deinit {
58+
buffer.baseAddress?.deallocate(capacity: buffer.count)
59+
}
60+
}

Sources/postgresql/postgresql.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)