Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deserialization of repeated enums #450

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .codegen/service.py.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import time
import random
import logging
from ..errors import OperationTimeout, OperationFailed
from ._internal import _enum, _from_dict, _repeated, Wait
from ._internal import _enum, _from_dict, _repeated_dict, _repeated_enum, Wait

_LOG = logging.getLogger('databricks.sdk')

Expand Down Expand Up @@ -45,8 +45,9 @@ class {{.PascalName}}{{if eq "List" .PascalName}}Request{{end}}:{{if .Descriptio
{{- define "from_dict_type" -}}
{{- if not .Entity }}None
{{- else if .Entity.ArrayValue }}
{{- if .Entity.ArrayValue.IsObject }}_repeated(d, '{{.Name}}', {{template "type-nq" .Entity.ArrayValue}})
{{- else if .Entity.ArrayValue.IsExternal }}_repeated(d, '{{.Name}}', {{template "type-nq" .Entity.ArrayValue}})
{{- if .Entity.ArrayValue.IsObject }}_repeated_dict(d, '{{.Name}}', {{template "type-nq" .Entity.ArrayValue}})
{{- else if .Entity.ArrayValue.IsExternal }}_repeated_dict(d, '{{.Name}}', {{.Entity.Package.Name}}.{{template "type-nq" .Entity.ArrayValue}})
{{- else if .Entity.ArrayValue.Enum }}_repeated_enum(d, '{{.Name}}', {{template "type-nq" .Entity.ArrayValue}})
{{- else}}d.get('{{.Name}}', None){{- end -}}
{{- else if .Entity.IsObject }}_from_dict(d, '{{.Name}}', {{.Entity.PascalName}})
{{- else if .Entity.IsExternal }}_from_dict(d, '{{.Name}}', {{.Entity.Package.Name}}.{{.Entity.PascalName}})
Expand Down
23 changes: 20 additions & 3 deletions databricks/sdk/service/_internal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Callable, Dict, Generic, Type, TypeVar
from typing import Callable, Dict, Generic, Optional, Type, TypeVar


def _from_dict(d: Dict[str, any], field: str, cls: Type) -> any:
Expand All @@ -8,17 +8,34 @@ def _from_dict(d: Dict[str, any], field: str, cls: Type) -> any:
return getattr(cls, 'from_dict')(d[field])


def _repeated(d: Dict[str, any], field: str, cls: Type) -> any:
def _repeated_dict(d: Dict[str, any], field: str, cls: Type) -> any:
if field not in d or not d[field]:
return None
from_dict = getattr(cls, 'from_dict')
return [from_dict(v) for v in d[field]]


def _get_enum_value(cls: Type, value: str) -> Optional[Type]:
return next((v for v in getattr(cls, '__members__').values() if v.value == value), None)


def _enum(d: Dict[str, any], field: str, cls: Type) -> any:
"""Unknown enum values are returned as None."""
if field not in d or not d[field]:
return None
return _get_enum_value(cls, d[field])


def _repeated_enum(d: Dict[str, any], field: str, cls: Type) -> any:
"""For now, unknown enum values are not included in the response."""
if field not in d or not d[field]:
return None
return next((v for v in getattr(cls, '__members__').values() if v.value == d[field]), None)
res = []
for e in d[field]:
val = _get_enum_value(cls, e)
if val:
res.append(val)
return res


ReturnType = TypeVar('ReturnType')
Expand Down
14 changes: 7 additions & 7 deletions databricks/sdk/service/billing.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 35 additions & 31 deletions databricks/sdk/service/catalog.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading