-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JB Lovland
committed
Jan 23, 2024
1 parent
e16f5f5
commit dc20e3c
Showing
4 changed files
with
62 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
|
||
from pydantic import ValidationError | ||
|
||
|
||
def fmu_discriminator( | ||
value: dict, | ||
) -> Literal[ | ||
"FMUAggregation", | ||
"FMURealization", | ||
"FMUCase", | ||
]: | ||
""" | ||
Discriminate the type of FMU based on the fields present in the value. | ||
This function determines the type of an FMU object by checking the presence | ||
of specific fields ('aggregation' and 'realization') in the given value. | ||
It returns a string literal indicating the determined type of the FMU. | ||
- If both 'aggregation' and 'realization' fields are present, | ||
it raises a ValidationError. | ||
- If 'aggregation' is present, it returns 'FMUAggregation'. | ||
- If 'realization' is present, it returns 'FMURealization'. | ||
- If neither is present, it defaults to 'FMUCase'. | ||
""" | ||
|
||
if not isinstance(value, dict): | ||
raise ValidationError("Input value must be a dictionary.") | ||
|
||
if "aggregation" in value and "realization" in value: | ||
raise ValidationError( | ||
"Value cannot have both 'aggregation' and 'realization' " | ||
"fields. It must exclusively be either an 'FMUAggregation' " | ||
"or an 'FMURealization'." | ||
) | ||
|
||
if "aggregation" in value: | ||
return "FMUAggregation" | ||
|
||
if "realization" in value: | ||
return "FMURealization" | ||
|
||
return "FMUCase" |
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