blob: ff976eb9a924fa7cf063657b709660a81d5d4014 [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 {
Kino Roya9da9eb2022-10-07 23:13:01 -070064 return try proto.read() as Int8
Chris Simpsona9b6c702018-04-08 07:11:37 -040065 }
66
67 public func write(to proto: TProtocol) throws {
Kino Roya9da9eb2022-10-07 23:13:01 -070068 try proto.write(Int8(self))
Chris Simpsona9b6c702018-04-08 07:11:37 -040069 }
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}
Kino Roya9da9eb2022-10-07 23:13:01 -0700132
133extension UUID : TSerializable {
134 public static var thriftType: TType { .uuid }
135
136 public static func read(from proto: TProtocol) throws -> UUID {
137 return try proto.read()
138 }
139
140 public func write(to proto: TProtocol) throws {
141 try proto.write(self)
142 }
143}