|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +{{! Select all the comment items and expand them here }} |
| 16 | +{{#.}} |
| 17 | +{{#comment}} |
| 18 | +{{comment}} |
| 19 | +{{/comment}} |
| 20 | +{{/.}} |
| 21 | + |
| 22 | +#include "github/presto-trunk/presto-native-execution/presto_cpp/main/thrift/ProtocolToThrift.h" |
| 23 | + |
| 24 | +namespace facebook::presto::thrift { |
| 25 | +
|
| 26 | +// These could be covered by a more general template but this way only |
| 27 | +// conversions to supported Thrift data types can be generated. |
| 28 | +void toThrift(const std::string& proto, std::string& thrift) { |
| 29 | + thrift = proto; |
| 30 | +} |
| 31 | +void toThrift(const bool& proto, bool& thrift) { |
| 32 | + thrift = proto; |
| 33 | +} |
| 34 | +void toThrift(const int16_t& proto, int16_t& thrift) { |
| 35 | + thrift = proto; |
| 36 | +} |
| 37 | +void toThrift(const int32_t& proto, int32_t& thrift) { |
| 38 | + thrift = proto; |
| 39 | +} |
| 40 | +void toThrift(const int64_t& proto, int64_t& thrift) { |
| 41 | + thrift = proto; |
| 42 | +} |
| 43 | +void toThrift(const double& proto, double& thrift) { |
| 44 | + thrift = proto; |
| 45 | +} |
| 46 | +void toThrift(const facebook::presto::protocol::Duration& duration, double& thrift) { |
| 47 | + thrift = duration.getValue(facebook::presto::protocol::TimeUnit::MILLISECONDS); |
| 48 | +} |
| 49 | +void toThrift(const facebook::presto::protocol::DataSize& dataSize, double& thrift) { |
| 50 | + thrift = dataSize.getValue(facebook::presto::protocol::DataUnit::BYTE); |
| 51 | +} |
| 52 | + |
| 53 | +template <typename P, typename T> |
| 54 | +void toThrift(const std::shared_ptr<P>& proto, std::shared_ptr<T>& thrift) { |
| 55 | + if (proto) { |
| 56 | + thrift = std::make_shared<T>(); |
| 57 | + toThrift(*proto, *thrift); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +template <typename P, typename T> |
| 62 | +void toThrift(const std::shared_ptr<P>& proto, T& thrift) { |
| 63 | + if (proto) { |
| 64 | + toThrift(*proto, thrift); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +template <typename V, typename S> |
| 69 | +void toThrift(const std::vector<V>& v, std::set<S>& s) { |
| 70 | + S toItem; |
| 71 | + for (const auto& fromItem : v) { |
| 72 | + toThrift(fromItem, toItem); |
| 73 | + s.insert(std::move(toItem)); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +template <typename P, typename T> |
| 78 | +void toThrift(const std::vector<P>& p, std::vector<T>& t) { |
| 79 | + t.reserve(p.size()); |
| 80 | + T toItem; |
| 81 | + for (const auto& fromItem : p) { |
| 82 | + toThrift(fromItem, toItem); |
| 83 | + t.emplace_back(std::move(toItem)); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +template <typename K1, typename V1, typename K2, typename V2> |
| 88 | +void toThrift(const std::map<K1, V1>& protoMap, std::map<K2, V2>& thriftMap) { |
| 89 | + K2 toKey; |
| 90 | + V2 toValue; |
| 91 | + for (const auto& [fromKey, fromValue] : protoMap) { |
| 92 | + toThrift(fromKey, toKey); |
| 93 | + toThrift(fromValue, toValue); |
| 94 | + thriftMap.emplace(std::move(toKey), std::move(toValue)); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +template <typename P, typename T> |
| 99 | +void toThrift(const std::shared_ptr<P>& proto, apache::thrift::optional_field_ref<T> thrift) { |
| 100 | + if (proto) { |
| 101 | + thrift.ensure(); |
| 102 | + toThrift(*proto, apache::thrift::can_throw(*thrift)); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +void fromThrift(const std::string& thrift, std::string& proto) { |
| 107 | + proto = thrift; |
| 108 | +} |
| 109 | +void fromThrift(const bool& thrift, bool& proto) { |
| 110 | + proto = thrift; |
| 111 | +} |
| 112 | +void fromThrift(const int32_t& thrift, int32_t& proto) { |
| 113 | + proto = thrift; |
| 114 | +} |
| 115 | +void fromThrift(const int64_t& thrift, int64_t& proto) { |
| 116 | + proto = thrift; |
| 117 | +} |
| 118 | +void fromThrift(const double& thrift, double& proto) { |
| 119 | + proto = thrift; |
| 120 | +} |
| 121 | + |
| 122 | +void fromThrift(const double& thrift, facebook::presto::protocol::Duration& duration) { |
| 123 | + duration = facebook::presto::protocol::Duration(thrift, facebook::presto::protocol::TimeUnit::MILLISECONDS); |
| 124 | +} |
| 125 | + |
| 126 | +void fromThrift(const double& thrift, facebook::presto::protocol::DataSize& dataSize) { |
| 127 | + dataSize = facebook::presto::protocol::DataSize(thrift, facebook::presto::protocol::DataUnit::BYTE); |
| 128 | +} |
| 129 | + |
| 130 | +template <typename P, typename T> |
| 131 | +void fromThrift(const apache::thrift::optional_field_ref<T>& thrift, std::shared_ptr<P>& proto) { |
| 132 | + if (thrift.has_value()) { |
| 133 | + proto = std::make_shared<P>(); |
| 134 | + fromThrift(*thrift, *proto); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +template <typename P, typename T> |
| 139 | +void fromThrift(const T& thrift, std::shared_ptr<P>& proto) { |
| 140 | + proto = std::make_shared<P>(); |
| 141 | + fromThrift(thrift, *proto); |
| 142 | +} |
| 143 | + |
| 144 | +template <typename P, typename T> |
| 145 | +void fromThrift(const std::shared_ptr<P>& thrift, std::shared_ptr<T>& proto) { |
| 146 | + if (thrift) { |
| 147 | + proto = std::make_shared<T>(); |
| 148 | + fromThrift(*thrift, *proto); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +template <typename V, typename S> |
| 153 | +void fromThrift(const std::set<S>& thrift, std::vector<V>& proto) { |
| 154 | + proto.reserve(thrift.size()); |
| 155 | + V toItem; |
| 156 | + for (const auto& fromItem : thrift) { |
| 157 | + fromThrift(fromItem, toItem); |
| 158 | + proto.emplace_back(std::move(toItem)); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +template <typename P, typename T> |
| 163 | +void fromThrift(const std::vector<P>& thrift, std::vector<T>& proto) { |
| 164 | + proto.reserve(thrift.size()); |
| 165 | + T toItem; |
| 166 | + for (const auto& fromItem : thrift) { |
| 167 | + fromThrift(fromItem, toItem); |
| 168 | + proto.emplace_back(std::move(toItem)); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +template <typename K1, typename V1, typename K2, typename V2> |
| 173 | +void fromThrift(const std::map<K1, V1>& thriftMap, std::map<K2, V2>& protoMap) { |
| 174 | + K2 toKey; |
| 175 | + V2 toValue; |
| 176 | + for (const auto& [fromKey, fromValue] : thriftMap) { |
| 177 | + fromThrift(fromKey, toKey); |
| 178 | + fromThrift(fromValue, toValue); |
| 179 | + protoMap.emplace(std::move(toKey), std::move(toValue)); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +{{! Select all the items and expand either the "hinc" member or the "struct", "enum" members }} |
| 184 | +{{#.}} |
| 185 | +{{#cinc}} |
| 186 | +{{&cinc}} |
| 187 | +{{/cinc}} |
| 188 | +{{^cinc}} |
| 189 | +{{#struct}} |
| 190 | +void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{&class_name}}& thrift) { |
| 191 | + {{#fields}} |
| 192 | + toThrift(proto.{{proto_name}}, {{^optional}}*{{/optional}}thrift.{{field_name}}_ref()); |
| 193 | + {{/fields}} |
| 194 | +} |
| 195 | +void fromThrift(const {{&class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) { |
| 196 | + {{#fields}} |
| 197 | + fromThrift({{^optional}}*{{/optional}}thrift.{{field_name}}_ref(), proto.{{proto_name}}); |
| 198 | + {{/fields}} |
| 199 | +} |
| 200 | + |
| 201 | +{{/struct}} |
| 202 | +{{#wrapper}} |
| 203 | +void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift) { |
| 204 | + {{#fields}} |
| 205 | + toThrift(proto, {{^optional}}*{{/optional}}thrift.{{field_name}}_ref()); |
| 206 | + {{/fields}} |
| 207 | +} |
| 208 | +void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) { |
| 209 | + {{#fields}} |
| 210 | + fromThrift({{^optional}}*{{/optional}}thrift.{{field_name}}_ref(), proto); |
| 211 | + {{/fields}} |
| 212 | +} |
| 213 | +{{/wrapper}} |
| 214 | +{{#enum}} |
| 215 | +void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift) { |
| 216 | + thrift = ({{class_name}})(static_cast<int>(proto)); |
| 217 | +} |
| 218 | +void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) { |
| 219 | + proto = (facebook::presto::protocol::{{class_name}})(static_cast<int>(thrift)); |
| 220 | +} |
| 221 | + |
| 222 | +{{/enum}} |
| 223 | +{{/cinc}} |
| 224 | +{{/.}} |
| 225 | + |
| 226 | +} |
| 227 | + |
0 commit comments