Skip to content

Commit

Permalink
[core][feat] list: allow for listing props with default props (#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Sep 23, 2024
1 parent 077a808 commit 146cd10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
23 changes: 19 additions & 4 deletions fixcore/fixcore/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from collections import defaultdict
from contextlib import suppress
from datetime import timedelta, datetime
from functools import partial, lru_cache
from functools import partial, lru_cache, cached_property
from itertools import dropwhile, chain
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -2444,8 +2444,12 @@ class PropToShow:
alternative_path: Optional[List[str]] = None
override_kind: Optional[str] = None

@cached_property
def path_str(self) -> str:
return ".".join(self.path)

def full_path(self) -> str:
return self.path_access or ".".join(self.path)
return self.path_access or self.path_str

def value(self, node: JsonElement) -> Optional[JsonElement]:
result = js_value_at(node, self.path)
Expand Down Expand Up @@ -2502,8 +2506,9 @@ class ListCommand(CLICommand, OutputTransformer):
## Options
- `--csv` [optional]: format the output as CSV. Can't be used together with `--markdown`.
- `--markdown` [optional]: format the output as Markdown table. Can't be used together with `--csv`.
- `--json-table` [optional]: format the output as JSON table.
- `--with-defaults` [optional]: show the default properties in addition to the defined ones.
## Examples
Expand Down Expand Up @@ -2619,6 +2624,7 @@ def args_info(self) -> ArgsInfo:
ArgInfo("--csv", help_text="format", option_group="format"),
ArgInfo("--markdown", help_text="format", option_group="format"),
ArgInfo("--json-table", help_text="format", option_group="format"),
ArgInfo("--with-defaults"),
ArgInfo(
expects_value=True,
help_text="comma separated list of properties to show",
Expand All @@ -2632,6 +2638,7 @@ def parse(self, arg: Optional[str] = None, ctx: CLIContext = EmptyContext, **kwa
output_type.add_argument("--csv", dest="csv", action="store_true")
output_type.add_argument("--markdown", dest="markdown", action="store_true")
output_type.add_argument("--json-table", dest="json_table", action="store_true")
parser.add_argument("--with-defaults", dest="with_defaults", action="store_true")
parsed, properties_list = parser.parse_known_args(arg.split() if arg else [])
properties = " ".join(properties_list) if properties_list else None
is_aggregate: bool = ctx.query is not None and ctx.query.aggregate is not None
Expand Down Expand Up @@ -2766,7 +2773,15 @@ def unique_name(path: List[str], current: str) -> str:
def props_to_show(
props_setting: Tuple[List[PropToShow], List[PropToShow], List[PropToShow], List[PropToShow]]
) -> List[PropToShow]:
props = parse_props_to_show(properties) if properties is not None else default_props_to_show(props_setting)
if properties:
props = parse_props_to_show(properties)
if parsed.with_defaults:
paths = {prop.path_str for prop in props}
for prop in default_props_to_show(props_setting):
if prop.path_str not in paths:
props.append(prop)
else:
props = default_props_to_show(props_setting)
return create_unique_names(props)

def fmt_json(elem: Json) -> JsonElement:
Expand Down
5 changes: 5 additions & 0 deletions fixcore/tests/fixcore/cli/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import os
import re
import sqlite3
from datetime import timedelta
from functools import partial
Expand Down Expand Up @@ -680,6 +681,10 @@ async def test_list_command(cli: CLI) -> None:
"Region / Zone",
]

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


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

0 comments on commit 146cd10

Please sign in to comment.