66#include " envoy/access_log/access_log.h"
77#include " envoy/common/pure.h"
88#include " envoy/config/typed_config.h"
9+ #include " envoy/formatter/http_formatter_context.h"
910#include " envoy/registry/registry.h"
1011#include " envoy/server/factory_context.h"
1112#include " envoy/stream_info/stream_info.h"
@@ -14,31 +15,31 @@ namespace Envoy {
1415namespace Formatter {
1516
1617/* *
17- * Template interface for multiple protocols/modules formatters.
18+ * Interface for multiple protocols/modules formatters.
1819 */
19- template < class FormatterContext > class FormatterBase {
20+ class Formatter {
2021public:
21- virtual ~FormatterBase () = default ;
22+ virtual ~Formatter () = default ;
2223
2324 /* *
2425 * Return a formatted substitution line.
2526 * @param context supplies the formatter context.
2627 * @param stream_info supplies the stream info.
2728 * @return std::string string containing the complete formatted substitution line.
2829 */
29- virtual std::string formatWithContext (const FormatterContext & context,
30+ virtual std::string formatWithContext (const Context & context,
3031 const StreamInfo::StreamInfo& stream_info) const PURE;
3132};
3233
33- template < class FormatterContext >
34- using FormatterBasePtr = std::unique_ptr<FormatterBase<FormatterContext> >;
34+ using FormatterPtr = std::unique_ptr<Formatter>;
35+ using FormatterConstSharedPtr = std::shared_ptr< const Formatter >;
3536
3637/* *
37- * Template interface for multiple protocols/modules formatter providers.
38+ * Interface for multiple protocols/modules formatter providers.
3839 */
39- template < class FormatterContext > class FormatterProviderBase {
40+ class FormatterProvider {
4041public:
41- virtual ~FormatterProviderBase () = default ;
42+ virtual ~FormatterProvider () = default ;
4243
4344 /* *
4445 * Format the value with the given context and stream info.
@@ -48,8 +49,7 @@ template <class FormatterContext> class FormatterProviderBase {
4849 * the given context and stream info.
4950 */
5051 virtual absl::optional<std::string>
51- formatWithContext (const FormatterContext& context,
52- const StreamInfo::StreamInfo& stream_info) const PURE;
52+ formatWithContext (const Context& context, const StreamInfo::StreamInfo& stream_info) const PURE;
5353
5454 /* *
5555 * Format the value with the given context and stream info.
@@ -59,16 +59,15 @@ template <class FormatterContext> class FormatterProviderBase {
5959 * context and stream info.
6060 */
6161 virtual ProtobufWkt::Value
62- formatValueWithContext (const FormatterContext & context,
62+ formatValueWithContext (const Context & context,
6363 const StreamInfo::StreamInfo& stream_info) const PURE;
6464};
6565
66- template <class FormatterContext >
67- using FormatterProviderBasePtr = std::unique_ptr<FormatterProviderBase<FormatterContext>>;
66+ using FormatterProviderPtr = std::unique_ptr<FormatterProvider>;
6867
69- template < class FormatterContext > class CommandParserBase {
68+ class CommandParser {
7069public:
71- virtual ~CommandParserBase () = default ;
70+ virtual ~CommandParser () = default ;
7271
7372 /* *
7473 * Return a FormatterProviderBasePtr if command arg and max_length are correct for the formatter
@@ -80,15 +79,13 @@ template <class FormatterContext> class CommandParserBase {
8079 *
8180 * @return FormattterProviderPtr substitution provider for the parsed command.
8281 */
83- virtual FormatterProviderBasePtr<FormatterContext>
84- parse (absl::string_view command, absl::string_view command_arg,
85- absl::optional<size_t > max_length) const PURE;
82+ virtual FormatterProviderPtr parse (absl::string_view command, absl::string_view command_arg,
83+ absl::optional<size_t > max_length) const PURE;
8684};
8785
88- template <class FormatterContext >
89- using CommandParserBasePtr = std::unique_ptr<CommandParserBase<FormatterContext>>;
86+ using CommandParserPtr = std::unique_ptr<CommandParser>;
9087
91- template < class FormatterContext > class CommandParserFactoryBase : public Config ::TypedFactory {
88+ class CommandParserFactory : public Config ::TypedFactory {
9289public:
9390 /* *
9491 * Creates a particular CommandParser implementation.
@@ -98,40 +95,30 @@ template <class FormatterContext> class CommandParserFactoryBase : public Config
9895 * @return CommandParserPtr the CommandParser which will be used in
9996 * SubstitutionFormatParser::parse() when evaluating an access log format string.
10097 */
101- virtual CommandParserBasePtr<FormatterContext>
98+ virtual CommandParserPtr
10299 createCommandParserFromProto (const Protobuf::Message& config,
103100 Server::Configuration::GenericFactoryContext& context) PURE;
104101
105- std::string category () const override {
106- static constexpr absl::string_view HttpContextCategory = " http" ;
107- if constexpr (FormatterContext::category () == HttpContextCategory) {
108- return " envoy.formatter" ; // Backward compatibility for HTTP.
109- } else {
110- return fmt::format (" envoy.formatters.{}" , FormatterContext::category ());
111- }
112- }
102+ std::string category () const override { return " envoy.formatter" ; }
113103};
114104
115- template <class FormatterContext >
116- class BuiltInCommandParserFactoryBase : public Config ::UntypedFactory {
105+ class BuiltInCommandParserFactory : public Config ::UntypedFactory {
117106public:
118- std::string category () const override {
119- return fmt::format (" envoy.built_in_formatters.{}" , FormatterContext::category ());
120- }
107+ std::string category () const override { return " envoy.built_in_formatters" ; }
121108
122109 /* *
123110 * Creates a particular CommandParser implementation.
124111 */
125- virtual CommandParserBasePtr<FormatterContext> createCommandParser () const PURE;
112+ virtual CommandParserPtr createCommandParser () const PURE;
126113};
127114
128115/* *
129116 * Helper class to get all built-in command parsers for a given formatter context.
130117 */
131- template < class FormatterContext > class BuiltInCommandParserFactoryHelper {
118+ class BuiltInCommandParserFactoryHelper {
132119public:
133- using Factory = BuiltInCommandParserFactoryBase<FormatterContext> ;
134- using Parsers = std::vector<CommandParserBasePtr<FormatterContext> >;
120+ using Factory = BuiltInCommandParserFactory ;
121+ using Parsers = std::vector<CommandParserPtr >;
135122
136123 /* *
137124 * Get all built-in command parsers for a given formatter context.
@@ -153,46 +140,5 @@ template <class FormatterContext> class BuiltInCommandParserFactoryHelper {
153140 }
154141};
155142
156- /* *
157- * Type placeholder for formatter providers that only require StreamInfo.
158- */
159- struct StreamInfoOnlyFormatterContext {
160- static constexpr absl::string_view category () { return " stream_info" ; }
161- };
162-
163- /* *
164- * Template specialization for formatter providers that only require StreamInfo.
165- * The only difference from the main template is the providers inheriting from this class
166- * will only take StreamInfo as input parameter.
167- */
168- template <> class FormatterProviderBase <StreamInfoOnlyFormatterContext> {
169- public:
170- virtual ~FormatterProviderBase () = default ;
171-
172- /* *
173- * Format the value with the given stream info.
174- * @param stream_info supplies the stream info.
175- * @return absl::optional<std::string> optional string containing a single value extracted from
176- * the given stream info.
177- */
178- virtual absl::optional<std::string> format (const StreamInfo::StreamInfo& stream_info) const PURE;
179-
180- /* *
181- * Format the value with the given stream info.
182- * @param stream_info supplies the stream info.
183- * @return ProtobufWkt::Value containing a single value extracted from the given stream info.
184- */
185- virtual ProtobufWkt::Value formatValue (const StreamInfo::StreamInfo& stream_info) const PURE;
186- };
187-
188- using StreamInfoFormatterProvider = FormatterProviderBase<StreamInfoOnlyFormatterContext>;
189- using StreamInfoFormatterProviderPtr = FormatterProviderBasePtr<StreamInfoOnlyFormatterContext>;
190- using StreamInfoCommandParser = CommandParserBase<StreamInfoOnlyFormatterContext>;
191- using StreamInfoCommandParserPtr = CommandParserBasePtr<StreamInfoOnlyFormatterContext>;
192- using BuiltInStreamInfoCommandParserFactory =
193- BuiltInCommandParserFactoryBase<StreamInfoOnlyFormatterContext>;
194- using BuiltInStreamInfoCommandParserFactoryHelper =
195- BuiltInCommandParserFactoryHelper<StreamInfoOnlyFormatterContext>;
196-
197143} // namespace Formatter
198144} // namespace Envoy
0 commit comments