Skip to content

Commit

Permalink
Merge pull request #379 from whdalsrnt/master
Browse files Browse the repository at this point in the history
refactor: refactor metadata generator
  • Loading branch information
whdalsrnt authored Jun 17, 2024
2 parents e8d7c92 + 7962c79 commit 88a8f8a
Showing 1 changed file with 36 additions and 3 deletions.
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

0 comments on commit 88a8f8a

Please sign in to comment.