|
6 | 6 |
|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | | -from json import dumps |
| 9 | +from json import loads |
10 | 10 | from sys import version_info |
11 | | -from typing import Any, Dict, Optional, Set, Union |
| 11 | +from typing import Any, Dict, List, Optional |
12 | 12 |
|
13 | | -from pydantic import BaseModel, Field, ValidationError, model_serializer |
| 13 | +from pydantic import BaseModel, ConfigDict |
14 | 14 |
|
15 | 15 | if version_info >= (3, 11): |
16 | 16 | from typing import Self |
17 | 17 | else: |
18 | 18 | from typing_extensions import Self |
19 | 19 |
|
20 | 20 |
|
21 | | -from algoliasearch.ingestion.models.destination_index_name import DestinationIndexName |
| 21 | +from algoliasearch.ingestion.models.record_type import RecordType |
| 22 | + |
| 23 | +_ALIASES = { |
| 24 | + "index_name": "indexName", |
| 25 | + "record_type": "recordType", |
| 26 | + "attributes_to_exclude": "attributesToExclude", |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +def _alias_generator(name: str) -> str: |
| 31 | + return _ALIASES.get(name, name) |
22 | 32 |
|
23 | 33 |
|
24 | 34 | class DestinationInput(BaseModel): |
25 | 35 | """ |
26 | 36 | DestinationInput |
27 | 37 | """ |
28 | 38 |
|
29 | | - oneof_schema_1_validator: Optional[DestinationIndexName] = Field(default=None) |
30 | | - |
31 | | - actual_instance: Union[DestinationIndexName, None] = None |
32 | | - one_of_schemas: Set[str] = {"DestinationIndexName"} |
33 | | - |
34 | | - def __init__(self, *args, **kwargs) -> None: |
35 | | - if args: |
36 | | - if len(args) > 1: |
37 | | - raise ValueError( |
38 | | - "If a position argument is used, only 1 is allowed to set `actual_instance`" |
39 | | - ) |
40 | | - if kwargs: |
41 | | - raise ValueError( |
42 | | - "If a position argument is used, keyword arguments cannot be used." |
43 | | - ) |
44 | | - super().__init__(actual_instance=args[0]) # pyright: ignore |
45 | | - else: |
46 | | - super().__init__(**kwargs) |
47 | | - |
48 | | - @model_serializer |
49 | | - def unwrap_actual_instance(self) -> Union[DestinationIndexName, Self, None]: |
50 | | - """ |
51 | | - Unwraps the `actual_instance` when calling the `to_json` method. |
52 | | - """ |
53 | | - return self.actual_instance if hasattr(self, "actual_instance") else self |
| 39 | + index_name: str |
| 40 | + """ Algolia index name (case-sensitive). """ |
| 41 | + record_type: Optional[RecordType] = None |
| 42 | + attributes_to_exclude: Optional[List[str]] = None |
| 43 | + """ Attributes from your source to exclude from Algolia records. Not all your data attributes will be useful for searching. Keeping your Algolia records small increases indexing and search performance. - Exclude nested attributes with `.` notation. For example, `foo.bar` indexes the `foo` attribute and all its children **except** the `bar` attribute. - Exclude attributes from arrays with `[i]`, where `i` is the index of the array element. For example, `foo.[0].bar` only excludes the `bar` attribute from the first element of the `foo` array, but indexes the complete `foo` attribute for all other elements. Use `*` as wildcard: `foo.[*].bar` excludes `bar` from all elements of the `foo` array. """ |
| 44 | + |
| 45 | + model_config = ConfigDict( |
| 46 | + strict=False, |
| 47 | + use_enum_values=True, |
| 48 | + populate_by_name=True, |
| 49 | + validate_assignment=True, |
| 50 | + protected_namespaces=(), |
| 51 | + alias_generator=_alias_generator, |
| 52 | + extra="allow", |
| 53 | + ) |
| 54 | + |
| 55 | + def to_json(self) -> str: |
| 56 | + return self.model_dump_json(by_alias=True, exclude_unset=True) |
54 | 57 |
|
55 | 58 | @classmethod |
56 | | - def from_dict(cls, obj: Union[str, Dict[str, Any]]) -> Self: |
| 59 | + def from_json(cls, json_str: str) -> Optional[Self]: |
57 | 60 | """Create an instance of DestinationInput from a JSON string""" |
58 | | - return cls.from_json(dumps(obj)) |
| 61 | + return cls.from_dict(loads(json_str)) |
| 62 | + |
| 63 | + def to_dict(self) -> Dict[str, Any]: |
| 64 | + """Return the dictionary representation of the model using alias.""" |
| 65 | + return self.model_dump( |
| 66 | + by_alias=True, |
| 67 | + exclude_none=True, |
| 68 | + exclude_unset=True, |
| 69 | + ) |
59 | 70 |
|
60 | 71 | @classmethod |
61 | | - def from_json(cls, json_str: str) -> Self: |
62 | | - """Returns the object represented by the json string""" |
63 | | - instance = cls.model_construct() |
64 | | - error_messages = [] |
65 | | - |
66 | | - try: |
67 | | - instance.actual_instance = DestinationIndexName.from_json(json_str) |
68 | | - |
69 | | - return instance |
70 | | - except (ValidationError, ValueError) as e: |
71 | | - error_messages.append(str(e)) |
| 72 | + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: |
| 73 | + """Create an instance of DestinationInput from a dict""" |
| 74 | + if obj is None: |
| 75 | + return None |
72 | 76 |
|
73 | | - raise ValueError( |
74 | | - "No match found when deserializing the JSON string into DestinationInput with oneOf schemas: DestinationIndexName. Details: " |
75 | | - + ", ".join(error_messages) |
76 | | - ) |
| 77 | + if not isinstance(obj, dict): |
| 78 | + return cls.model_validate(obj) |
77 | 79 |
|
78 | | - def to_json(self) -> str: |
79 | | - """Returns the JSON representation of the actual instance""" |
80 | | - if self.actual_instance is None: |
81 | | - return "null" |
82 | | - |
83 | | - if hasattr(self.actual_instance, "to_json") and callable( |
84 | | - self.actual_instance.to_json # pyright: ignore |
85 | | - ): |
86 | | - return self.actual_instance.to_json() # pyright: ignore |
87 | | - else: |
88 | | - return dumps(self.actual_instance) |
89 | | - |
90 | | - def to_dict(self) -> Optional[Union[Dict[str, Any], DestinationIndexName]]: |
91 | | - """Returns the dict representation of the actual instance""" |
92 | | - if self.actual_instance is None: |
93 | | - return None |
| 80 | + obj["recordType"] = obj.get("recordType") |
94 | 81 |
|
95 | | - if hasattr(self.actual_instance, "to_dict") and callable( |
96 | | - self.actual_instance.to_dict # pyright: ignore |
97 | | - ): |
98 | | - return self.actual_instance.to_dict() # pyright: ignore |
99 | | - else: |
100 | | - return self.actual_instance # pyright: ignore |
| 82 | + return cls.model_validate(obj) |
0 commit comments