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

feat: Labeled enumerations #2875

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all 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
35 changes: 33 additions & 2 deletions singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,20 @@
allowed_values: list[T] | None = None,
examples: list[T] | None = None,
nullable: bool | None = None,
title: str | None = None,
) -> None:
"""Initialize the type helper.

Args:
allowed_values: A list of allowed values.
examples: A list of example values.
nullable: If True, the property may be null.
title: An optional title for this JSON schema type.
"""
self.allowed_values = allowed_values
self.examples = examples
self.nullable = nullable
self.title = title

@DefaultInstanceProperty
def type_dict(self) -> dict:
Expand All @@ -242,6 +245,9 @@
if self.examples:
result["examples"] = self.examples

if self.title:
result["title"] = self.title

Check warning on line 249 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L249

Added line #L249 was not covered by tests

return result

def to_dict(self) -> dict:
Expand Down Expand Up @@ -996,14 +1002,33 @@
{
"const": "foo"
}
>>> t2 = OneOf(
... Constant("https://api.example.com", title="US"),
... Constant("https://api-eu.example.com", title="EU"),
... )
>>> print(t2.to_json(indent=2))
{
"oneOf": [
{
"const": "https://api.example.com",
"title": "US"
},
{
"const": "https://api-eu.example.com",
"title": "EU"
}
]
}
"""

def __init__(self, value: _JsonValue) -> None:
def __init__(self, value: _JsonValue, **kwargs: t.Any) -> None:
"""Initialize Constant.

Args:
value: Value of the constant.
kwargs: Keyword-arguments.
"""
super().__init__(**kwargs)
self.value = value

@property
Expand All @@ -1013,7 +1038,13 @@
Returns:
A dictionary describing the type.
"""
return {"const": self.value}
result = {"const": self.value}

extras = self.extras
if "title" in extras:
result["title"] = extras["title"]

Check warning on line 1045 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1045

Added line #L1045 was not covered by tests

return result


class DiscriminatedUnion(OneOf):
Expand Down
Loading