Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 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 | |
| 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 } |
| 29 | } |
| 30 | |
| 31 | public extension TStruct { |
| 32 | public static var fieldIds: [String: (id: Int32, type: TType)] { return [:] } |
| 33 | public static var thriftType: TType { return .struct } |
| 34 | |
| 35 | public func write(to proto: TProtocol) throws { |
| 36 | // Write struct name first |
| 37 | try proto.writeStructBegin(name: Self.structName) |
| 38 | |
| 39 | try self.forEach { name, value, id in |
| 40 | // Write to protocol |
| 41 | try proto.writeFieldValue(value, name: name, |
| 42 | type: value.thriftType, id: id) |
| 43 | } |
| 44 | try proto.writeFieldStop() |
| 45 | try proto.writeStructEnd() |
| 46 | } |
| 47 | |
| 48 | public var hashValue: Int { |
| 49 | let prime = 31 |
| 50 | var result = 1 |
| 51 | self.forEach { _, value, _ in |
| 52 | result = prime &* result &+ (value.hashValue) |
| 53 | } |
| 54 | return result |
| 55 | } |
| 56 | |
| 57 | /// Provides a block for handling each (available) thrift property using reflection |
| 58 | /// Caveat: Skips over optional values |
| 59 | |
| 60 | |
| 61 | /// Provides a block for handling each (available) thrift property using reflection |
| 62 | /// |
| 63 | /// - parameter block: block for handling property |
| 64 | /// |
| 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) |
| 69 | |
| 70 | // Iterate through all children, ignore empty property names |
| 71 | for (propName, propValue) in mirror.children { |
| 72 | guard let propName = propName else { continue } |
| 73 | |
| 74 | if let tval = unwrap(any: propValue) as? TSerializable, let id = Self.fieldIds[propName] { |
| 75 | try block(propName, tval, id) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 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 |
| 86 | /// |
| 87 | /// - parameter any: Any instance to attempt to unwrap |
| 88 | /// |
| 89 | /// - returns: Unwrapped Any as Optional<Any> |
| 90 | private func unwrap(any: Any) -> Any? { |
| 91 | let mi = Mirror(reflecting: any) |
| 92 | |
| 93 | if mi.displayStyle != .optional { return any } |
| 94 | if mi.children.count == 0 { return nil } |
| 95 | |
| 96 | let (_, some) = mi.children.first! |
| 97 | return some |
| 98 | } |
| 99 | } |
| 100 | |