Line data Source code
1 : #pragma once 2 : 3 : #include <vector> 4 : 5 : #include "source/common/protobuf/message_validator_impl.h" 6 : #include "source/common/protobuf/protobuf.h" 7 : #include "source/common/protobuf/utility.h" 8 : 9 : namespace Envoy { 10 : namespace ProtobufMessage { 11 : namespace Helper { 12 : 13 : std::unique_ptr<Protobuf::Message> typeUrlToMessage(absl::string_view type_url); 14 : 15 : template <typename T> 16 : std::pair<std::unique_ptr<Protobuf::Message>, absl::string_view> 17 0 : convertTypedStruct(const Protobuf::Message& message) { 18 0 : auto* typed_struct = Protobuf::DynamicCastToGenerated<T>(&message); 19 0 : auto inner_message = typeUrlToMessage(typed_struct->type_url()); 20 0 : absl::string_view target_type_url = typed_struct->type_url(); 21 : // inner_message might be invalid as we did not previously check type_url during loading. 22 0 : if (inner_message != nullptr) { 23 0 : #ifdef ENVOY_ENABLE_YAML 24 0 : MessageUtil::jsonConvert(typed_struct->value(), ProtobufMessage::getNullValidationVisitor(), 25 0 : *inner_message); 26 : #else 27 : throwEnvoyExceptionOrPanic("JSON and YAML support compiled out."); 28 : #endif 29 0 : } 30 0 : return {std::move(inner_message), target_type_url}; 31 0 : } 32 : 33 : /** 34 : * RAII wrapper that push message to parents on construction and pop it on destruction. 35 : */ 36 : struct ScopedMessageParents { 37 : ScopedMessageParents(std::vector<const Protobuf::Message*>& parents, 38 : const Protobuf::Message& message); 39 : 40 : ~ScopedMessageParents(); 41 : 42 : private: 43 : std::vector<const Protobuf::Message*>& parents_; 44 : }; 45 : 46 : } // namespace Helper 47 : } // namespace ProtobufMessage 48 : } // namespace Envoy