/* * 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. */ #pragma once #include #include #include #include #include #include #include #include #include namespace apache { namespace thrift { namespace compiler { class schematizer { public: using InternFunc = std::function val, t_program* program)>; explicit schematizer( const t_program_bundle* bundle, InternFunc intern_value = &default_intern_value) : bundle_(bundle), intern_value_(std::move(intern_value)) {} // Creates a constant of type schema.Struct describing the argument. // https://github.com/facebook/fbthrift/blob/main/thrift/lib/thrift/schema.thrift std::unique_ptr gen_schema(const t_structured& node); std::unique_ptr gen_schema(const t_service& node); std::unique_ptr gen_schema(const t_const& node); std::unique_ptr gen_schema(const t_enum& node); std::unique_ptr gen_schema(const t_program& node); std::unique_ptr gen_schema(const t_typedef& node); // Creates a constant of type schema.Schema describing the argument and all // types recursively referenced by it. Calls gen_schema internally. std::unique_ptr gen_full_schema(const t_service& node); private: const t_program_bundle* bundle_; InternFunc intern_value_; static t_program::value_id default_intern_value( std::unique_ptr val, t_program* program); t_type_ref stdType(std::string_view uri); std::unique_ptr typeUri(const t_type& type); void add_definition( t_const_value& schema, const t_named& node, const t_program* program, schematizer::InternFunc& intern_value); std::unique_ptr gen_type( schematizer* generator, const t_program* program, t_const_value* defns_schema, const t_type& type); std::unique_ptr gen_type( const t_type& type, const t_program* program) { return gen_type(nullptr, program, nullptr, type); } void add_fields( schematizer* generator, const t_program* program, t_const_value* defns_schema, t_const_value& schema, const std::string& fields_name, node_list_view fields, schematizer::InternFunc& intern_value); }; // Turns a t_const_value holding some value V into a t_const_value holding a // protocol.Value representing V. // Currently only uses bool/i64/double/string/list/map. // TODO: allow increasing type fidelity. std::unique_ptr wrap_with_protocol_value( const t_const_value& value, t_type_ref ttype); } // namespace compiler } // namespace thrift } // namespace apache