/* * 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 #include using namespace apache::thrift; using namespace static_reflection::demo; struct get_property { template using apply = typename Member::annotations::values::property; }; struct legacy_to_flat_translator { template void operator()( fatal::tag, const std::string& from, flat_config& to) const { auto& value = typename Member::getter{}(to); value = folly::to(from); } }; void translate(const legacy_config& from, flat_config& to) { for (const auto& i : from) { fatal::trie_find::members, get_property>( i.first.begin(), i.first.end(), legacy_to_flat_translator(), i.second, to); } } struct flat_to_legacy_translator { template void operator()( fatal::indexed, const flat_config& from, legacy_config& to) { using property = typename Member::annotations::values::property; const auto key = fatal::z_data(); const auto& value = typename Member::getter{}(from); to[key] = folly::to(value); } }; void translate(const flat_config& from, legacy_config& to) { using members = reflect_struct::members; fatal::foreach(flat_to_legacy_translator(), from, to); } template struct get_type_class_; template <> struct get_type_class_ { using type = type_class::structure; }; template <> struct get_type_class_ { using type = type_class::map; }; template using get_type_class = typename get_type_class_::type; template void test(const From& from) { To to; translate(from, to); print_as(get_type_class{}, from); print_as(get_type_class{}, to); } int main(int argc, char** argv) { folly::init(&argc, &argv); std::cerr << "legacy -> flat: "; test( static_reflection::demo::legacy_config_constants::example()); std::cerr << "flat -> legacy: "; test( static_reflection::demo::flat_config_constants::example()); return 0; }