From b164ce94033cbaa9a8c142d2bdb44c7861d9c076 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 1 Aug 2018 11:28:52 -0400 Subject: [PATCH] Code generate Swift wrappers for protocol buffers. --- SignalServiceKit/protobuf/Makefile | 8 + SignalServiceKit/protobuf/Provisioning.proto | 2 +- .../Protos/Generated/FingerprintProto.swift | 125 ++++++++ .../Protos/Generated/Provisioning.pb.swift | 44 ++- .../Protos/Generated/ProvisioningProto.swift | 190 +++++++++++ .../src/Protos/Generated/SignalIOSProto.swift | 145 +++++++++ .../src/Protos/Generated/WebSocketProto.swift | 298 ++++++++++++++++++ 7 files changed, 787 insertions(+), 25 deletions(-) create mode 100644 SignalServiceKit/src/Protos/Generated/FingerprintProto.swift create mode 100644 SignalServiceKit/src/Protos/Generated/ProvisioningProto.swift create mode 100644 SignalServiceKit/src/Protos/Generated/SignalIOSProto.swift create mode 100644 SignalServiceKit/src/Protos/Generated/WebSocketProto.swift diff --git a/SignalServiceKit/protobuf/Makefile b/SignalServiceKit/protobuf/Makefile index a0ae94ad1..fd5eb7ead 100644 --- a/SignalServiceKit/protobuf/Makefile +++ b/SignalServiceKit/protobuf/Makefile @@ -16,15 +16,23 @@ signal_service_protos: SignalService.proto provisioning_protos: Provisioning.proto $(PROTOC) --swift_out=../src/Protos/Generated \ Provisioning.proto + $(WRAPPER_SCRIPT) --dst-dir=../src/Protos/Generated \ + --wrapper-prefix=ProvisioningProto --proto-prefix=ProvisioningProtos --proto-file=Provisioning.proto fingerprint_protos: Fingerprint.proto $(PROTOC) --swift_out=../src/Protos/Generated \ Fingerprint.proto + $(WRAPPER_SCRIPT) --dst-dir=../src/Protos/Generated \ + --wrapper-prefix=FingerprintProto --proto-prefix=FingerprintProtos --proto-file=Fingerprint.proto websocket_protos: WebSocketResources.proto $(PROTOC) --swift_out=../src/Protos/Generated \ WebSocketResources.proto + $(WRAPPER_SCRIPT) --dst-dir=../src/Protos/Generated \ + --wrapper-prefix=WebSocketProto --proto-prefix=WebSocketProtos --proto-file=WebSocketResources.proto signal_ios_protos: SignalIOS.proto $(PROTOC) --swift_out=../src/Protos/Generated \ SignalIOS.proto + $(WRAPPER_SCRIPT) --dst-dir=../src/Protos/Generated \ + --wrapper-prefix=SignalIOSProto --proto-prefix=IOSProtos --proto-file=SignalIOS.proto diff --git a/SignalServiceKit/protobuf/Provisioning.proto b/SignalServiceKit/protobuf/Provisioning.proto index 065b94a5e..d295131c8 100644 --- a/SignalServiceKit/protobuf/Provisioning.proto +++ b/SignalServiceKit/protobuf/Provisioning.proto @@ -8,7 +8,7 @@ // the legacy proto format. syntax = "proto2"; -package ProvisioningProto; +package ProvisioningProtos; option java_package = "org.whispersystems.signalservice.internal.push"; option java_outer_classname = "ProvisioningProtos"; diff --git a/SignalServiceKit/src/Protos/Generated/FingerprintProto.swift b/SignalServiceKit/src/Protos/Generated/FingerprintProto.swift new file mode 100644 index 000000000..4048fdc56 --- /dev/null +++ b/SignalServiceKit/src/Protos/Generated/FingerprintProto.swift @@ -0,0 +1,125 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +import Foundation + +// WARNING: This code is generated. Only edit within the markers. + +public enum FingerprintProtoError: Error { + case invalidProtobuf(description: String) +} + +// MARK: - FingerprintProtoLogicalFingerprint + +@objc public class FingerprintProtoLogicalFingerprint: NSObject { + + @objc public let identityData: Data? + + @objc public init(identityData: Data?) { + self.identityData = identityData + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> FingerprintProtoLogicalFingerprint { + let proto = try FingerprintProtos_LogicalFingerprint(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: FingerprintProtos_LogicalFingerprint) throws -> FingerprintProtoLogicalFingerprint { + var identityData: Data? = nil + if proto.hasIdentityData { + identityData = proto.identityData + } + + // MARK: - Begin Validation Logic for FingerprintProtoLogicalFingerprint - + + // MARK: - End Validation Logic for FingerprintProtoLogicalFingerprint - + + let result = FingerprintProtoLogicalFingerprint(identityData: identityData) + return result + } + + fileprivate var asProtobuf: FingerprintProtos_LogicalFingerprint { + let proto = FingerprintProtos_LogicalFingerprint.with { (builder) in + if let identityData = self.identityData { + builder.identityData = identityData + } + } + + return proto + } +} + +// MARK: - FingerprintProtoLogicalFingerprints + +@objc public class FingerprintProtoLogicalFingerprints: NSObject { + + @objc public let version: UInt32 + @objc public let localFingerprint: FingerprintProtoLogicalFingerprint? + @objc public let remoteFingerprint: FingerprintProtoLogicalFingerprint? + + @objc public init(version: UInt32, + localFingerprint: FingerprintProtoLogicalFingerprint?, + remoteFingerprint: FingerprintProtoLogicalFingerprint?) { + self.version = version + self.localFingerprint = localFingerprint + self.remoteFingerprint = remoteFingerprint + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> FingerprintProtoLogicalFingerprints { + let proto = try FingerprintProtos_LogicalFingerprints(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: FingerprintProtos_LogicalFingerprints) throws -> FingerprintProtoLogicalFingerprints { + var version: UInt32 = 0 + if proto.hasVersion { + version = proto.version + } + + var localFingerprint: FingerprintProtoLogicalFingerprint? = nil + if proto.hasLocalFingerprint { + localFingerprint = try FingerprintProtoLogicalFingerprint.parseProto(proto.localFingerprint) + } + + var remoteFingerprint: FingerprintProtoLogicalFingerprint? = nil + if proto.hasRemoteFingerprint { + remoteFingerprint = try FingerprintProtoLogicalFingerprint.parseProto(proto.remoteFingerprint) + } + + // MARK: - Begin Validation Logic for FingerprintProtoLogicalFingerprints - + + // MARK: - End Validation Logic for FingerprintProtoLogicalFingerprints - + + let result = FingerprintProtoLogicalFingerprints(version: version, + localFingerprint: localFingerprint, + remoteFingerprint: remoteFingerprint) + return result + } + + fileprivate var asProtobuf: FingerprintProtos_LogicalFingerprints { + let proto = FingerprintProtos_LogicalFingerprints.with { (builder) in + builder.version = self.version + + if let localFingerprint = self.localFingerprint { + builder.localFingerprint = localFingerprint.asProtobuf + } + + if let remoteFingerprint = self.remoteFingerprint { + builder.remoteFingerprint = remoteFingerprint.asProtobuf + } + } + + return proto + } +} diff --git a/SignalServiceKit/src/Protos/Generated/Provisioning.pb.swift b/SignalServiceKit/src/Protos/Generated/Provisioning.pb.swift index 42732a28a..3f111622a 100644 --- a/SignalServiceKit/src/Protos/Generated/Provisioning.pb.swift +++ b/SignalServiceKit/src/Protos/Generated/Provisioning.pb.swift @@ -1,10 +1,6 @@ -// DO NOT EDIT. // -// Generated by the Swift generator plugin for the protocol buffer compiler. -// Source: Provisioning.proto +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // -// For information on using the generated types, please see the documenation: -// https://github.com/apple/swift-protobuf/ //* // Copyright (C) 2014-2016 Open Whisper Systems @@ -22,12 +18,12 @@ import SwiftProtobuf // incompatible with the version of SwiftProtobuf to which you are linking. // Please ensure that your are building against the same version of the API // that was used to generate this file. -fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck { +private struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck { struct _2: SwiftProtobuf.ProtobufAPIVersion_2 {} typealias Version = _2 } -struct ProvisioningProto_ProvisionEnvelope { +struct ProvisioningProtos_ProvisionEnvelope { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for // methods supported on all messages. @@ -55,11 +51,11 @@ struct ProvisioningProto_ProvisionEnvelope { init() {} - fileprivate var _publicKey: Data? = nil - fileprivate var _body: Data? = nil + fileprivate var _publicKey: Data? + fileprivate var _body: Data? } -struct ProvisioningProto_ProvisionMessage { +struct ProvisioningProtos_ProvisionMessage { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for // methods supported on all messages. @@ -131,24 +127,24 @@ struct ProvisioningProto_ProvisionMessage { init() {} - fileprivate var _identityKeyPublic: Data? = nil - fileprivate var _identityKeyPrivate: Data? = nil - fileprivate var _number: String? = nil - fileprivate var _provisioningCode: String? = nil - fileprivate var _userAgent: String? = nil - fileprivate var _profileKey: Data? = nil - fileprivate var _readReceipts: Bool? = nil + fileprivate var _identityKeyPublic: Data? + fileprivate var _identityKeyPrivate: Data? + fileprivate var _number: String? + fileprivate var _provisioningCode: String? + fileprivate var _userAgent: String? + fileprivate var _profileKey: Data? + fileprivate var _readReceipts: Bool? } // MARK: - Code below here is support for the SwiftProtobuf runtime. -fileprivate let _protobuf_package = "ProvisioningProto" +private let _protobuf_package = "ProvisioningProtos" -extension ProvisioningProto_ProvisionEnvelope: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { +extension ProvisioningProtos_ProvisionEnvelope: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { static let protoMessageName: String = _protobuf_package + ".ProvisionEnvelope" static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ 1: .same(proto: "publicKey"), - 2: .same(proto: "body"), + 2: .same(proto: "body") ] mutating func decodeMessage(decoder: inout D) throws { @@ -171,7 +167,7 @@ extension ProvisioningProto_ProvisionEnvelope: SwiftProtobuf.Message, SwiftProto try unknownFields.traverse(visitor: &visitor) } - func _protobuf_generated_isEqualTo(other: ProvisioningProto_ProvisionEnvelope) -> Bool { + func _protobuf_generated_isEqualTo(other: ProvisioningProtos_ProvisionEnvelope) -> Bool { if self._publicKey != other._publicKey {return false} if self._body != other._body {return false} if unknownFields != other.unknownFields {return false} @@ -179,7 +175,7 @@ extension ProvisioningProto_ProvisionEnvelope: SwiftProtobuf.Message, SwiftProto } } -extension ProvisioningProto_ProvisionMessage: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { +extension ProvisioningProtos_ProvisionMessage: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { static let protoMessageName: String = _protobuf_package + ".ProvisionMessage" static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ 1: .same(proto: "identityKeyPublic"), @@ -188,7 +184,7 @@ extension ProvisioningProto_ProvisionMessage: SwiftProtobuf.Message, SwiftProtob 4: .same(proto: "provisioningCode"), 5: .same(proto: "userAgent"), 6: .same(proto: "profileKey"), - 7: .same(proto: "readReceipts"), + 7: .same(proto: "readReceipts") ] mutating func decodeMessage(decoder: inout D) throws { @@ -231,7 +227,7 @@ extension ProvisioningProto_ProvisionMessage: SwiftProtobuf.Message, SwiftProtob try unknownFields.traverse(visitor: &visitor) } - func _protobuf_generated_isEqualTo(other: ProvisioningProto_ProvisionMessage) -> Bool { + func _protobuf_generated_isEqualTo(other: ProvisioningProtos_ProvisionMessage) -> Bool { if self._identityKeyPublic != other._identityKeyPublic {return false} if self._identityKeyPrivate != other._identityKeyPrivate {return false} if self._number != other._number {return false} diff --git a/SignalServiceKit/src/Protos/Generated/ProvisioningProto.swift b/SignalServiceKit/src/Protos/Generated/ProvisioningProto.swift new file mode 100644 index 000000000..e044b6c50 --- /dev/null +++ b/SignalServiceKit/src/Protos/Generated/ProvisioningProto.swift @@ -0,0 +1,190 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +import Foundation + +// WARNING: This code is generated. Only edit within the markers. + +public enum ProvisioningProtoError: Error { + case invalidProtobuf(description: String) +} + +// MARK: - ProvisioningProtoProvisionEnvelope + +@objc public class ProvisioningProtoProvisionEnvelope: NSObject { + + @objc public let publicKey: Data? + @objc public let body: Data? + + @objc public init(publicKey: Data?, + body: Data?) { + self.publicKey = publicKey + self.body = body + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> ProvisioningProtoProvisionEnvelope { + let proto = try ProvisioningProtos_ProvisionEnvelope(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: ProvisioningProtos_ProvisionEnvelope) throws -> ProvisioningProtoProvisionEnvelope { + var publicKey: Data? = nil + if proto.hasPublicKey { + publicKey = proto.publicKey + } + + var body: Data? = nil + if proto.hasBody { + body = proto.body + } + + // MARK: - Begin Validation Logic for ProvisioningProtoProvisionEnvelope - + + // MARK: - End Validation Logic for ProvisioningProtoProvisionEnvelope - + + let result = ProvisioningProtoProvisionEnvelope(publicKey: publicKey, + body: body) + return result + } + + fileprivate var asProtobuf: ProvisioningProtos_ProvisionEnvelope { + let proto = ProvisioningProtos_ProvisionEnvelope.with { (builder) in + if let publicKey = self.publicKey { + builder.publicKey = publicKey + } + + if let body = self.body { + builder.body = body + } + } + + return proto + } +} + +// MARK: - ProvisioningProtoProvisionMessage + +@objc public class ProvisioningProtoProvisionMessage: NSObject { + + @objc public let identityKeyPublic: Data? + @objc public let identityKeyPrivate: Data? + @objc public let number: String? + @objc public let provisioningCode: String? + @objc public let userAgent: String? + @objc public let profileKey: Data? + @objc public let readReceipts: Bool + + @objc public init(identityKeyPublic: Data?, + identityKeyPrivate: Data?, + number: String?, + provisioningCode: String?, + userAgent: String?, + profileKey: Data?, + readReceipts: Bool) { + self.identityKeyPublic = identityKeyPublic + self.identityKeyPrivate = identityKeyPrivate + self.number = number + self.provisioningCode = provisioningCode + self.userAgent = userAgent + self.profileKey = profileKey + self.readReceipts = readReceipts + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> ProvisioningProtoProvisionMessage { + let proto = try ProvisioningProtos_ProvisionMessage(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: ProvisioningProtos_ProvisionMessage) throws -> ProvisioningProtoProvisionMessage { + var identityKeyPublic: Data? = nil + if proto.hasIdentityKeyPublic { + identityKeyPublic = proto.identityKeyPublic + } + + var identityKeyPrivate: Data? = nil + if proto.hasIdentityKeyPrivate { + identityKeyPrivate = proto.identityKeyPrivate + } + + var number: String? = nil + if proto.hasNumber { + number = proto.number + } + + var provisioningCode: String? = nil + if proto.hasProvisioningCode { + provisioningCode = proto.provisioningCode + } + + var userAgent: String? = nil + if proto.hasUserAgent { + userAgent = proto.userAgent + } + + var profileKey: Data? = nil + if proto.hasProfileKey { + profileKey = proto.profileKey + } + + var readReceipts: Bool = false + if proto.hasReadReceipts { + readReceipts = proto.readReceipts + } + + // MARK: - Begin Validation Logic for ProvisioningProtoProvisionMessage - + + // MARK: - End Validation Logic for ProvisioningProtoProvisionMessage - + + let result = ProvisioningProtoProvisionMessage(identityKeyPublic: identityKeyPublic, + identityKeyPrivate: identityKeyPrivate, + number: number, + provisioningCode: provisioningCode, + userAgent: userAgent, + profileKey: profileKey, + readReceipts: readReceipts) + return result + } + + fileprivate var asProtobuf: ProvisioningProtos_ProvisionMessage { + let proto = ProvisioningProtos_ProvisionMessage.with { (builder) in + if let identityKeyPublic = self.identityKeyPublic { + builder.identityKeyPublic = identityKeyPublic + } + + if let identityKeyPrivate = self.identityKeyPrivate { + builder.identityKeyPrivate = identityKeyPrivate + } + + if let number = self.number { + builder.number = number + } + + if let provisioningCode = self.provisioningCode { + builder.provisioningCode = provisioningCode + } + + if let userAgent = self.userAgent { + builder.userAgent = userAgent + } + + if let profileKey = self.profileKey { + builder.profileKey = profileKey + } + + builder.readReceipts = self.readReceipts + } + + return proto + } +} diff --git a/SignalServiceKit/src/Protos/Generated/SignalIOSProto.swift b/SignalServiceKit/src/Protos/Generated/SignalIOSProto.swift new file mode 100644 index 000000000..d79a5deef --- /dev/null +++ b/SignalServiceKit/src/Protos/Generated/SignalIOSProto.swift @@ -0,0 +1,145 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +import Foundation + +// WARNING: This code is generated. Only edit within the markers. + +public enum SignalIOSProtoError: Error { + case invalidProtobuf(description: String) +} + +// MARK: - SignalIOSProtoBackupSnapshotBackupEntity + +@objc public class SignalIOSProtoBackupSnapshotBackupEntity: NSObject { + + // MARK: - SignalIOSProtoBackupSnapshotBackupEntityType + + @objc public enum SignalIOSProtoBackupSnapshotBackupEntityType: Int32 { + case unknown = 0 + case migration = 1 + case thread = 2 + case interaction = 3 + case attachment = 4 + } + + private class func SignalIOSProtoBackupSnapshotBackupEntityTypeWrap(_ value: IOSProtos_BackupSnapshot.BackupEntity.TypeEnum) -> SignalIOSProtoBackupSnapshotBackupEntityType { + switch value { + case .unknown: return .unknown + case .migration: return .migration + case .thread: return .thread + case .interaction: return .interaction + case .attachment: return .attachment + } + } + + private class func SignalIOSProtoBackupSnapshotBackupEntityTypeUnwrap(_ value: SignalIOSProtoBackupSnapshotBackupEntityType) -> IOSProtos_BackupSnapshot.BackupEntity.TypeEnum { + switch value { + case .unknown: return .unknown + case .migration: return .migration + case .thread: return .thread + case .interaction: return .interaction + case .attachment: return .attachment + } + } + + @objc public let type: SignalIOSProtoBackupSnapshotBackupEntityType + @objc public let entityData: Data? + + @objc public init(type: SignalIOSProtoBackupSnapshotBackupEntityType, + entityData: Data?) { + self.type = type + self.entityData = entityData + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> SignalIOSProtoBackupSnapshotBackupEntity { + let proto = try IOSProtos_BackupSnapshot.BackupEntity(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: IOSProtos_BackupSnapshot.BackupEntity) throws -> SignalIOSProtoBackupSnapshotBackupEntity { + var type: SignalIOSProtoBackupSnapshotBackupEntityType = .unknown + if proto.hasType { + type = SignalIOSProtoBackupSnapshotBackupEntityTypeWrap(proto.type) + } + + var entityData: Data? = nil + if proto.hasEntityData { + entityData = proto.entityData + } + + // MARK: - Begin Validation Logic for SignalIOSProtoBackupSnapshotBackupEntity - + + // MARK: - End Validation Logic for SignalIOSProtoBackupSnapshotBackupEntity - + + let result = SignalIOSProtoBackupSnapshotBackupEntity(type: type, + entityData: entityData) + return result + } + + fileprivate var asProtobuf: IOSProtos_BackupSnapshot.BackupEntity { + let proto = IOSProtos_BackupSnapshot.BackupEntity.with { (builder) in + builder.type = SignalIOSProtoBackupSnapshotBackupEntity.SignalIOSProtoBackupSnapshotBackupEntityTypeUnwrap(self.type) + + if let entityData = self.entityData { + builder.entityData = entityData + } + } + + return proto + } +} + +// MARK: - SignalIOSProtoBackupSnapshot + +@objc public class SignalIOSProtoBackupSnapshot: NSObject { + + @objc public let entity: [SignalIOSProtoBackupSnapshotBackupEntity] + + @objc public init(entity: [SignalIOSProtoBackupSnapshotBackupEntity]) { + self.entity = entity + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> SignalIOSProtoBackupSnapshot { + let proto = try IOSProtos_BackupSnapshot(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: IOSProtos_BackupSnapshot) throws -> SignalIOSProtoBackupSnapshot { + var entity: [SignalIOSProtoBackupSnapshotBackupEntity] = [] + for item in proto.entity { + let wrapped = try SignalIOSProtoBackupSnapshotBackupEntity.parseProto(item) + entity.append(wrapped) + } + + // MARK: - Begin Validation Logic for SignalIOSProtoBackupSnapshot - + + // MARK: - End Validation Logic for SignalIOSProtoBackupSnapshot - + + let result = SignalIOSProtoBackupSnapshot(entity: entity) + return result + } + + fileprivate var asProtobuf: IOSProtos_BackupSnapshot { + let proto = IOSProtos_BackupSnapshot.with { (builder) in + var entityUnwrapped = [IOSProtos_BackupSnapshot.BackupEntity]() + for item in entity { + entityUnwrapped.append(item.asProtobuf) + } + builder.entity = entityUnwrapped + } + + return proto + } +} diff --git a/SignalServiceKit/src/Protos/Generated/WebSocketProto.swift b/SignalServiceKit/src/Protos/Generated/WebSocketProto.swift new file mode 100644 index 000000000..57d0d7722 --- /dev/null +++ b/SignalServiceKit/src/Protos/Generated/WebSocketProto.swift @@ -0,0 +1,298 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +import Foundation + +// WARNING: This code is generated. Only edit within the markers. + +public enum WebSocketProtoError: Error { + case invalidProtobuf(description: String) +} + +// MARK: - WebSocketProtoWebSocketRequestMessage + +@objc public class WebSocketProtoWebSocketRequestMessage: NSObject { + + @objc public let verb: String? + @objc public let path: String? + @objc public let body: Data? + @objc public let headers: [String] + @objc public let requestId: UInt64 + + @objc public init(verb: String?, + path: String?, + body: Data?, + headers: [String], + requestId: UInt64) { + self.verb = verb + self.path = path + self.body = body + self.headers = headers + self.requestId = requestId + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> WebSocketProtoWebSocketRequestMessage { + let proto = try WebSocketProtos_WebSocketRequestMessage(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: WebSocketProtos_WebSocketRequestMessage) throws -> WebSocketProtoWebSocketRequestMessage { + var verb: String? = nil + if proto.hasVerb { + verb = proto.verb + } + + var path: String? = nil + if proto.hasPath { + path = proto.path + } + + var body: Data? = nil + if proto.hasBody { + body = proto.body + } + + var headers: [String] = [] + for item in proto.headers { + let wrapped = item + headers.append(wrapped) + } + + var requestId: UInt64 = 0 + if proto.hasRequestId { + requestId = proto.requestId + } + + // MARK: - Begin Validation Logic for WebSocketProtoWebSocketRequestMessage - + + // MARK: - End Validation Logic for WebSocketProtoWebSocketRequestMessage - + + let result = WebSocketProtoWebSocketRequestMessage(verb: verb, + path: path, + body: body, + headers: headers, + requestId: requestId) + return result + } + + fileprivate var asProtobuf: WebSocketProtos_WebSocketRequestMessage { + let proto = WebSocketProtos_WebSocketRequestMessage.with { (builder) in + if let verb = self.verb { + builder.verb = verb + } + + if let path = self.path { + builder.path = path + } + + if let body = self.body { + builder.body = body + } + + var headersUnwrapped = [String]() + for item in headers { + headersUnwrapped.append(item) + } + builder.headers = headersUnwrapped + + builder.requestId = self.requestId + } + + return proto + } +} + +// MARK: - WebSocketProtoWebSocketResponseMessage + +@objc public class WebSocketProtoWebSocketResponseMessage: NSObject { + + @objc public let requestId: UInt64 + @objc public let status: UInt32 + @objc public let message: String? + @objc public let headers: [String] + @objc public let body: Data? + + @objc public init(requestId: UInt64, + status: UInt32, + message: String?, + headers: [String], + body: Data?) { + self.requestId = requestId + self.status = status + self.message = message + self.headers = headers + self.body = body + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> WebSocketProtoWebSocketResponseMessage { + let proto = try WebSocketProtos_WebSocketResponseMessage(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: WebSocketProtos_WebSocketResponseMessage) throws -> WebSocketProtoWebSocketResponseMessage { + var requestId: UInt64 = 0 + if proto.hasRequestId { + requestId = proto.requestId + } + + var status: UInt32 = 0 + if proto.hasStatus { + status = proto.status + } + + var message: String? = nil + if proto.hasMessage { + message = proto.message + } + + var headers: [String] = [] + for item in proto.headers { + let wrapped = item + headers.append(wrapped) + } + + var body: Data? = nil + if proto.hasBody { + body = proto.body + } + + // MARK: - Begin Validation Logic for WebSocketProtoWebSocketResponseMessage - + + // MARK: - End Validation Logic for WebSocketProtoWebSocketResponseMessage - + + let result = WebSocketProtoWebSocketResponseMessage(requestId: requestId, + status: status, + message: message, + headers: headers, + body: body) + return result + } + + fileprivate var asProtobuf: WebSocketProtos_WebSocketResponseMessage { + let proto = WebSocketProtos_WebSocketResponseMessage.with { (builder) in + builder.requestId = self.requestId + + builder.status = self.status + + if let message = self.message { + builder.message = message + } + + var headersUnwrapped = [String]() + for item in headers { + headersUnwrapped.append(item) + } + builder.headers = headersUnwrapped + + if let body = self.body { + builder.body = body + } + } + + return proto + } +} + +// MARK: - WebSocketProtoWebSocketMessage + +@objc public class WebSocketProtoWebSocketMessage: NSObject { + + // MARK: - WebSocketProtoWebSocketMessageType + + @objc public enum WebSocketProtoWebSocketMessageType: Int32 { + case unknown = 0 + case request = 1 + case response = 2 + } + + private class func WebSocketProtoWebSocketMessageTypeWrap(_ value: WebSocketProtos_WebSocketMessage.TypeEnum) -> WebSocketProtoWebSocketMessageType { + switch value { + case .unknown: return .unknown + case .request: return .request + case .response: return .response + } + } + + private class func WebSocketProtoWebSocketMessageTypeUnwrap(_ value: WebSocketProtoWebSocketMessageType) -> WebSocketProtos_WebSocketMessage.TypeEnum { + switch value { + case .unknown: return .unknown + case .request: return .request + case .response: return .response + } + } + + @objc public let type: WebSocketProtoWebSocketMessageType + @objc public let request: WebSocketProtoWebSocketRequestMessage? + @objc public let response: WebSocketProtoWebSocketResponseMessage? + + @objc public init(type: WebSocketProtoWebSocketMessageType, + request: WebSocketProtoWebSocketRequestMessage?, + response: WebSocketProtoWebSocketResponseMessage?) { + self.type = type + self.request = request + self.response = response + } + + @objc + public func serializedData() throws -> Data { + return try self.asProtobuf.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> WebSocketProtoWebSocketMessage { + let proto = try WebSocketProtos_WebSocketMessage(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: WebSocketProtos_WebSocketMessage) throws -> WebSocketProtoWebSocketMessage { + var type: WebSocketProtoWebSocketMessageType = .unknown + if proto.hasType { + type = WebSocketProtoWebSocketMessageTypeWrap(proto.type) + } + + var request: WebSocketProtoWebSocketRequestMessage? = nil + if proto.hasRequest { + request = try WebSocketProtoWebSocketRequestMessage.parseProto(proto.request) + } + + var response: WebSocketProtoWebSocketResponseMessage? = nil + if proto.hasResponse { + response = try WebSocketProtoWebSocketResponseMessage.parseProto(proto.response) + } + + // MARK: - Begin Validation Logic for WebSocketProtoWebSocketMessage - + + // MARK: - End Validation Logic for WebSocketProtoWebSocketMessage - + + let result = WebSocketProtoWebSocketMessage(type: type, + request: request, + response: response) + return result + } + + fileprivate var asProtobuf: WebSocketProtos_WebSocketMessage { + let proto = WebSocketProtos_WebSocketMessage.with { (builder) in + builder.type = WebSocketProtoWebSocketMessage.WebSocketProtoWebSocketMessageTypeUnwrap(self.type) + + if let request = self.request { + builder.request = request.asProtobuf + } + + if let response = self.response { + builder.response = response.asProtobuf + } + } + + return proto + } +}