Skip to content

Commit

Permalink
BI-5485 Fix icon config loading (#489)
Browse files Browse the repository at this point in the history
* BI-5485 Fix icon config loading

* mypy

* mypy

* fix icon type
  • Loading branch information
KonstantAnxiety authored Jun 14, 2024
1 parent 7684a83 commit c6a1287
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/app_configs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CONNECTORS:
YDB:
HAS_AUTH: false
CONNECTOR_AVAILABILITY:
icon_src:
icon_type: data
data: "blank"
sections: []
uncategorized:
- availability: free
Expand Down
35 changes: 29 additions & 6 deletions lib/dl_api_lib/dl_api_lib/connector_availability/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ def as_dict(self, conn_type: str) -> dict[str, Any]:
conn_type=conn_type,
)

@classmethod
@abc.abstractmethod
def from_settings(cls, settings: ObjectLikeConfig | ConnectorIconSrc) -> ConnectorIconSrcConfig:
raise NotImplementedError


@attr.s(kw_only=True)
class ConnectorIconSrcConfigData(ConnectorIconSrcConfig):
Expand All @@ -160,6 +165,13 @@ def as_dict(self, conn_type: str) -> dict[str, Any]:
data=data,
)

@classmethod
def from_settings(cls, settings: ObjectLikeConfig | ConnectorIconSrc) -> ConnectorIconSrcConfigData:
return cls(
icon_type=ConnectorIconSrcType.data,
data=settings.data,
)


@attr.s(kw_only=True)
class ConnectorIconSrcConfigUrl(ConnectorIconSrcConfig):
Expand All @@ -175,16 +187,27 @@ def as_dict(self, conn_type: str) -> dict[str, Any]:
url=url,
)

@classmethod
def from_settings(cls, settings: ObjectLikeConfig | ConnectorIconSrc) -> ConnectorIconSrcConfigUrl:
assert isinstance(
settings.url_prefix, str
), f'Expected a string value in URL config, got "{type(settings.url_prefix)}"'
return cls(
icon_type=ConnectorIconSrcType.url,
url_prefix=settings.url_prefix,
)


def connector_icon_src_config_factory(icon_data: ConnectorIconSrc) -> ConnectorIconSrcConfig:
icon_data_dict = icon_data.as_dict()
def connector_icon_src_config_factory(icon_data: ConnectorIconSrc | ObjectLikeConfig) -> ConnectorIconSrcConfig:
icon_type = icon_data.icon_type
icon_type_str = icon_type.value if isinstance(icon_type, ConnectorIconSrcType) else icon_type

cfg_class: dict[ConnectorIconSrcType, Type[ConnectorIconSrcConfig]] = {
ConnectorIconSrcType.data: ConnectorIconSrcConfigData,
ConnectorIconSrcType.url: ConnectorIconSrcConfigUrl,
cfg_class: dict[str, Type[ConnectorIconSrcConfig]] = {
ConnectorIconSrcType.data.value: ConnectorIconSrcConfigData,
ConnectorIconSrcType.url.value: ConnectorIconSrcConfigUrl,
}

return cfg_class[icon_data.icon_type](**icon_data_dict)
return cfg_class[icon_type_str].from_settings(icon_data)


@attr.s(kw_only=True)
Expand Down
8 changes: 1 addition & 7 deletions lib/dl_configs/dl_configs/connector_availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ class ConnectorIconSrcType(DynamicEnum):
class ConnectorIconSrc:
icon_type: ConnectorIconSrcType = attr.ib(default=ConnectorIconSrcType.data)
url_prefix: Optional[str] = attr.ib(default=None)

def as_dict(self) -> dict[str, Any]:
conn_icon_settings_dict: dict[str, Any] = dict()
if self.icon_type == ConnectorIconSrcType.url:
conn_icon_settings_dict["url_prefix"] = self.url_prefix
conn_icon_settings_dict["icon_type"] = self.icon_type
return conn_icon_settings_dict
data: Optional[str] = attr.ib(default=None)


@attr.s(kw_only=True)
Expand Down

0 comments on commit c6a1287

Please sign in to comment.