Skip to content

Commit 76fc767

Browse files
committed
config: restructure format_config()
The recent changes have replaced the bodies of most if/else-if cases with simple helper method calls. This makes it easy to adapt the structure into a clearer switch statement, leaving a simple if/else in the default case. Make things a little simpler to read by reducing the nesting depth via a new goto statement when we want to skip values. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 078065c commit 76fc767

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

builtin/config.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct config_display_options {
124124
.key_delim = ' ', \
125125
}
126126

127+
#define TYPE_NONE 0
127128
#define TYPE_BOOL 1
128129
#define TYPE_INT 2
129130
#define TYPE_BOOL_OR_INT 3
@@ -390,32 +391,57 @@ static int format_config(const struct config_display_options *opts,
390391
show_config_origin(opts, kvi, buf);
391392
if (opts->show_keys)
392393
strbuf_addstr(buf, key_);
393-
if (!opts->omit_values) {
394-
if (opts->show_keys)
395-
strbuf_addch(buf, opts->key_delim);
396-
397-
if (opts->type == TYPE_INT)
398-
res = format_config_int64(buf, key_, value_, kvi, gently);
399-
else if (opts->type == TYPE_BOOL)
400-
res = format_config_bool(buf, key_, value_, gently);
401-
else if (opts->type == TYPE_BOOL_OR_INT)
402-
res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
403-
else if (opts->type == TYPE_BOOL_OR_STR)
404-
res = format_config_bool_or_str(buf, value_);
405-
else if (opts->type == TYPE_PATH)
406-
res = format_config_path(buf, key_, value_, gently);
407-
else if (opts->type == TYPE_EXPIRY_DATE)
408-
res = format_config_expiry_date(buf, key_, value_, gently);
409-
else if (opts->type == TYPE_COLOR)
410-
res = format_config_color(buf, key_, value_, gently);
411-
else if (value_) {
394+
395+
if (opts->omit_values)
396+
goto terminator;
397+
398+
if (opts->show_keys)
399+
strbuf_addch(buf, opts->key_delim);
400+
401+
switch (opts->type) {
402+
case TYPE_INT:
403+
res = format_config_int64(buf, key_, value_, kvi, gently);
404+
break;
405+
406+
case TYPE_BOOL:
407+
res = format_config_bool(buf, key_, value_, gently);
408+
break;
409+
410+
case TYPE_BOOL_OR_INT:
411+
res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
412+
break;
413+
414+
case TYPE_BOOL_OR_STR:
415+
res = format_config_bool_or_str(buf, value_);
416+
break;
417+
418+
case TYPE_PATH:
419+
res = format_config_path(buf, key_, value_, gently);
420+
break;
421+
422+
case TYPE_EXPIRY_DATE:
423+
res = format_config_expiry_date(buf, key_, value_, gently);
424+
break;
425+
426+
case TYPE_COLOR:
427+
res = format_config_color(buf, key_, value_, gently);
428+
break;
429+
430+
case TYPE_NONE:
431+
if (value_) {
412432
strbuf_addstr(buf, value_);
413433
} else {
414434
/* Just show the key name; back out delimiter */
415435
if (opts->show_keys)
416436
strbuf_setlen(buf, buf->len - 1);
417437
}
438+
break;
439+
440+
default:
441+
BUG("undefined type %d", opts->type);
418442
}
443+
444+
terminator:
419445
strbuf_addch(buf, opts->term);
420446
return res;
421447
}

0 commit comments

Comments
 (0)