Skip to content

Commit

Permalink
Merge pull request #33 from asfadmin/rew/fix-builer-none-value
Browse files Browse the repository at this point in the history
Add `None` as an expected template value
  • Loading branch information
reweeden authored Jan 8, 2025
2 parents a455180 + 09be0f9 commit 133923f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mandible/metadata_mapper/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def build_with_config(template: Any, config: BuildConfig) -> Template:
return tuple(build_with_config(v, config) for v in template)
elif isinstance(template, set):
return set(build_with_config(v, config) for v in template)
elif isinstance(template, (str, int, float, bool)):
elif template is None or isinstance(template, (str, int, float, bool)):
return template

raise ValueError(template)
15 changes: 10 additions & 5 deletions mandible/metadata_mapper/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
KeyFunc = Callable[[Context], str]
Key = Union[str, KeyFunc]
Template = Union[
# Primitives
bool,
float,
int,
str,
# Special constants
None,
# Nested structures
dict[str, "Template"],
list["Template"],
tuple["Template", ...],
set["Template"],
str,
int,
float,
bool,
tuple["Template", ...],
# Special types
KeyFunc,
]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mandible"
version = "0.9.0"
version = "0.9.1"
description = "A generic framework for writing satellite data ingest systems"
authors = ["Rohan Weeden <reweeden@alaska.edu>", "Matt Perry <mperry37@alaska.edu>"]
license = "APACHE-2"
Expand Down
31 changes: 31 additions & 0 deletions tests/integration_tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,34 @@ def test_template(source_provider, context):
"list": [1, 2, 3, "A", "B", "C"],
"number": 30.5,
}


def test_template_default(source_provider, context):
mapper = MetadataMapper(
template=build({
"badkey": mapped("fixed_name_file", "badkey", default=None),
}),
source_provider=source_provider,
)

assert mapper.get_metadata(context) == {
"badkey": None,
}


def test_template_default_multiple_build(source_provider, context):
base_template = build({
"badkey": mapped("fixed_name_file", "badkey", default=None),
})
mapper = MetadataMapper(
template=build({
**base_template,
"goodkey": mapped("fixed_name_file", "integer"),
}),
source_provider=source_provider,
)

assert mapper.get_metadata(context) == {
"badkey": None,
"goodkey": 10,
}

0 comments on commit 133923f

Please sign in to comment.