Skip to content

Commit

Permalink
Merge branch 'main' into kgpayne/record-before-stream-error
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon authored Jul 4, 2023
2 parents b1acdfc + d228ab0 commit c51b0cd
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:
file_glob: true

- name: Publish
uses: pypa/[email protected].6
uses: pypa/[email protected].7
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,28 @@ name: Test
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- "cookiecutter/**"
- "samples/**"
- "singer_sdk/**"
- "tests/**"
- "noxfile.py"
- "poetry.lock"
- "pyproject.toml"
- ".github/workflows/test.yml"
- ".github/workflows/constraints.txt"
push:
branches: [main]
paths:
- "cookiecutter/**"
- "samples/**"
- "singer_sdk/**"
- "tests/**"
- "noxfile.py"
- "poetry.lock"
- "pyproject.toml"
- ".github/workflows/test.yml"
- ".github/workflows/constraints.txt"
workflow_dispatch:
inputs: {}

Expand Down Expand Up @@ -177,3 +197,4 @@ jobs:
uses: codecov/[email protected]
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
8 changes: 8 additions & 0 deletions docs/classes/typing/singer_sdk.typing.Constant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
singer_sdk.typing.Constant
==========================

.. currentmodule:: singer_sdk.typing

.. autoclass:: Constant
:members:
:special-members: __init__, __call__
8 changes: 8 additions & 0 deletions docs/classes/typing/singer_sdk.typing.DiscriminatedUnion.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
singer_sdk.typing.DiscriminatedUnion
====================================

.. currentmodule:: singer_sdk.typing

.. autoclass:: DiscriminatedUnion
:members:
:special-members: __init__, __call__
8 changes: 8 additions & 0 deletions docs/classes/typing/singer_sdk.typing.OneOf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
singer_sdk.typing.OneOf
=======================

.. currentmodule:: singer_sdk.typing

.. autoclass:: OneOf
:members:
:special-members: __init__, __call__
3 changes: 3 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ JSON Schema builder classes
typing.PropertiesList
typing.ArrayType
typing.BooleanType
typing.Constant
typing.CustomType
typing.DateTimeType
typing.DateType
typing.DiscriminatedUnion
typing.DurationType
typing.EmailType
typing.HostnameType
Expand All @@ -104,6 +106,7 @@ JSON Schema builder classes
typing.JSONPointerType
typing.NumberType
typing.ObjectType
typing.OneOf
typing.Property
typing.RegexType
typing.RelativeJSONPointerType
Expand Down
74 changes: 37 additions & 37 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion singer_sdk/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_batches(
start=1,
):
filename = f"{prefix}{sync_id}-{i}.json.gz"
with self.batch_config.storage.fs() as fs:
with self.batch_config.storage.fs(create=True) as fs:
# TODO: Determine compression from config.
with fs.open(filename, "wb") as f, gzip.GzipFile(
fileobj=f,
Expand Down
144 changes: 144 additions & 0 deletions singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,150 @@ def type_dict(self) -> dict: # type: ignore[override]
return result


class OneOf(JSONPointerType):
"""OneOf type.
This type allows for a value to be one of a set of types.
Examples:
>>> t = OneOf(StringType, IntegerType)
>>> print(t.to_json(indent=2))
{
"oneOf": [
{
"type": [
"string"
]
},
{
"type": [
"integer"
]
}
]
}
"""

def __init__(self, *types: W | type[W]) -> None:
"""Initialize OneOf type.
Args:
types: Types to choose from.
"""
self.wrapped = types

@property
def type_dict(self) -> dict: # type: ignore[override]
"""Get type dictionary.
Returns:
A dictionary describing the type.
"""
return {"oneOf": [t.type_dict for t in self.wrapped]}


class Constant(JSONTypeHelper):
"""A constant property.
A property that is always the same value.
Examples:
>>> t = Constant("foo")
>>> print(t.to_json(indent=2))
{
"const": "foo"
}
"""

def __init__(self, value: _JsonValue) -> None:
"""Initialize Constant.
Args:
value: Value of the constant.
"""
self.value = value

@property
def type_dict(self) -> dict: # type: ignore[override]
"""Get type dictionary.
Returns:
A dictionary describing the type.
"""
return {"const": self.value}


class DiscriminatedUnion(OneOf):
"""A discriminator property.
This is a special case of :class:`singer_sdk.typing.OneOf`, where values are
JSON objects, and the type of the object is determined by a property in the
object.
The property is a :class:`singer_sdk.typing.Constant` called the discriminator
property.
"""

def __init__(self, key: str, **options: ObjectType) -> None:
"""Initialize a discriminated union type.
Args:
key: Name of the discriminator property.
options: Mapping of discriminator values to object types.
Examples:
>>> t = DiscriminatedUnion("species", cat=ObjectType(), dog=ObjectType())
>>> print(t.to_json(indent=2))
{
"oneOf": [
{
"type": "object",
"properties": {
"species": {
"const": "cat",
"description": "Discriminator for object of type 'cat'."
}
},
"required": [
"species"
]
},
{
"type": "object",
"properties": {
"species": {
"const": "dog",
"description": "Discriminator for object of type 'dog'."
}
},
"required": [
"species"
]
}
]
}
"""
self.key = key
self.options = options

super().__init__(
*(
ObjectType(
Property(
key,
Constant(k),
required=True,
description=f"Discriminator for object of type '{k}'.",
),
*v.wrapped.values(),
additional_properties=v.additional_properties,
pattern_properties=v.pattern_properties,
)
for k, v in options.items()
),
)


class CustomType(JSONTypeHelper):
"""Accepts an arbitrary JSON Schema dictionary."""

Expand Down
Loading

0 comments on commit c51b0cd

Please sign in to comment.