Skip to content

Commit 5b08b3e

Browse files
proposal: Display full path in the header for nested columns (#515)
1 parent 53b0751 commit 5b08b3e

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

linodecli/output.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def print(
103103
)
104104

105105
if isinstance(columns[0], OpenAPIResponseAttr):
106-
header = [c.column_name for c in columns]
106+
header = [c.name for c in columns]
107107
else:
108108
header = columns
109109

@@ -238,7 +238,9 @@ def _get_columns(self, attrs):
238238
columns = []
239239
for col in self.columns.split(","):
240240
for attr in attrs:
241-
if attr.column_name == col:
241+
# Display this column if the format string
242+
# matches the column_name or path of this column
243+
if col in (attr.column_name, attr.name):
242244
attrs.remove(attr)
243245
columns.append(attr)
244246

@@ -314,6 +316,10 @@ def _json_output(self, header, data, to):
314316
"""
315317
Prints data in JSON format
316318
"""
319+
# Special handling for JSON headers.
320+
# We're only interested in the last part of the column name.
321+
header = [v.split(".")[-1] for v in header]
322+
317323
content = []
318324
if len(data) and isinstance(data[0], dict): # we got delimited json in
319325
# parse down to the value we display

tests/unit/test_output.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,10 @@ def test_print_subtable_single(
504504

505505
output = output.getvalue().splitlines()
506506

507-
lines = ["foo\tbar\tfoobar", "cool\tcool2\twow"]
507+
lines = [
508+
"foo.single_nested.foo\tfoo.single_nested.bar\tfoobar",
509+
"cool\tcool2\twow",
510+
]
508511

509512
for i, line in enumerate(lines):
510513
assert line in output[i]
@@ -551,3 +554,37 @@ def test_print_subtable_with_selection(
551554

552555
for i, line in enumerate(lines):
553556
assert line in output[i]
557+
558+
def test_format_nested_field(
559+
self, mock_cli, get_operation_for_subtable_test
560+
):
561+
output = io.StringIO()
562+
563+
mock_cli.output_handler.mode = OutputMode.delimited
564+
mock_cli.output_handler.single_table = True
565+
mock_cli.output_handler.columns = "foo.single_nested.bar"
566+
567+
mock_data = {
568+
"table": [{"foo": "cool", "bar": 12345}],
569+
"foo": {
570+
"single_nested": {"foo": "cool", "bar": "cool2"},
571+
"table": [{"foobar": ["127.0.0.1", "127.0.0.2"]}],
572+
},
573+
"foobar": "wow",
574+
}
575+
576+
mock_cli.output_handler.print_response(
577+
get_operation_for_subtable_test.response_model,
578+
data=[mock_data],
579+
to=output,
580+
)
581+
582+
output = output.getvalue().splitlines()
583+
584+
lines = [
585+
"foo.single_nested.bar",
586+
"cool2",
587+
]
588+
589+
for i, line in enumerate(lines):
590+
assert line in output[i]

0 commit comments

Comments
 (0)