From 2cc05a67f69cab1e98c295ce23ae95592f167422 Mon Sep 17 00:00:00 2001
From: Euler Taveira <euler.taveira@enterprisedb.com>
Date: Mon, 16 Nov 2020 18:49:57 -0300
Subject: [PATCH 4/6] Simplify parse_output_parameters function

Instead of individual variables, pass PGOutputData to
parse_output_parameters. It is easier to read and maintain while
adding new options to pgoutput.
---
 src/backend/replication/pgoutput/pgoutput.c | 29 ++++++++-------------
 src/include/replication/pgoutput.h          |  1 +
 2 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c
index bd3c2a3b99..b25e67edcb 100644
--- a/src/backend/replication/pgoutput/pgoutput.c
+++ b/src/backend/replication/pgoutput/pgoutput.c
@@ -161,9 +161,7 @@ _PG_output_plugin_init(OutputPluginCallbacks *cb)
 }
 
 static void
-parse_output_parameters(List *options, uint32 *protocol_version,
-						List **publication_names, bool *binary,
-						bool *messages, bool *enable_streaming)
+parse_output_parameters(List *options, PGOutputData *data)
 {
 	ListCell   *lc;
 	bool		protocol_version_given = false;
@@ -172,8 +170,9 @@ parse_output_parameters(List *options, uint32 *protocol_version,
 	bool		messages_option_given = false;
 	bool		streaming_given = false;
 
-	*binary = false;
-	*messages = false;
+	data->binary = false;
+	data->messages = false;
+	data->streaming = false;
 
 	foreach(lc, options)
 	{
@@ -203,7 +202,7 @@ parse_output_parameters(List *options, uint32 *protocol_version,
 						 errmsg("proto_version \"%s\" out of range",
 								strVal(defel->arg))));
 
-			*protocol_version = (uint32) parsed;
+			data->protocol_version = (uint32) parsed;
 		}
 		else if (strcmp(defel->defname, "publication_names") == 0)
 		{
@@ -214,7 +213,7 @@ parse_output_parameters(List *options, uint32 *protocol_version,
 			publication_names_given = true;
 
 			if (!SplitIdentifierString(strVal(defel->arg), ',',
-									   publication_names))
+									   &data->publication_names))
 				ereport(ERROR,
 						(errcode(ERRCODE_INVALID_NAME),
 						 errmsg("invalid publication_names syntax")));
@@ -227,7 +226,7 @@ parse_output_parameters(List *options, uint32 *protocol_version,
 						 errmsg("conflicting or redundant options")));
 			binary_option_given = true;
 
-			*binary = defGetBoolean(defel);
+			data->binary = defGetBoolean(defel);
 		}
 		else if (strcmp(defel->defname, "messages") == 0)
 		{
@@ -237,7 +236,7 @@ parse_output_parameters(List *options, uint32 *protocol_version,
 						 errmsg("conflicting or redundant options")));
 			messages_option_given = true;
 
-			*messages = defGetBoolean(defel);
+			data->messages = defGetBoolean(defel);
 		}
 		else if (strcmp(defel->defname, "streaming") == 0)
 		{
@@ -247,7 +246,7 @@ parse_output_parameters(List *options, uint32 *protocol_version,
 						 errmsg("conflicting or redundant options")));
 			streaming_given = true;
 
-			*enable_streaming = defGetBoolean(defel);
+			data->streaming = defGetBoolean(defel);
 		}
 		else
 			elog(ERROR, "unrecognized pgoutput option: %s", defel->defname);
@@ -261,7 +260,6 @@ static void
 pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
 				 bool is_init)
 {
-	bool		enable_streaming = false;
 	PGOutputData *data = palloc0(sizeof(PGOutputData));
 
 	/* Create our memory context for private allocations. */
@@ -282,12 +280,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
 	if (!is_init)
 	{
 		/* Parse the params and ERROR if we see any we don't recognize */
-		parse_output_parameters(ctx->output_plugin_options,
-								&data->protocol_version,
-								&data->publication_names,
-								&data->binary,
-								&data->messages,
-								&enable_streaming);
+		parse_output_parameters(ctx->output_plugin_options, data);
 
 		/* Check if we support requested protocol */
 		if (data->protocol_version > LOGICALREP_PROTO_MAX_VERSION_NUM)
@@ -313,7 +306,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
 		 * we only allow it with sufficient version of the protocol, and when
 		 * the output plugin supports it.
 		 */
-		if (!enable_streaming)
+		if (!data->streaming)
 			ctx->streaming = false;
 		else if (data->protocol_version < LOGICALREP_PROTO_STREAM_VERSION_NUM)
 			ereport(ERROR,
diff --git a/src/include/replication/pgoutput.h b/src/include/replication/pgoutput.h
index 3b7273bd89..62a7d0a6d3 100644
--- a/src/include/replication/pgoutput.h
+++ b/src/include/replication/pgoutput.h
@@ -26,6 +26,7 @@ typedef struct PGOutputData
 	List	   *publications;
 	bool		binary;
 	bool		messages;
+	bool		streaming;
 } PGOutputData;
 
 #endif							/* PGOUTPUT_H */
-- 
2.20.1

