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
21 /// Protocol for Generated Structs to conform to
22 /// Dictionary maps field names to internal IDs and uses Reflection
23 /// to iterate through all fields.
24 /// `writeFieldValue(_:name:type:id:)` calls `TSerializable.write(to:)` internally
25 /// giving a nice recursive behavior for nested TStructs, TLists, TMaps, and TSets
26 public protocol TStruct : TSerializable {
27 static var fieldIds: [String: Int32] { get }
28 static var structName: String { get }
31 public extension TStruct {
32 static var fieldIds: [String: (id: Int32, type: TType)] { return [:] }
33 static var thriftType: TType { return .struct }
35 func write(to proto: TProtocol) throws {
36 // Write struct name first
37 try proto.writeStructBegin(name: Self.structName)
39 try self.forEach { name, value, id in
41 try proto.writeFieldValue(value, name: name,
42 type: value.thriftType, id: id)
44 try proto.writeFieldStop()
45 try proto.writeStructEnd()
51 self.forEach { _, value, _ in
52 result = prime &* result &+ (value.hashValue)
57 /// Provides a block for handling each (available) thrift property using reflection
58 /// Caveat: Skips over optional values
61 /// Provides a block for handling each (available) thrift property using reflection
63 /// - parameter block: block for handling property
65 /// - throws: rethrows any Error thrown in block
66 private func forEach(_ block: (_ name: String, _ value: TSerializable, _ id: Int32) throws -> Void) rethrows {
67 // Mirror the object, getting (name: String?, value: Any) for every property
68 let mirror = Mirror(reflecting: self)
70 // Iterate through all children, ignore empty property names
71 for (propName, propValue) in mirror.children {
72 guard let propName = propName else { continue }
74 if let tval = unwrap(any: propValue) as? TSerializable, let id = Self.fieldIds[propName] {
75 try block(propName, tval, id)
81 /// Any can mysteriously be an Optional<Any> at the same time,
82 /// this checks and always returns Optional<Any> without double wrapping
83 /// we then try to bind value as TSerializable to ignore any extension properties
84 /// and the like and verify the property exists and grab the Thrift
85 /// property ID at the same time
87 /// - parameter any: Any instance to attempt to unwrap
89 /// - returns: Unwrapped Any as Optional<Any>
90 private func unwrap(any: Any) -> Any? {
91 let mi = Mirror(reflecting: any)
93 if mi.displayStyle != .optional { return any }
94 if mi.children.count == 0 { return nil }
96 let (_, some) = mi.children.first!