-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[low-code] replace emptySchemaLoader with DefaultSchemaLoader (#18947)
* replace emptySchemaLoader with DefaultSchemaLoader * fix test name * fix test * add logging for when we default to the empty schema * increment patch version * fix formatting * update changelog
- Loading branch information
Showing
12 changed files
with
92 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
airbyte-cdk/python/airbyte_cdk/sources/declarative/schema/default_schema_loader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import logging | ||
from dataclasses import InitVar, dataclass | ||
from typing import Any, Mapping | ||
|
||
from airbyte_cdk.sources.declarative.schema.json_file_schema_loader import JsonFileSchemaLoader | ||
from airbyte_cdk.sources.declarative.schema.schema_loader import SchemaLoader | ||
from airbyte_cdk.sources.declarative.types import Config | ||
from dataclasses_jsonschema import JsonSchemaMixin | ||
|
||
|
||
@dataclass | ||
class DefaultSchemaLoader(SchemaLoader, JsonSchemaMixin): | ||
""" | ||
Loads a schema from the default location or returns an empty schema for streams that have not defined their schema file yet. | ||
Attributes: | ||
config (Config): The user-provided configuration as specified by the source's spec | ||
options (Mapping[str, Any]): Additional arguments to pass to the string interpolation if needed | ||
""" | ||
|
||
config: Config | ||
options: InitVar[Mapping[str, Any]] | ||
|
||
def __post_init__(self, options: Mapping[str, Any]): | ||
self._options = options | ||
self.default_loader = JsonFileSchemaLoader(options=options, config=self.config) | ||
|
||
def get_json_schema(self) -> Mapping[str, Any]: | ||
""" | ||
Attempts to retrieve a schema from the default filepath location or returns the empty schema if a schema cannot be found. | ||
:return: The empty schema | ||
""" | ||
|
||
try: | ||
return self.default_loader.get_json_schema() | ||
except FileNotFoundError: | ||
# A slight hack since we don't directly have the stream name. However, when building the default filepath we assume the | ||
# runtime options stores stream name 'name' so we'll do the same here | ||
stream_name = self._options.get("name", "") | ||
logging.info(f"Could not find schema for stream {stream_name}, defaulting to the empty schema") | ||
return {} |
36 changes: 0 additions & 36 deletions
36
airbyte-cdk/python/airbyte_cdk/sources/declarative/schema/empty_schema_loader.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
airbyte-cdk/python/unit_tests/sources/declarative/schema/test_default_schema_loader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from airbyte_cdk.sources.declarative.schema import DefaultSchemaLoader | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"found_schema, found_error, expected_schema", | ||
[ | ||
pytest.param( | ||
{"type": "object", "properties": {}}, None, {"type": "object", "properties": {}}, id="test_has_schema_in_default_location" | ||
), | ||
pytest.param(None, FileNotFoundError, {}, id="test_schema_file_does_not_exist"), | ||
], | ||
) | ||
def test_get_json_schema(found_schema, found_error, expected_schema): | ||
default_schema_loader = DefaultSchemaLoader({}, {}) | ||
|
||
json_file_schema_loader = MagicMock() | ||
if found_schema: | ||
json_file_schema_loader.get_json_schema.return_value = {"type": "object", "properties": {}} | ||
if found_error: | ||
json_file_schema_loader.get_json_schema.side_effect = found_error | ||
|
||
default_schema_loader.default_loader = json_file_schema_loader | ||
|
||
actual_schema = default_schema_loader.get_json_schema() | ||
assert actual_schema == expected_schema |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters