Repository: envoyproxy/envoy
File: source/common/router/config_impl.cc (C++)

Code before:
      append_xfh_(route.route().append_x_forwarded_host()),
      using_new_timeouts_(route.route().has_max_stream_duration()),
      match_grpc_(route.match().has_grpc()),
      case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)) {

  auto config_or_error =
      PerFilterConfigs::create(route.typed_per_filter_config(), factory_context, validator);
  SET_AND_RETURN_IF_NOT_OK(config_or_error.status(), creation_status);
  per_filter_configs_ = std::move(config_or_error.value());

  auto policy_or_error =
      buildRetryPolicy(vhost->retryPolicy(), route.route(), validator, factory_context);
  SET_AND_RETURN_IF_NOT_OK(policy_or_error.status(), creation_status);
  retry_policy_ = policy_or_error.value() != nullptr ? std::move(policy_or_error.value())
                                                     : RetryPolicyImpl::DefaultRetryPolicy;

  if (route.has_direct_response() && route.direct_response().has_body()) {
    auto provider_or_error = Envoy::Config::DataSource::DataSourceProvider::create(
        route.direct_response().body(), factory_context.mainThreadDispatcher(),
        factory_context.threadLocal(), factory_context.api(), true,
        vhost_->globalRouteConfig().maxDirectResponseBodySizeBytes());
    SET_AND_RETURN_IF_NOT_OK(provider_or_error.status(), creation_status);
    direct_response_body_provider_ = std::move(provider_or_error.value());
  }

  if (route.has_direct_response() && route.direct_response().has_body_format()) {
    Server::GenericFactoryContextImpl generic_context(factory_context,
                                                      factory_context.messageValidationVisitor());
    auto formatter_or_error = Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
        route.direct_response().body_format(), generic_context);
    SET_AND_RETURN_IF_NOT_OK(formatter_or_error.status(), creation_status);
    direct_response_body_formatter_ = std::move(formatter_or_error.value());
  }

  if (!route.request_headers_to_add().empty() || !route.request_headers_to_remove().empty()) {
    auto parser_or_error =
        HeaderParser::configure(route.request_headers_to_add(), route.request_headers_to_remove());
    SET_AND_RETURN_IF_NOT_OK(parser_or_error.status(), creation_status);
    request_headers_parser_ = std::move(parser_or_error.value());
  }
  if (!route.response_headers_to_add().empty() || !route.response_headers_to_remove().empty()) {
    auto parser_or_error = HeaderParser::configure(route.response_headers_to_add(),
                                                   route.response_headers_to_remove());
    SET_AND_RETURN_IF_NOT_OK(parser_or_error.status(), creation_status);
    response_headers_parser_ = std::move(parser_or_error.value());
  }
  if (route.has_metadata()) {
    metadata_ = std::make_unique<RouteMetadataPack>(route.metadata());
  }
  if (route.route().has_metadata_match()) {
    const auto filter_it = route.route().metadata_match().filter_metadata().find(

Review comment (suggestion):
FYI in the C++ implementation of protobuf you don't have to check two things for a layered presence check, you can just check `route.direct_response().has_body_format()`, because direct_response() will return a reference to the default uninitialized message, and that won't have a body_format, so the result will be the same.

Code after:
      append_xfh_(route.route().append_x_forwarded_host()),
      using_new_timeouts_(route.route().has_max_stream_duration()),
      match_grpc_(route.match().has_grpc()),
      case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)) {

  auto config_or_error =
      PerFilterConfigs::create(route.typed_per_filter_config(), factory_context, validator);
  SET_AND_RETURN_IF_NOT_OK(config_or_error.status(), creation_status);
  per_filter_configs_ = std::move(config_or_error.value());

  auto policy_or_error =
      buildRetryPolicy(vhost->retryPolicy(), route.route(), validator, factory_context);
  SET_AND_RETURN_IF_NOT_OK(policy_or_error.status(), creation_status);
  retry_policy_ = policy_or_error.value() != nullptr ? std::move(policy_or_error.value())
                                                     : RetryPolicyImpl::DefaultRetryPolicy;

  if (route.has_direct_response() && route.direct_response().has_body()) {
    auto provider_or_error = Envoy::Config::DataSource::DataSourceProvider::create(
        route.direct_response().body(), factory_context.mainThreadDispatcher(),
        factory_context.threadLocal(), factory_context.api(), true,
        vhost_->globalRouteConfig().maxDirectResponseBodySizeBytes());
    SET_AND_RETURN_IF_NOT_OK(provider_or_error.status(), creation_status);
    direct_response_body_provider_ = std::move(provider_or_error.value());
  }

  if (route.direct_response().has_body_format()) {
    Server::GenericFactoryContextImpl generic_context(factory_context,
                                                      factory_context.messageValidationVisitor());
    auto formatter_or_error = Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
        route.direct_response().body_format(), generic_context);
    SET_AND_RETURN_IF_NOT_OK(formatter_or_error.status(), creation_status);
    direct_response_body_formatter_ = std::move(formatter_or_error.value());
  }

  if (!route.request_headers_to_add().empty() || !route.request_headers_to_remove().empty()) {
    auto parser_or_error =
        HeaderParser::configure(route.request_headers_to_add(), route.request_headers_to_remove());
    SET_AND_RETURN_IF_NOT_OK(parser_or_error.status(), creation_status);
    request_headers_parser_ = std::move(parser_or_error.value());
  }
  if (!route.response_headers_to_add().empty() || !route.response_headers_to_remove().empty()) {
    auto parser_or_error = HeaderParser::configure(route.response_headers_to_add(),
                                                   route.response_headers_to_remove());
    SET_AND_RETURN_IF_NOT_OK(parser_or_error.status(), creation_status);
    response_headers_parser_ = std::move(parser_or_error.value());
  }
  if (route.has_metadata()) {
    metadata_ = std::make_unique<RouteMetadataPack>(route.metadata());
  }
  if (route.route().has_metadata_match()) {
    const auto filter_it = route.route().metadata_match().filter_metadata().find(