Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor metadata generator #379

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/spaceone/inventory/plugin/collector/lib/metadata_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _separate_metadata(self):
return query_sets_meta, search_meta, table_meta, tabs_meta, widget_meta

def _generate_search(self, search_meta: dict) -> list:
return self._generate_fields(search_meta["fields"])
return self._generate_fields(search_meta["fields"], is_search=True)

def _generate_table(self, table_meta: dict) -> dict:
table_metadata = self._generate_default_dynamic_view(
Expand Down Expand Up @@ -123,6 +123,15 @@ def _generate_tabs(self, tabs_meta: list) -> dict:
if "root_path" in tab_meta:
dynamic_view["options"]["root_path"] = tab_meta["root_path"]

if "unwind" in tab_meta:
dynamic_view["options"]["unwind"] = tab_meta["unwind"]

if "default_sort" in tab_meta:
dynamic_view["options"]["default_sort"] = tab_meta["default_sort"]

if "search" in tab_meta:
dynamic_view["options"]["search"] = tab_meta["search"]

new_tabs_metadata.append(dynamic_view)
return {"layouts": new_tabs_metadata}

Expand All @@ -146,7 +155,7 @@ def _generate_sort(table: dict) -> dict:

return Sort(**sort_option).dict()

def _generate_fields(self, fields: list) -> list:
def _generate_fields(self, fields: list, is_search: bool = True) -> list:
gen_fields = []
for field in fields:
if "type" not in field:
Expand Down Expand Up @@ -177,7 +186,10 @@ def _generate_fields(self, fields: list) -> list:
gen_fields.append(self._generate_image_field(field))

elif field["type"] == "enum":
gen_fields.append(self._generate_enum_field(field))
if is_search:
gen_fields.append(self._generate_search_enum_field(field))
else:
gen_fields.append(self._generate_enum_field(field))

elif field["type"] == "more":
gen_fields.append(self._generate_more_field(field))
Expand Down Expand Up @@ -375,6 +387,27 @@ def _generate_image_field(self, field: dict, is_enum: bool = False) -> dict:
else:
return EnumImageField(**field).dict(exclude_none=True)

def _generate_search_enum_field(self, field: dict) -> dict:
if "key" not in field:
field = self._add_key_name_fields(field)

enums = {}
if "enums" in field:
for enum in field["enums"]:
enum_keys = list(enum.keys())
enum_key = enum_keys[0]
if enum_label := enum.get("label"):
enums[enum_key] = {"label": enum_label}
else:
enums[enum_key] = {"label": enum_key}

return {
"key": field["key"],
"name": field["name"],
"type": "enum",
"enums": enums,
}

def _generate_enum_field(self, field: dict) -> dict:
if "key" not in field:
field = self._add_key_name_fields(field)
Expand Down
Loading