Skip to content

Commit

Permalink
Fix DataModel derivation handling (#30558)
Browse files Browse the repository at this point in the history
Changes:

- Make sure tests were run (we failed that before and they were broken)
- Update derivation handling to have different choices on what gets
  inherited and what not, to make unit tests pass
- Fix ordering in test since now attribute is updated not replaced

Tested:

- Unit tests pass
  • Loading branch information
andy31415 authored and pull[bot] committed Feb 12, 2024
1 parent a408565 commit 6106428
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions scripts/py_matter_idl/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pw_python_package("matter_idl") {

tests = [
"matter_idl/test_backwards_compatibility.py",
"matter_idl/test_data_model_xml.py",
"matter_idl/test_matter_idl_parser.py",
"matter_idl/test_generators.py",
"matter_idl/test_idl_generator.py",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import dataclasses
import logging
from typing import Iterable, Optional, Protocol, TypeVar

from matter_idl.matter_idl_types import Attribute, Bitmap, Cluster, Command, Enum, Event, Idl, Struct
from matter_idl.matter_idl_types import Attribute, AttributeQuality, Bitmap, Cluster, Command, Enum, Event, Idl, Struct

from .context import Context, IdlPostProcessor
from .parsing import NormalizeName
Expand Down Expand Up @@ -81,6 +82,9 @@ def merge_event_into(e: Event, cluster: Cluster):


def merge_attribute_into(a: Attribute, cluster: Cluster):
"""Pushes an attribute from a base cluster into an already
parsed cluster.
"""
existing: Optional[Attribute] = None
for existing_a in cluster.attributes:
if existing_a.definition.name == a.definition.name:
Expand All @@ -90,13 +94,21 @@ def merge_attribute_into(a: Attribute, cluster: Cluster):
if existing:
# Merge examples:
# UserLabelCluster::LabelList changes qualities (access, mandatory)
#
# TODO: add more examples and handle accordingly, sometimes just the
# conformance is changed
#
# To replace we could do:
# cluster.attributes.remove(existing)
# However then we lose any overrides
# ModeDishwasher:
# - changes the type of a list from sint32 to list[ModeOptionStruct]
# - Sets the field as read-only

# Carry over data type and definitions, except quality and maturity
existing.definition = dataclasses.replace(
a.definition,
qualities=existing.definition.qualities,
api_maturity=existing.definition.api_maturity,
)

# Inherit attribute quality
if existing.qualities == AttributeQuality.NONE:
existing.qualities = a.qualities

return

cluster.attributes.append(a)
Expand Down
4 changes: 1 addition & 3 deletions scripts/py_matter_idl/matter_idl/test_data_model_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,13 @@ def testClusterDerivation(self):
ModeTagStruct modeTags[] = 2;
}
readonly attribute ModeOptionStruct supportedModes[] = 0;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
// baseline inserted after, so to pass the test add this at the end
readonly attribute ModeOptionStruct supportedModes[] = 0;
}
''')

Expand Down

0 comments on commit 6106428

Please sign in to comment.