Skip to content

Commit 146cd10

Browse files
authored
[core][feat] list: allow for listing props with default props (#2200)
1 parent 077a808 commit 146cd10

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

fixcore/fixcore/cli/command.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from collections import defaultdict
1919
from contextlib import suppress
2020
from datetime import timedelta, datetime
21-
from functools import partial, lru_cache
21+
from functools import partial, lru_cache, cached_property
2222
from itertools import dropwhile, chain
2323
from pathlib import Path
2424
from typing import (
@@ -2444,8 +2444,12 @@ class PropToShow:
24442444
alternative_path: Optional[List[str]] = None
24452445
override_kind: Optional[str] = None
24462446

2447+
@cached_property
2448+
def path_str(self) -> str:
2449+
return ".".join(self.path)
2450+
24472451
def full_path(self) -> str:
2448-
return self.path_access or ".".join(self.path)
2452+
return self.path_access or self.path_str
24492453

24502454
def value(self, node: JsonElement) -> Optional[JsonElement]:
24512455
result = js_value_at(node, self.path)
@@ -2502,8 +2506,9 @@ class ListCommand(CLICommand, OutputTransformer):
25022506
## Options
25032507
25042508
- `--csv` [optional]: format the output as CSV. Can't be used together with `--markdown`.
2505-
25062509
- `--markdown` [optional]: format the output as Markdown table. Can't be used together with `--csv`.
2510+
- `--json-table` [optional]: format the output as JSON table.
2511+
- `--with-defaults` [optional]: show the default properties in addition to the defined ones.
25072512
25082513
## Examples
25092514
@@ -2619,6 +2624,7 @@ def args_info(self) -> ArgsInfo:
26192624
ArgInfo("--csv", help_text="format", option_group="format"),
26202625
ArgInfo("--markdown", help_text="format", option_group="format"),
26212626
ArgInfo("--json-table", help_text="format", option_group="format"),
2627+
ArgInfo("--with-defaults"),
26222628
ArgInfo(
26232629
expects_value=True,
26242630
help_text="comma separated list of properties to show",
@@ -2632,6 +2638,7 @@ def parse(self, arg: Optional[str] = None, ctx: CLIContext = EmptyContext, **kwa
26322638
output_type.add_argument("--csv", dest="csv", action="store_true")
26332639
output_type.add_argument("--markdown", dest="markdown", action="store_true")
26342640
output_type.add_argument("--json-table", dest="json_table", action="store_true")
2641+
parser.add_argument("--with-defaults", dest="with_defaults", action="store_true")
26352642
parsed, properties_list = parser.parse_known_args(arg.split() if arg else [])
26362643
properties = " ".join(properties_list) if properties_list else None
26372644
is_aggregate: bool = ctx.query is not None and ctx.query.aggregate is not None
@@ -2766,7 +2773,15 @@ def unique_name(path: List[str], current: str) -> str:
27662773
def props_to_show(
27672774
props_setting: Tuple[List[PropToShow], List[PropToShow], List[PropToShow], List[PropToShow]]
27682775
) -> List[PropToShow]:
2769-
props = parse_props_to_show(properties) if properties is not None else default_props_to_show(props_setting)
2776+
if properties:
2777+
props = parse_props_to_show(properties)
2778+
if parsed.with_defaults:
2779+
paths = {prop.path_str for prop in props}
2780+
for prop in default_props_to_show(props_setting):
2781+
if prop.path_str not in paths:
2782+
props.append(prop)
2783+
else:
2784+
props = default_props_to_show(props_setting)
27702785
return create_unique_names(props)
27712786

27722787
def fmt_json(elem: Json) -> JsonElement:

fixcore/tests/fixcore/cli/command_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import os
5+
import re
56
import sqlite3
67
from datetime import timedelta
78
from functools import partial
@@ -680,6 +681,10 @@ async def test_list_command(cli: CLI) -> None:
680681
"Region / Zone",
681682
]
682683

684+
# define properties and add default properties
685+
result = await cli.execute_cli_command("search is (foo) and id=0 | list --with-defaults kind as k, id", list_sink)
686+
assert re.fullmatch("k=foo, id=0, age=.+, cloud=collector, account=sub_root", result[0][0])
687+
683688

684689
@pytest.mark.asyncio
685690
async def test_jq_command(cli: CLI) -> None:

0 commit comments

Comments
 (0)