blob: 1374700ca81f3b06ab9e1b82c73411b69ded5df8 [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 {
Chris Simpsona9b6c702018-04-08 07:11:37 -040024
25 /// TType for instance
26 static var thriftType: TType { get }
27
28 /// Read TSerializable instance from Protocol
29 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
36extension 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
Chris Simpsona9b6c702018-04-08 07:11:37 -040045/// Default read/write for primitave Thrift types:
46/// Bool, Int8 (byte), Int16, Int32, Int64, Double, String
47
48extension Bool : TSerializable {
49 public static var thriftType: TType { return .bool }
50
51 public static func read(from proto: TProtocol) throws -> Bool {
52 return try proto.read()
53 }
54
55 public func write(to proto: TProtocol) throws {
56 try proto.write(self)
57 }
58}
59
60extension Int8 : TSerializable {
61 public static var thriftType: TType { return .i8 }
62
63 public static func read(from proto: TProtocol) throws -> Int8 {
64 return Int8(try proto.read() as UInt8)
65 }
66
67 public func write(to proto: TProtocol) throws {
68 try proto.write(UInt8(self))
69 }
70}
71
72extension Int16 : TSerializable {
73 public static var thriftType: TType { return .i16 }
74
75 public static func read(from proto: TProtocol) throws -> Int16 {
76 return try proto.read()
77 }
78
79 public func write(to proto: TProtocol) throws {
80 try proto.write(self)
81 }
82}
83
84extension Int32 : TSerializable {
85 public static var thriftType: TType { return .i32 }
86
87 public static func read(from proto: TProtocol) throws -> Int32 {
88 return try proto.read()
89 }
90
91 public func write(to proto: TProtocol) throws {
92 try proto.write(self)
93 }
94}
95
96
97extension Int64 : TSerializable {
98 public static var thriftType: TType { return .i64 }
99
100 public static func read(from proto: TProtocol) throws -> Int64 {
101 return try proto.read()
102 }
103
104 public func write(to proto: TProtocol) throws {
105 try proto.write(self)
106 }
107}
108
109extension Double : TSerializable {
110 public static var thriftType: TType { return .double }
111
112 public static func read(from proto: TProtocol) throws -> Double {
113 return try proto.read()
114 }
115
116 public func write(to proto: TProtocol) throws {
117 try proto.write(self)
118 }
119}
120
121extension String : TSerializable {
122 public static var thriftType: TType { return .string }
123
124 public static func read(from proto: TProtocol) throws -> String {
125 return try proto.read()
126 }
127
128 public func write(to proto: TProtocol) throws {
129 try proto.write(self)
130 }
131}