-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
a4f16d9
commit 73e0b89
Showing
7 changed files
with
106 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from collections.abc import Callable | ||
|
||
from fastcs.datatypes import Bool, DataType, Enum, Float, Int, String, T | ||
|
||
TANGO_ALLOWED_DATATYPES = (Bool, DataType, Enum, Float, Int, String) | ||
|
||
|
||
def get_cast_method_to_tango_type(datatype: DataType[T]) -> Callable[[T], object]: | ||
match datatype: | ||
case Enum(): | ||
|
||
def cast_to_tango_type(value) -> int: | ||
return datatype.validate(value).value | ||
case datatype if issubclass(type(datatype), TANGO_ALLOWED_DATATYPES): | ||
|
||
def cast_to_tango_type(value) -> object: | ||
return datatype.validate(value) | ||
case _: | ||
raise ValueError(f"Unsupported datatype {datatype}") | ||
return cast_to_tango_type | ||
|
||
|
||
def get_cast_method_from_tango_type(datatype: DataType[T]) -> Callable[[object], T]: | ||
match datatype: | ||
case Enum(enum_cls): | ||
|
||
def cast_from_tango_type(value: object) -> T: | ||
return datatype.validate(enum_cls(value)) | ||
|
||
case datatype if issubclass(type(datatype), TANGO_ALLOWED_DATATYPES): | ||
|
||
def cast_from_tango_type(value) -> T: | ||
return datatype.validate(value) | ||
case _: | ||
raise ValueError(f"Unsupported datatype {datatype}") | ||
|
||
return cast_from_tango_type |
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