1 /*
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  * under the License.
18  */
19 
20 import Foundation
21 
22 
23 public protocol TSerializable {
24 
25   /// TType for instance
26   static var thriftType: TType { get }
27 
28   /// Read TSerializable instance from Protocol
readnull29   static func read(from proto: TProtocol) throws -> Self
30 
31   /// Write TSerializable instance to Protocol
32   func write(to proto: TProtocol) throws
33 
34 }
35 
36 extension TSerializable {
37   public static func write(_ value: Self, to proto: TProtocol) throws {
38     try value.write(to: proto)
39   }
40 
41   /// convenience for member access
42   public var thriftType: TType { return Self.thriftType }
43 }
44 
45 /// Default read/write for primitave Thrift types:
46 /// Bool, Int8 (byte), Int16, Int32, Int64, Double, String
47 
48 extension Bool : TSerializable {
49   public static var thriftType: TType { return .bool }
50 
readnull51   public static func read(from proto: TProtocol) throws -> Bool {
52     return try proto.read()
53   }
54 
writenull55   public func write(to proto: TProtocol) throws {
56     try proto.write(self)
57   }
58 }
59 
60 extension Int8 : TSerializable {
61   public static var thriftType: TType { return .i8 }
62 
readnull63   public static func read(from proto: TProtocol) throws -> Int8 {
64     return Int8(try proto.read() as UInt8)
65   }
66 
writenull67   public func write(to proto: TProtocol) throws {
68     try proto.write(UInt8(self))
69   }
70 }
71 
72 extension Int16 : TSerializable {
73   public static var thriftType: TType { return .i16 }
74 
readnull75   public static func read(from proto: TProtocol) throws -> Int16 {
76     return try proto.read()
77   }
78 
writenull79   public func write(to proto: TProtocol) throws {
80     try proto.write(self)
81   }
82 }
83 
84 extension Int32 : TSerializable {
85   public static var thriftType: TType { return .i32 }
86 
readnull87   public static func read(from proto: TProtocol) throws -> Int32 {
88     return try proto.read()
89   }
90 
writenull91   public func write(to proto: TProtocol) throws {
92     try proto.write(self)
93   }
94 }
95 
96 
97 extension Int64 : TSerializable {
98   public static var thriftType: TType { return .i64 }
99 
readnull100   public static func read(from proto: TProtocol) throws -> Int64 {
101     return try proto.read()
102   }
103 
writenull104   public func write(to proto: TProtocol) throws {
105     try proto.write(self)
106   }
107 }
108 
109 extension Double : TSerializable {
110   public static var thriftType: TType { return .double }
111 
readnull112   public static func read(from proto: TProtocol) throws -> Double {
113     return try proto.read()
114   }
115 
writenull116   public func write(to proto: TProtocol) throws {
117     try proto.write(self)
118   }
119 }
120 
121 extension String : TSerializable {
122   public static var thriftType: TType { return .string }
123 
readnull124   public static func read(from proto: TProtocol) throws -> String {
125     return try proto.read()
126   }
127 
writenull128   public func write(to proto: TProtocol) throws {
129     try proto.write(self)
130   }
131 }
132