|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from typing_extensions import TypedDict, NotRequired |
| 4 | + |
| 5 | +from elimity_insights_client._domain_graph_schema import ( |
| 6 | + DomainGraphSchema, |
| 7 | + AttributeType, |
| 8 | + RelationshipAttributeType, |
| 9 | + EntityType, |
| 10 | + Type, |
| 11 | +) |
| 12 | +from elimity_insights_client._util import map_list |
| 13 | + |
| 14 | +AttributeTypeDict = TypedDict( |
| 15 | + "AttributeTypeDict", |
| 16 | + { |
| 17 | + "archived": bool, |
| 18 | + "description": str, |
| 19 | + "entityTypeId": str, |
| 20 | + "id": str, |
| 21 | + "name": str, |
| 22 | + "type": str, |
| 23 | + }, |
| 24 | +) |
| 25 | +EntityTypeDict = TypedDict( |
| 26 | + "EntityTypeDict", |
| 27 | + { |
| 28 | + "anonymized": bool, |
| 29 | + "icon": str, |
| 30 | + "id": str, |
| 31 | + "plural": str, |
| 32 | + "singular": str, |
| 33 | + }, |
| 34 | +) |
| 35 | +RelationshipAttributeTypeDict = TypedDict( |
| 36 | + "RelationshipAttributeTypeDict", |
| 37 | + { |
| 38 | + "archived": bool, |
| 39 | + "childType": str, |
| 40 | + "description": NotRequired[str], |
| 41 | + "id": str, |
| 42 | + "name": str, |
| 43 | + "parentType": str, |
| 44 | + "type": str, |
| 45 | + }, |
| 46 | +) |
| 47 | +DomainGraphSchemaDict = TypedDict( |
| 48 | + "DomainGraphSchemaDict", |
| 49 | + { |
| 50 | + "entityAttributeTypes": List[AttributeTypeDict], |
| 51 | + "entityTypes": List[EntityTypeDict], |
| 52 | + "relationshipAttributeTypes": List[RelationshipAttributeTypeDict], |
| 53 | + }, |
| 54 | +) |
| 55 | + |
| 56 | + |
| 57 | +def decode_domain_graph_schema(json: DomainGraphSchemaDict) -> DomainGraphSchema: |
| 58 | + """Decode the given JSON value to a domain graph schema.""" |
| 59 | + attribute_types = json["entityAttributeTypes"] |
| 60 | + attribute_types_ = map_list(_decode_attribute_type, attribute_types) |
| 61 | + entity_types = json["entityTypes"] |
| 62 | + entity_types_ = map_list(_decode_entity_type, entity_types) |
| 63 | + relationship_attribute_types = json["relationshipAttributeTypes"] |
| 64 | + relationship_attribute_types_ = map_list( |
| 65 | + _decode_relationship_attribute_types, relationship_attribute_types |
| 66 | + ) |
| 67 | + return DomainGraphSchema( |
| 68 | + attribute_types_, entity_types_, relationship_attribute_types_ |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def _decode_attribute_type(json: AttributeTypeDict) -> AttributeType: |
| 73 | + archived = json["archived"] |
| 74 | + description = json["description"] |
| 75 | + entity_type = json["entityTypeId"] |
| 76 | + id_ = json["id"] |
| 77 | + name = json["name"] |
| 78 | + type_ = json["type"] |
| 79 | + type__ = _decode_type(type_) |
| 80 | + return AttributeType(archived, description, entity_type, id_, name, type__) |
| 81 | + |
| 82 | + |
| 83 | +def _decode_entity_type(json: EntityTypeDict) -> EntityType: |
| 84 | + anonymized = json["anonymized"] |
| 85 | + icon = json["icon"] |
| 86 | + id = json["id"] |
| 87 | + plural = json["plural"] |
| 88 | + singular = json["singular"] |
| 89 | + return EntityType(anonymized, icon, id, plural, singular) |
| 90 | + |
| 91 | + |
| 92 | +def _decode_relationship_attribute_types( |
| 93 | + json: RelationshipAttributeTypeDict, |
| 94 | +) -> RelationshipAttributeType: |
| 95 | + archived = json["archived"] |
| 96 | + description = json.get("description", "") |
| 97 | + from_entity_type = json["parentType"] |
| 98 | + id_ = json["id"] |
| 99 | + name = json["name"] |
| 100 | + to_entity_type = json["childType"] |
| 101 | + type_ = json["type"] |
| 102 | + type__ = _decode_type(type_) |
| 103 | + return RelationshipAttributeType( |
| 104 | + archived, description, from_entity_type, id_, name, to_entity_type, type__ |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def _decode_type(json: str) -> Type: |
| 109 | + if json == "boolean": |
| 110 | + return Type.BOOLEAN |
| 111 | + elif json == "date": |
| 112 | + return Type.DATE |
| 113 | + elif json == "dateTime": |
| 114 | + return Type.DATE_TIME |
| 115 | + elif json == "number": |
| 116 | + return Type.NUMBER |
| 117 | + elif json == "string": |
| 118 | + return Type.STRING |
| 119 | + else: |
| 120 | + return Type.TIME |
0 commit comments