Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TableStructParameter: allow the table key to be specified via SNREF #288

Merged
merged 1 commit into from
Apr 16, 2024
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
6 changes: 5 additions & 1 deletion odxtools/basicstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .parameters.parameterwithdop import ParameterWithDOP
from .parameters.physicalconstantparameter import PhysicalConstantParameter
from .parameters.tablekeyparameter import TableKeyParameter
from .parameters.tablestructparameter import TableStructParameter
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
Expand Down Expand Up @@ -316,4 +317,7 @@ def _resolve_snrefs(self, diag_layer: "DiagLayer") -> None:
super()._resolve_snrefs(diag_layer)

for param in self.parameters:
param._resolve_snrefs(diag_layer)
if isinstance(param, TableStructParameter):
param._table_struct_resolve_snrefs(diag_layer, param_list=self.parameters)
else:
param._resolve_snrefs(diag_layer)
2 changes: 1 addition & 1 deletion odxtools/multiplexercase.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _build_odxlinks(self) -> Dict[OdxLinkId, Any]:

def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
raise RuntimeError("Calling MultiplexerCase._resolve_odxlinks() is not allowed. "
"Use ._mux_case_resolve_odxlinks()().")
"Use ._mux_case_resolve_odxlinks().")

def _mux_case_resolve_odxlinks(self, odxlinks: OdxLinkDatabase, *,
key_physical_type: DataType) -> None:
Expand Down
31 changes: 23 additions & 8 deletions odxtools/parameters/tablestructparameter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# SPDX-License-Identifier: MIT
import warnings
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List, Optional, cast
from xml.etree import ElementTree

from typing_extensions import override
from typing_extensions import override, final

from ..decodestate import DecodeState
from ..encodestate import EncodeState
from ..exceptions import DecodeError, EncodeError, OdxWarning, odxraise, odxrequire
from ..exceptions import DecodeError, EncodeError, odxraise, odxrequire
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from ..odxtypes import ParameterValue
from ..utils import dataclass_fields_asdict
Expand Down Expand Up @@ -61,15 +60,31 @@ def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
self._table_key = odxlinks.resolve(self.table_key_ref, TableKeyParameter)

@override
@final
def _resolve_snrefs(self, diag_layer: "DiagLayer") -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider in the future making _resolve_snrefs take a "context" or "lookup" object, where diag_layer is a property.

We are getting too many "_resolve_snrefs" special cases

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. this is a major refactoring, though. I'll open an issue...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done: #290

raise RuntimeError(f"Calling TableStructParameter._resolve_snref() is not allowed. "
f"Use ._table_struct_resolve_snrefs() instead.")

def _table_struct_resolve_snrefs(self, diag_layer: "DiagLayer", *,
param_list: List[Parameter]) -> None:
super()._resolve_snrefs(diag_layer)

if self.table_key_snref is not None:
warnings.warn(
"Table keys cannot yet be defined using SNREFs"
" in TableStructParameters.",
OdxWarning,
stacklevel=1)
tk_candidates = [p for p in param_list if p.short_name == self.table_key_snref]
if len(tk_candidates) > 1:
odxraise(f"Short name reference '{self.table_key_snref}' could "
f"not be uniquely resolved.")
elif len(tk_candidates) == 0:
odxraise(f"Short name reference '{self.table_key_snref}' could "
f"not be resolved.")
return

tk = tk_candidates[0]
if not isinstance(tk, TableKeyParameter):
odxraise(f"Table struct '{self.short_name}' references non-TableKey parameter "
f"`{self.table_key_snref}' as its table key.")

self._table_key = tk

@property
def table_key(self) -> TableKeyParameter:
Expand Down
Loading