2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
22 public enum TMessageType: Int32 {
29 public enum TType: Int32 {
47 public protocol TProtocol {
48 var transport: TTransport { get set }
49 init(on transport: TTransport)
52 func readMessageBegin() throws -> (String, TMessageType, Int32)
53 func readMessageEnd() throws
54 func readStructBegin() throws -> String
55 func readStructEnd() throws
56 func readFieldBegin() throws -> (String, TType, Int32)
57 func readFieldEnd() throws
58 func readMapBegin() throws -> (TType, TType, Int32)
59 func readMapEnd() throws
60 func readSetBegin() throws -> (TType, Int32)
61 func readSetEnd() throws
62 func readListBegin() throws -> (TType, Int32)
63 func readListEnd() throws
65 func read() throws -> String
66 func read() throws -> Bool
67 func read() throws -> UInt8
68 func read() throws -> Int16
69 func read() throws -> Int32
70 func read() throws -> Int64
71 func read() throws -> Double
72 func read() throws -> Data
76 func writeMessageBegin(name: String, type messageType: TMessageType, sequenceID: Int32) throws
77 func writeMessageEnd() throws
78 func writeStructBegin(name: String) throws
79 func writeStructEnd() throws
80 func writeFieldBegin(name: String, type fieldType: TType, fieldID: Int32) throws
81 func writeFieldStop() throws
82 func writeFieldEnd() throws
83 func writeMapBegin(keyType: TType, valueType: TType, size: Int32) throws
84 func writeMapEnd() throws
85 func writeSetBegin(elementType: TType, size: Int32) throws
86 func writeSetEnd() throws
87 func writeListBegin(elementType: TType, size: Int32) throws
88 func writeListEnd() throws
90 func write(_ value: String) throws
91 func write(_ value: Bool) throws
92 func write(_ value: UInt8) throws
93 func write(_ value: Int16) throws
94 func write(_ value: Int32) throws
95 func write(_ value: Int64) throws
96 func write(_ value: Double) throws
97 func write(_ value: Data) throws
100 public extension TProtocol {
101 func writeFieldValue(_ value: TSerializable, name: String, type: TType, id: Int32) throws {
102 try writeFieldBegin(name: name, type: type, fieldID: id)
103 try value.write(to: self)
107 func validateValue(_ value: Any?, named name: String) throws {
109 throw TProtocolError(error: .unknown, message: "Missing required value for field: \(name)")
113 func readResultMessageBegin() throws {
114 let (_, type, _) = try readMessageBegin();
115 if type == .exception {
116 let x = try readException()
122 func readException() throws -> TApplicationError {
123 return try TApplicationError.read(from: self)
126 func writeException(messageName name: String, sequenceID: Int32, ex: TApplicationError) throws {
127 try writeMessageBegin(name: name, type: .exception, sequenceID: sequenceID)
128 try ex.write(to: self)
129 try writeMessageEnd()
132 func skip(type: TType) throws {
134 case .bool: _ = try read() as Bool
135 case .i8: _ = try read() as UInt8
136 case .i16: _ = try read() as Int16
137 case .i32: _ = try read() as Int32
138 case .i64: _ = try read() as Int64
139 case .double: _ = try read() as Double
140 case .string: _ = try read() as String
143 _ = try readStructBegin()
145 let (_, fieldType, _) = try readFieldBegin()
146 if fieldType == .stop {
149 try skip(type: fieldType)
156 let (keyType, valueType, size) = try readMapBegin()
158 try skip(type: keyType)
159 try skip(type: valueType)
165 let (elemType, size) = try readSetBegin()
167 try skip(type: elemType)
172 let (elemType, size) = try readListBegin()
174 try skip(type: elemType)
179 throw TProtocolError(error: .invalidData, message: "Invalid data")