Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/fastcs/transport/epics/ca/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def _verify_in_datatype(_, value):
def cast_from_epics_type(datatype: DataType[T], value: object) -> T:
"""Casts from an EPICS datatype to a FastCS datatype."""
match datatype:
case Bool():
if value == 0:
return False
elif value == 1:
return True
else:
raise ValueError(f"Invalid bool value from EPICS record {value}")
case Enum():
if len(datatype.members) <= MBB_MAX_CHOICES:
return datatype.validate(datatype.members[value])
Expand Down
15 changes: 15 additions & 0 deletions tests/transport/epics/ca/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_casting_to_epics(datatype, input, output):
@pytest.mark.parametrize(
"datatype, input",
[
(object(), 0),
# TODO cover Waveform and Table cases
(Enum(ShortEnum), 0), # can't use index
(Enum(ShortEnum), LongEnum.TOO), # wrong enum.Enum class
Expand Down Expand Up @@ -120,12 +121,26 @@ def test_cast_to_epics_validations(datatype, input):
(Enum(ShortMixedEnum), 0, ShortMixedEnum.STRING_MEMBER),
(Enum(ShortMixedEnum), 1, ShortMixedEnum.INT_MEMBER),
(Enum(ShortMixedEnum), 2, ShortMixedEnum.NONE_MEMBER),
(Bool(), 1, True),
(Bool(), 0, False),
],
)
def test_cast_from_epics_type(datatype, from_epics, result):
assert cast_from_epics_type(datatype, from_epics) == result


@pytest.mark.parametrize(
"datatype, input",
[
(object(), 0),
(Bool(), 3),
],
)
def test_cast_from_epics_validations(datatype, input):
with pytest.raises(ValueError):
cast_from_epics_type(datatype, input)


@pytest.mark.parametrize(
"datatype,in_record,out_record",
[
Expand Down
Loading