1
#pragma once
2

            
3
#include "envoy/config/subscription.h"
4

            
5
#include "source/common/protobuf/utility.h"
6

            
7
namespace Envoy {
8
namespace Config {
9

            
10
template <typename Current> class OpaqueResourceDecoderImpl : public Config::OpaqueResourceDecoder {
11
public:
12
  OpaqueResourceDecoderImpl(ProtobufMessage::ValidationVisitor& validation_visitor,
13
                            absl::string_view name_field)
14
13668
      : validation_visitor_(validation_visitor), name_field_(name_field) {}
15

            
16
  // Config::OpaqueResourceDecoder
17
15174
  ProtobufTypes::MessagePtr decodeResource(const Protobuf::Any& resource) override {
18
15174
    auto typed_message = std::make_unique<Current>();
19
    // If the Any is a synthetic empty message (e.g. because the resource field was not set in
20
    // Resource, this might be empty, so we shouldn't decode.
21
15174
    if (!resource.type_url().empty()) {
22
15163
      MessageUtil::anyConvertAndValidate<Current>(resource, *typed_message, validation_visitor_);
23
15163
    }
24
15174
    return typed_message;
25
15174
  }
26

            
27
13243
  std::string resourceName(const Protobuf::Message& resource) override {
28
13243
    return MessageUtil::getStringField(resource, name_field_);
29
13243
  }
30

            
31
private:
32
  ProtobufMessage::ValidationVisitor& validation_visitor_;
33
  const std::string name_field_;
34
};
35

            
36
} // namespace Config
37
} // namespace Envoy