blob: b45096b69e88f88f54f420940bfbc3eeec2886d3 [file] [log] [blame]
Chris Simpsona9b6c702018-04-08 07:11:37 -04001/*
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
20import Foundation
21
22
23public protocol TSerializable {
24 var hashValue: Int { get }
25
26 /// TType for instance
27 static var thriftType: TType { get }
28
29 /// Read TSerializable instance from Protocol
30 static func read(from proto: TProtocol) throws -> Self
31
32 /// Write TSerializable instance to Protocol
33 func write(to proto: TProtocol) throws
34
35}
36
37extension TSerializable {
38 public static func write(_ value: Self, to proto: TProtocol) throws {
39 try value.write(to: proto)
40 }
41
42 /// convenience for member access
43 public var thriftType: TType { return Self.thriftType }
44}
45
46public func ==<T>(lhs: T, rhs: T) -> Bool where T : TSerializable {
47 return lhs.hashValue == rhs.hashValue
48}
49
50/// Default read/write for primitave Thrift types:
51/// Bool, Int8 (byte), Int16, Int32, Int64, Double, String
52
53extension Bool : TSerializable {
54 public static var thriftType: TType { return .bool }
55
56 public static func read(from proto: TProtocol) throws -> Bool {
57 return try proto.read()
58 }
59
60 public func write(to proto: TProtocol) throws {
61 try proto.write(self)
62 }
63}
64
65extension Int8 : TSerializable {
66 public static var thriftType: TType { return .i8 }
67
68 public static func read(from proto: TProtocol) throws -> Int8 {
69 return Int8(try proto.read() as UInt8)
70 }
71
72 public func write(to proto: TProtocol) throws {
73 try proto.write(UInt8(self))
74 }
75}
76
77extension Int16 : TSerializable {
78 public static var thriftType: TType { return .i16 }
79
80 public static func read(from proto: TProtocol) throws -> Int16 {
81 return try proto.read()
82 }
83
84 public func write(to proto: TProtocol) throws {
85 try proto.write(self)
86 }
87}
88
89extension Int32 : TSerializable {
90 public static var thriftType: TType { return .i32 }
91
92 public static func read(from proto: TProtocol) throws -> Int32 {
93 return try proto.read()
94 }
95
96 public func write(to proto: TProtocol) throws {
97 try proto.write(self)
98 }
99}
100
101
102extension Int64 : TSerializable {
103 public static var thriftType: TType { return .i64 }
104
105 public static func read(from proto: TProtocol) throws -> Int64 {
106 return try proto.read()
107 }
108
109 public func write(to proto: TProtocol) throws {
110 try proto.write(self)
111 }
112}
113
114extension Double : TSerializable {
115 public static var thriftType: TType { return .double }
116
117 public static func read(from proto: TProtocol) throws -> Double {
118 return try proto.read()
119 }
120
121 public func write(to proto: TProtocol) throws {
122 try proto.write(self)
123 }
124}
125
126extension String : TSerializable {
127 public static var thriftType: TType { return .string }
128
129 public static func read(from proto: TProtocol) throws -> String {
130 return try proto.read()
131 }
132
133 public func write(to proto: TProtocol) throws {
134 try proto.write(self)
135 }
136}