|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from json import dumps, loads |
| 10 | +from typing import Dict, Optional, Self, Union |
| 11 | + |
| 12 | +from pydantic import ( |
| 13 | + BaseModel, |
| 14 | + Field, |
| 15 | + StrictInt, |
| 16 | + StrictStr, |
| 17 | + ValidationError, |
| 18 | + model_serializer, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class BuiltInOperationValue(BaseModel): |
| 23 | + """ |
| 24 | + BuiltInOperationValue |
| 25 | + """ |
| 26 | + |
| 27 | + oneof_schema_1_validator: Optional[StrictStr] = Field( |
| 28 | + default=None, |
| 29 | + description="A string to append or remove for the `Add`, `Remove`, and `AddUnique` operations.", |
| 30 | + ) |
| 31 | + oneof_schema_2_validator: Optional[StrictInt] = Field( |
| 32 | + default=None, |
| 33 | + description="A number to add, remove, or append, depending on the operation.", |
| 34 | + ) |
| 35 | + actual_instance: Optional[Union[int, str]] = None |
| 36 | + |
| 37 | + def __init__(self, *args, **kwargs) -> None: |
| 38 | + if args: |
| 39 | + if len(args) > 1: |
| 40 | + raise ValueError( |
| 41 | + "If a position argument is used, only 1 is allowed to set `actual_instance`" |
| 42 | + ) |
| 43 | + if kwargs: |
| 44 | + raise ValueError( |
| 45 | + "If a position argument is used, keyword arguments cannot be used." |
| 46 | + ) |
| 47 | + super().__init__(actual_instance=args[0]) |
| 48 | + else: |
| 49 | + super().__init__(**kwargs) |
| 50 | + |
| 51 | + @model_serializer |
| 52 | + def unwrap_actual_instance(self) -> Optional[Union[int, str]]: |
| 53 | + """ |
| 54 | + Unwraps the `actual_instance` when calling the `to_json` method. |
| 55 | + """ |
| 56 | + return self.actual_instance |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def from_dict(cls, obj: dict) -> Self: |
| 60 | + return cls.from_json(dumps(obj)) |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def from_json(cls, json_str: str) -> Self: |
| 64 | + """Returns the object represented by the json string""" |
| 65 | + instance = cls.model_construct() |
| 66 | + error_messages = [] |
| 67 | + |
| 68 | + try: |
| 69 | + instance.oneof_schema_1_validator = loads(json_str) |
| 70 | + instance.actual_instance = instance.oneof_schema_1_validator |
| 71 | + |
| 72 | + return instance |
| 73 | + except (ValidationError, ValueError) as e: |
| 74 | + error_messages.append(str(e)) |
| 75 | + try: |
| 76 | + instance.oneof_schema_2_validator = loads(json_str) |
| 77 | + instance.actual_instance = instance.oneof_schema_2_validator |
| 78 | + |
| 79 | + return instance |
| 80 | + except (ValidationError, ValueError) as e: |
| 81 | + error_messages.append(str(e)) |
| 82 | + |
| 83 | + raise ValueError( |
| 84 | + "No match found when deserializing the JSON string into BuiltInOperationValue with oneOf schemas: int, str. Details: " |
| 85 | + + ", ".join(error_messages) |
| 86 | + ) |
| 87 | + |
| 88 | + def to_json(self) -> str: |
| 89 | + """Returns the JSON representation of the actual instance""" |
| 90 | + if self.actual_instance is None: |
| 91 | + return "null" |
| 92 | + |
| 93 | + to_json = getattr(self.actual_instance, "to_json", None) |
| 94 | + if callable(to_json): |
| 95 | + return self.actual_instance.to_json() |
| 96 | + else: |
| 97 | + return dumps(self.actual_instance) |
| 98 | + |
| 99 | + def to_dict(self) -> Dict: |
| 100 | + """Returns the dict representation of the actual instance""" |
| 101 | + if self.actual_instance is None: |
| 102 | + return None |
| 103 | + |
| 104 | + to_dict = getattr(self.actual_instance, "to_dict", None) |
| 105 | + if callable(to_dict): |
| 106 | + return self.actual_instance.to_dict() |
| 107 | + else: |
| 108 | + return self.actual_instance |
0 commit comments