|
11 | 11 |
|
12 | 12 |
|
13 | 13 | from __future__ import annotations |
| 14 | +from inspect import getfullargspec |
| 15 | +import json |
14 | 16 | import pprint |
15 | 17 | import re # noqa: F401 |
16 | | -import json |
17 | | - |
18 | 18 |
|
| 19 | +from typing import Any, List, Optional |
| 20 | +from pydantic import BaseModel, Field, StrictStr, ValidationError, validator |
| 21 | +from jellyfish._openapi_client.models.component_hls import ComponentHLS |
| 22 | +from jellyfish._openapi_client.models.component_rtsp import ComponentRTSP |
| 23 | +from typing import Union, Any, List, TYPE_CHECKING |
| 24 | +from pydantic import StrictStr, Field |
19 | 25 |
|
20 | | -from pydantic import BaseModel, Field, StrictStr |
21 | | -from jellyfish._openapi_client.models.component_metadata import ComponentMetadata |
| 26 | +COMPONENT_ONE_OF_SCHEMAS = ["ComponentHLS", "ComponentRTSP"] |
22 | 27 |
|
23 | 28 | class Component(BaseModel): |
24 | 29 | """ |
25 | 30 | Describes component |
26 | 31 | """ |
27 | | - id: StrictStr = Field(..., description="Assigned component id") |
28 | | - metadata: ComponentMetadata = Field(...) |
29 | | - type: StrictStr = Field(..., description="Component type") |
30 | | - __properties = ["id", "metadata", "type"] |
| 32 | + # data type: ComponentHLS |
| 33 | + oneof_schema_1_validator: Optional[ComponentHLS] = None |
| 34 | + # data type: ComponentRTSP |
| 35 | + oneof_schema_2_validator: Optional[ComponentRTSP] = None |
| 36 | + if TYPE_CHECKING: |
| 37 | + actual_instance: Union[ComponentHLS, ComponentRTSP] |
| 38 | + else: |
| 39 | + actual_instance: Any |
| 40 | + one_of_schemas: List[str] = Field(COMPONENT_ONE_OF_SCHEMAS, const=True) |
31 | 41 |
|
32 | 42 | class Config: |
33 | | - """Pydantic configuration""" |
34 | | - allow_population_by_field_name = True |
35 | 43 | validate_assignment = True |
36 | 44 |
|
37 | | - def to_str(self) -> str: |
38 | | - """Returns the string representation of the model using alias""" |
39 | | - return pprint.pformat(self.dict(by_alias=True)) |
| 45 | + discriminator_value_class_map = { |
| 46 | + } |
| 47 | + |
| 48 | + def __init__(self, *args, **kwargs): |
| 49 | + if args: |
| 50 | + if len(args) > 1: |
| 51 | + raise ValueError("If a position argument is used, only 1 is allowed to set `actual_instance`") |
| 52 | + if kwargs: |
| 53 | + raise ValueError("If a position argument is used, keyword arguments cannot be used.") |
| 54 | + super().__init__(actual_instance=args[0]) |
| 55 | + else: |
| 56 | + super().__init__(**kwargs) |
| 57 | + |
| 58 | + @validator('actual_instance') |
| 59 | + def actual_instance_must_validate_oneof(cls, v): |
| 60 | + instance = Component.construct() |
| 61 | + error_messages = [] |
| 62 | + match = 0 |
| 63 | + # validate data type: ComponentHLS |
| 64 | + if not isinstance(v, ComponentHLS): |
| 65 | + error_messages.append(f"Error! Input type `{type(v)}` is not `ComponentHLS`") |
| 66 | + else: |
| 67 | + match += 1 |
| 68 | + # validate data type: ComponentRTSP |
| 69 | + if not isinstance(v, ComponentRTSP): |
| 70 | + error_messages.append(f"Error! Input type `{type(v)}` is not `ComponentRTSP`") |
| 71 | + else: |
| 72 | + match += 1 |
| 73 | + if match > 1: |
| 74 | + # more than 1 match |
| 75 | + raise ValueError("Multiple matches found when setting `actual_instance` in Component with oneOf schemas: ComponentHLS, ComponentRTSP. Details: " + ", ".join(error_messages)) |
| 76 | + elif match == 0: |
| 77 | + # no match |
| 78 | + raise ValueError("No match found when setting `actual_instance` in Component with oneOf schemas: ComponentHLS, ComponentRTSP. Details: " + ", ".join(error_messages)) |
| 79 | + else: |
| 80 | + return v |
40 | 81 |
|
41 | | - def to_json(self) -> str: |
42 | | - """Returns the JSON representation of the model using alias""" |
43 | | - return json.dumps(self.to_dict()) |
| 82 | + @classmethod |
| 83 | + def from_dict(cls, obj: dict) -> Component: |
| 84 | + return cls.from_json(json.dumps(obj)) |
44 | 85 |
|
45 | 86 | @classmethod |
46 | 87 | def from_json(cls, json_str: str) -> Component: |
47 | | - """Create an instance of Component from a JSON string""" |
48 | | - return cls.from_dict(json.loads(json_str)) |
49 | | - |
50 | | - def to_dict(self): |
51 | | - """Returns the dictionary representation of the model using alias""" |
52 | | - _dict = self.dict(by_alias=True, |
53 | | - exclude={ |
54 | | - }, |
55 | | - exclude_none=True) |
56 | | - # override the default output from pydantic by calling `to_dict()` of metadata |
57 | | - if self.metadata: |
58 | | - _dict['metadata'] = self.metadata.to_dict() |
59 | | - return _dict |
| 88 | + """Returns the object represented by the json string""" |
| 89 | + instance = Component.construct() |
| 90 | + error_messages = [] |
| 91 | + match = 0 |
| 92 | + |
| 93 | + # use oneOf discriminator to lookup the data type |
| 94 | + _data_type = json.loads(json_str).get("type") |
| 95 | + if not _data_type: |
| 96 | + raise ValueError("Failed to lookup data type from the field `type` in the input.") |
| 97 | + |
| 98 | + # check if data type is `ComponentHLS` |
| 99 | + if _data_type == "ComponentHLS": |
| 100 | + instance.actual_instance = ComponentHLS.from_json(json_str) |
| 101 | + return instance |
| 102 | + |
| 103 | + # check if data type is `ComponentRTSP` |
| 104 | + if _data_type == "ComponentRTSP": |
| 105 | + instance.actual_instance = ComponentRTSP.from_json(json_str) |
| 106 | + return instance |
| 107 | + |
| 108 | + # check if data type is `ComponentHLS` |
| 109 | + if _data_type == "hls": |
| 110 | + instance.actual_instance = ComponentHLS.from_json(json_str) |
| 111 | + return instance |
| 112 | + |
| 113 | + # check if data type is `ComponentRTSP` |
| 114 | + if _data_type == "rtsp": |
| 115 | + instance.actual_instance = ComponentRTSP.from_json(json_str) |
| 116 | + return instance |
| 117 | + |
| 118 | + # deserialize data into ComponentHLS |
| 119 | + try: |
| 120 | + instance.actual_instance = ComponentHLS.from_json(json_str) |
| 121 | + match += 1 |
| 122 | + except (ValidationError, ValueError) as e: |
| 123 | + error_messages.append(str(e)) |
| 124 | + # deserialize data into ComponentRTSP |
| 125 | + try: |
| 126 | + instance.actual_instance = ComponentRTSP.from_json(json_str) |
| 127 | + match += 1 |
| 128 | + except (ValidationError, ValueError) as e: |
| 129 | + error_messages.append(str(e)) |
| 130 | + |
| 131 | + if match > 1: |
| 132 | + # more than 1 match |
| 133 | + raise ValueError("Multiple matches found when deserializing the JSON string into Component with oneOf schemas: ComponentHLS, ComponentRTSP. Details: " + ", ".join(error_messages)) |
| 134 | + elif match == 0: |
| 135 | + # no match |
| 136 | + raise ValueError("No match found when deserializing the JSON string into Component with oneOf schemas: ComponentHLS, ComponentRTSP. Details: " + ", ".join(error_messages)) |
| 137 | + else: |
| 138 | + return instance |
60 | 139 |
|
61 | | - @classmethod |
62 | | - def from_dict(cls, obj: dict) -> Component: |
63 | | - """Create an instance of Component from a dict""" |
64 | | - if obj is None: |
| 140 | + def to_json(self) -> str: |
| 141 | + """Returns the JSON representation of the actual instance""" |
| 142 | + if self.actual_instance is None: |
| 143 | + return "null" |
| 144 | + |
| 145 | + to_json = getattr(self.actual_instance, "to_json", None) |
| 146 | + if callable(to_json): |
| 147 | + return self.actual_instance.to_json() |
| 148 | + else: |
| 149 | + return json.dumps(self.actual_instance) |
| 150 | + |
| 151 | + def to_dict(self) -> dict: |
| 152 | + """Returns the dict representation of the actual instance""" |
| 153 | + if self.actual_instance is None: |
65 | 154 | return None |
66 | 155 |
|
67 | | - if not isinstance(obj, dict): |
68 | | - return Component.parse_obj(obj) |
| 156 | + to_dict = getattr(self.actual_instance, "to_dict", None) |
| 157 | + if callable(to_dict): |
| 158 | + return self.actual_instance.to_dict() |
| 159 | + else: |
| 160 | + # primitive type |
| 161 | + return self.actual_instance |
69 | 162 |
|
70 | | - _obj = Component.parse_obj({ |
71 | | - "id": obj.get("id"), |
72 | | - "metadata": ComponentMetadata.from_dict(obj.get("metadata")) if obj.get("metadata") is not None else None, |
73 | | - "type": obj.get("type") |
74 | | - }) |
75 | | - return _obj |
| 163 | + def to_str(self) -> str: |
| 164 | + """Returns the string representation of the actual instance""" |
| 165 | + return pprint.pformat(self.dict()) |
76 | 166 |
|
77 | 167 |
|
0 commit comments