diff --git a/source/common/protobuf/utility.h b/source/common/protobuf/utility.h index 2d427e7d9e7ad..b14e3720b6b09 100644 --- a/source/common/protobuf/utility.h +++ b/source/common/protobuf/utility.h @@ -255,7 +255,7 @@ class MessageUtil { static std::size_t hash(const Protobuf::Message& message); #ifdef ENVOY_ENABLE_YAML - static void loadFromJson(const std::string& json, Protobuf::Message& message, + static void loadFromJson(absl::string_view json, Protobuf::Message& message, ProtobufMessage::ValidationVisitor& validation_visitor); /** * Return ok only when strict conversion(don't ignore unknown field) succeeds. @@ -264,9 +264,9 @@ class MessageUtil { * Return error status for relaxed conversion and set has_unknown_field to false if relaxed * conversion(ignore unknown field) fails. */ - static absl::Status loadFromJsonNoThrow(const std::string& json, Protobuf::Message& message, + static absl::Status loadFromJsonNoThrow(absl::string_view json, Protobuf::Message& message, bool& has_unknown_fileld); - static void loadFromJson(const std::string& json, ProtobufWkt::Struct& message); + static void loadFromJson(absl::string_view json, ProtobufWkt::Struct& message); static void loadFromYaml(const std::string& yaml, Protobuf::Message& message, ProtobufMessage::ValidationVisitor& validation_visitor); #endif diff --git a/source/common/protobuf/yaml_utility.cc b/source/common/protobuf/yaml_utility.cc index 4409abc33d649..741817bc04315 100644 --- a/source/common/protobuf/yaml_utility.cc +++ b/source/common/protobuf/yaml_utility.cc @@ -115,7 +115,7 @@ void jsonConvertInternal(const Protobuf::Message& source, } // namespace -void MessageUtil::loadFromJson(const std::string& json, Protobuf::Message& message, +void MessageUtil::loadFromJson(absl::string_view json, Protobuf::Message& message, ProtobufMessage::ValidationVisitor& validation_visitor) { bool has_unknown_field; auto load_status = loadFromJsonNoThrow(json, message, has_unknown_field); @@ -124,15 +124,16 @@ void MessageUtil::loadFromJson(const std::string& json, Protobuf::Message& messa } if (has_unknown_field) { // If the parsing failure is caused by the unknown fields. - THROW_IF_NOT_OK(validation_visitor.onUnknownField("type " + message.GetTypeName() + " reason " + - load_status.ToString())); + THROW_IF_NOT_OK(validation_visitor.onUnknownField( + fmt::format("type {} reason {}", message.GetTypeName(), load_status.ToString()))); } else { // If the error has nothing to do with unknown field. - throw EnvoyException("Unable to parse JSON as proto (" + load_status.ToString() + "): " + json); + throw EnvoyException( + fmt::format("Unable to parse JSON as proto ({}): {}", load_status.ToString(), json)); } } -absl::Status MessageUtil::loadFromJsonNoThrow(const std::string& json, Protobuf::Message& message, +absl::Status MessageUtil::loadFromJsonNoThrow(absl::string_view json, Protobuf::Message& message, bool& has_unknown_fileld) { has_unknown_fileld = false; Protobuf::util::JsonParseOptions options; @@ -163,7 +164,7 @@ absl::Status MessageUtil::loadFromJsonNoThrow(const std::string& json, Protobuf: return relaxed_status; } -void MessageUtil::loadFromJson(const std::string& json, ProtobufWkt::Struct& message) { +void MessageUtil::loadFromJson(absl::string_view json, ProtobufWkt::Struct& message) { // No need to validate if converting to a Struct, since there are no unknown // fields possible. loadFromJson(json, message, ProtobufMessage::getNullValidationVisitor());