Skip to content
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

Message encoder/decoder #1664

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Sources/GRPCCore/Coding/Coding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// Serializes a message into a sequence of bytes.
///
/// Message serializers convert an input message to a sequence of bytes. Serializers are used to
/// convert messages into a form which is suitable for sending over a network. The reverse
/// operation, deserialization, is performed by a ``MessageDeserializer``.
///
/// Serializers are used frequently and implementations should take care to ensure that
/// serialization is as cheap as possible.
public protocol MessageSerializer<Message>: Sendable {
/// The type of message this serializer can serialize.
associatedtype Message

/// Serializes a ``Message`` into a sequence of bytes.
///
/// - Parameter message: The message to serialize.
/// - Returns: The serialized bytes of a message.
func serialize(_ message: Message) throws -> [UInt8]
}

/// Deserializes a sequence of bytes into a message.
///
/// Message deserializers convert a sequence of bytes into a message. Deserializers are used to
/// convert bytes received from the network into an application specific message. The reverse
/// operation, serialization, is performed by a ``MessageSerializer``.
///
/// Deserializers are used frequently and implementations should take care to ensure that
/// deserialization is as cheap as possible.
public protocol MessageDeserializer<Message>: Sendable {
/// The type of message this deserializer can deserialize.
associatedtype Message

/// Deserializes a sequence of bytes into a ``Message``.
///
/// - Parameter serializedMessageBytes: The bytes to deserialize.
/// - Returns: The deserialized message.
func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message
}
42 changes: 42 additions & 0 deletions Tests/GRPCCoreTests/Coding/CodingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import GRPCCore
import XCTest

final class CodingTests: XCTestCase {
func testJSONRoundtrip() throws {
// This test just demonstrates that the API is suitable.

struct Message: Codable, Hashable {
var foo: String
var bar: Int
var baz: Baz

struct Baz: Codable, Hashable {
var bazzy: Double
}
}

let message = Message(foo: "foo", bar: 42, baz: .init(bazzy: 3.1415))

let serializer = JSONSerializer<Message>()
let deserializer = JSONDeserializer<Message>()

let bytes = try serializer.serialize(message)
let roundTrip = try deserializer.deserialize(bytes)
XCTAssertEqual(roundTrip, message)
}
}
43 changes: 43 additions & 0 deletions Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import GRPCCore

import struct Foundation.Data
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder

private let jsonEncoder = JSONEncoder()
private let jsonDecoder = JSONDecoder()

struct JSONSerializer<Message: Codable>: MessageSerializer {
func serialize(_ message: Message) throws -> [UInt8] {
do {
return try Array(jsonEncoder.encode(message))
} catch {
throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
}
}
}

struct JSONDeserializer<Message: Codable>: MessageDeserializer {
func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message {
do {
return try jsonDecoder.decode(Message.self, from: Data(serializedMessageBytes))
} catch {
throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
}
}
}