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
absl::StatusOr<std::pair<std::unique_ptr<Protobuf::Message>, absl::string_view>>
17
6
convertTypedStruct(const Protobuf::Message& message) {
18
6
  auto* typed_struct = Protobuf::DynamicCastMessage<T>(&message);
19
6
  auto inner_message = typeUrlToMessage(typed_struct->type_url());
20
6
  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
6
  if (inner_message != nullptr) {
23
4
#ifdef ENVOY_ENABLE_YAML
24
4
    MessageUtil::jsonConvert(typed_struct->value(), ProtobufMessage::getNullValidationVisitor(),
25
4
                             *inner_message);
26
#else
27
    return absl::InvalidArgumentError("JSON and YAML support compiled out.");
28
#endif
29
4
  }
30
6
  return std::make_pair(std::move(inner_message), target_type_url);
31
6
}
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