Skip to content

Commit f40ef8d

Browse files
authored
Add as_dict support (#34508)
* Add as_dict support * updates * updates * Update * update * update * updates * update * update * updates
1 parent 419f6fd commit f40ef8d

File tree

4 files changed

+372
-6
lines changed

4 files changed

+372
-6
lines changed

sdk/search/azure-search-documents/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
### Bugs Fixed
1212

13-
- Fixed the issue that `SearchIndexerSkillset`, `SearchField`, `SearchIndex`, `AnalyzeTextOptions`, `SearchResourceEncryptionKey`, `SynonymMap`, `SearchIndexerDataSourceConnection` could not be serialized.
13+
- Fixed the issue that `SearchIndexerSkillset`, `SearchField`, `SearchIndex`, `AnalyzeTextOptions`, `SearchResourceEncryptionKey`, `SynonymMap`, `SearchIndexerDataSourceConnection` could not be serialized and `as_dict` did not work.
1414

1515
### Other Changes
1616

sdk/search/azure-search-documents/azure/search/documents/indexes/models/_index.py

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def _to_generated(self) -> _SearchField:
205205
)
206206

207207
@classmethod
208-
def _from_generated(cls, search_field):
208+
def _from_generated(cls, search_field) -> Optional["SearchField"]:
209209
if not search_field:
210210
return None
211211
# pylint:disable=protected-access
@@ -243,15 +243,63 @@ def serialize(self, keep_readonly: bool = False, **kwargs: Any) -> MutableMappin
243243
return self._to_generated().serialize(keep_readonly=keep_readonly, **kwargs)
244244

245245
@classmethod
246-
def deserialize(cls, data: Any, content_type: Optional[str] = None) -> "SearchField":
246+
def deserialize(cls, data: Any, content_type: Optional[str] = None) -> Optional["SearchField"]:
247247
"""Parse a str using the RestAPI syntax and return a SearchField instance.
248248
249249
:param str data: A str using RestAPI structure. JSON by default.
250250
:param str content_type: JSON by default, set application/xml if XML.
251251
:returns: A SearchField instance
252252
:raises: DeserializationError if something went wrong
253253
"""
254-
return cls._from_generated(_SearchField.deserialize(data, content_type=content_type)) # type: ignore
254+
return cls._from_generated(_SearchField.deserialize(data, content_type=content_type))
255+
256+
def as_dict(self, keep_readonly: bool = True, **kwargs: Any) -> MutableMapping[str, Any]:
257+
"""Return a dict that can be serialized using json.dump.
258+
259+
:param bool keep_readonly: If you want to serialize the readonly attributes
260+
:returns: A dict JSON compatible object
261+
:rtype: dict
262+
"""
263+
return self._to_generated().as_dict(keep_readonly=keep_readonly, **kwargs) # type: ignore
264+
265+
@classmethod
266+
def from_dict(
267+
cls,
268+
data: Any,
269+
content_type: Optional[str] = None,
270+
) -> Optional["SearchField"]:
271+
"""Parse a dict using given key extractor return a model.
272+
273+
:param dict data: A dict using RestAPI structure
274+
:param str content_type: JSON by default, set application/xml if XML.
275+
:returns: A SearchField instance
276+
:rtype: SearchField
277+
:raises: DeserializationError if something went wrong
278+
"""
279+
return cls._from_generated(_SearchField.from_dict(data, content_type=content_type))
280+
281+
def __eq__(self, other: Any) -> bool:
282+
"""Compare objects by comparing all attributes.
283+
284+
:param Any other: the object to compare with
285+
:returns: True if all attributes are equal, else False
286+
:rtype: bool
287+
"""
288+
if isinstance(other, self.__class__):
289+
return self.__dict__ == other.__dict__
290+
return False
291+
292+
def __ne__(self, other: Any) -> bool:
293+
"""Compare objects by comparing all attributes.
294+
295+
:param Any other: the object to compare with
296+
:returns: False if all attributes are equal, else True
297+
:rtype: bool
298+
"""
299+
return not self.__eq__(other)
300+
301+
def __str__(self) -> str:
302+
return str(self.__dict__)
255303

256304

257305
def SimpleField(
@@ -674,6 +722,54 @@ def deserialize(cls, data: Any, content_type: Optional[str] = None) -> "SearchIn
674722
"""
675723
return cls._from_generated(_SearchIndex.deserialize(data, content_type=content_type))
676724

725+
def as_dict(self, keep_readonly: bool = True, **kwargs: Any) -> MutableMapping[str, Any]:
726+
"""Return a dict that can be serialized using json.dump.
727+
728+
:param bool keep_readonly: If you want to serialize the readonly attributes
729+
:returns: A dict JSON compatible object
730+
:rtype: dict
731+
"""
732+
return self._to_generated().as_dict(keep_readonly=keep_readonly, **kwargs) # type: ignore
733+
734+
@classmethod
735+
def from_dict(
736+
cls,
737+
data: Any,
738+
content_type: Optional[str] = None,
739+
) -> "SearchIndex":
740+
"""Parse a dict using given key extractor return a model.
741+
742+
:param dict data: A dict using RestAPI structure
743+
:param str content_type: JSON by default, set application/xml if XML.
744+
:returns: A SearchIndex instance
745+
:rtype: SearchIndex
746+
:raises: DeserializationError if something went wrong
747+
"""
748+
return cls._from_generated(_SearchIndex.from_dict(data, content_type=content_type))
749+
750+
def __eq__(self, other: Any) -> bool:
751+
"""Compare objects by comparing all attributes.
752+
753+
:param Any other: the object to compare with
754+
:returns: True if all attributes are equal, else False
755+
:rtype: bool
756+
"""
757+
if isinstance(other, self.__class__):
758+
return self.__dict__ == other.__dict__
759+
return False
760+
761+
def __ne__(self, other: Any) -> bool:
762+
"""Compare objects by comparing all attributes.
763+
764+
:param Any other: the object to compare with
765+
:returns: False if all attributes are equal, else True
766+
:rtype: bool
767+
"""
768+
return not self.__eq__(other)
769+
770+
def __str__(self) -> str:
771+
return str(self.__dict__)
772+
677773

678774
def pack_search_field(search_field: SearchField) -> _SearchField:
679775
if isinstance(search_field, dict):

0 commit comments

Comments
 (0)