/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include "icsp/lib/Compression.h" namespace thrift::rust::thrift_any { template std::unique_ptr to_any(const std::string& compact_serialized_t) { // // deserialize T from compact // T t_obj = apache::thrift::CompactSerializer::deserialize(compact_serialized_t); // // convert Basic to Any // apache::thrift::type::AnyStruct any = apache::thrift::type::TypeRegistry::generated() .store(t_obj) .toThrift(); // // serialize Any to compact // std::string compact_any = apache::thrift::CompactSerializer::serialize(any); return std::make_unique(std::move(compact_any)); } template std::unique_ptr from_any(const std::string& any) { // // deserialize Any from compact // const apache::thrift::type::AnyStruct any_obj = apache::thrift::CompactSerializer::deserialize< apache::thrift::type::AnyStruct>(any); apache::thrift::type::AnyData anydata(any_obj); // // convert Any to T // T t_obj; apache::thrift::type::TypeRegistry::generated().load(anydata, t_obj); // // serialize T to compact // std::string compact_t = apache::thrift::CompactSerializer::serialize(t_obj); return std::make_unique(std::move(compact_t)); } std::unique_ptr basic_to_any(const std::string& basic) { return to_any(basic); } std::unique_ptr any_to_basic(const std::string& any) { return from_any(any); } std::unique_ptr simple_union_to_any( const std::string& simple_union) { return to_any(simple_union); } std::unique_ptr any_to_simple_union(const std::string& any) { return from_any(any); } std::unique_ptr compress_any(const std::string& any) { const apache::thrift::type::AnyStruct any_obj = apache::thrift::CompactSerializer::deserialize< apache::thrift::type::AnyStruct>(any); apache::thrift::type::AnyData anydata(any_obj); anydata = facebook::icsp::compressAny(anydata); std::string compact_t = apache::thrift::CompactSerializer::serialize( anydata.toThrift()); return std::make_unique(std::move(compact_t)); } std::unique_ptr decompress_any(const std::string& any) { const apache::thrift::type::AnyStruct any_obj = apache::thrift::CompactSerializer::deserialize< apache::thrift::type::AnyStruct>(any); apache::thrift::type::AnyData anydata(any_obj); anydata = facebook::icsp::decompressAny(anydata); std::string compact_t = apache::thrift::CompactSerializer::serialize( anydata.toThrift()); return std::make_unique(std::move(compact_t)); } } // namespace thrift::rust::thrift_any