git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 1/2] protocol: add protocol version formatting function

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Aug 4, 2021, 23:32 UTC
Message-ID
<87czqsn5k9.fsf@evledraar.gmail.com>
In-Reply-To
<63f98062788a7c5d44fafecfde78077bdce2af73.1628115065.git.steadmon@google.com>
On Wed, Aug 04 2021, Josh Steadmon wrote:
Show 21 quoted lines
> Add a function to get human-readable names for the various wire protocol
> versions.
>
> Signed-off-by: Josh Steadmon <steadmon@google.com>
> ---
>  protocol.c | 14 ++++++++++++++
>  protocol.h |  6 ++++++
>  2 files changed, 20 insertions(+)
>
> diff --git a/protocol.c b/protocol.c
> index 052d7edbb9..7ec7ce896e 100644
> --- a/protocol.c
> +++ b/protocol.c
> @@ -14,6 +14,20 @@ static enum protocol_version parse_protocol_version(const char *value)
>  		return protocol_unknown_version;
>  }
>  
> +const char *format_protocol_version(enum protocol_version version)
> +{
> +	switch (version) {
> +		case protocol_v0:
Don't indent "case" one more than "switch".

(Looking at CodingGuidelines we only have that advice for *.sh, but we follow it for C too).

Show 7 quoted lines
> +			return "0";
> +		case protocol_v1:
> +			return "1";
> +		case protocol_v2:
> +			return "2";
> +		default:
> +			return "UNKNOWN_VERSION";

Using the "default" case like that is an anti-pattern, i.e. you enumerted all arms except protocol_unknown_version, which is implicitly covered by your "default" case.

Just list it, and don't have a "default" case, then the compiler will complain if we ever add a new case that's not covered.

As an aside, and not strictly needed here: More generally between this and parse_protocol_version() it seems like this would benefit from just declaring the bidirectional mapping beween the enum fields and string values, and then have this and that function use that. See e.g. object_type_strings and associated functions in object.c for another enum that has such a bidirectional lookup.

Previous: Josh SteadmonNext: Josh Steadmon
Message 7 of 16 in “connect, protocol: log negotiated protocol version”
  1. connect, protocol: log negotiated protocol versionJosh Steadmon, Aug 3, 2021
  2. Taylor BlauAug 3, 2021
  3. Josh SteadmonAug 4, 2021
  4. Junio C HamanoAug 4, 2021
  5. 0/2 log negotiated protocol version.Josh Steadmon, Aug 4, 2021
  6. 1/2 protocol: add protocol version formatting functionJosh Steadmon, Aug 4, 2021
  7. Ævar Arnfjörð BjarmasonAug 4, 2021
  8. 2/2 connect, protocol: log negotiated protocol versionJosh Steadmon, Aug 4, 2021
  9. Eric SunshineAug 4, 2021
  10. Josh SteadmonAug 6, 2021
  11. Ævar Arnfjörð BjarmasonAug 4, 2021
  12. Taylor BlauAug 5, 2021
  13. Ævar Arnfjörð BjarmasonAug 5, 2021
  14. Josh SteadmonAug 6, 2021
  15. connect, protocol: log negotiated protocol versionJosh Steadmon, Aug 10, 2021
  16. Taylor BlauAug 16, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.