Skip to content

Commit

Permalink
Fix support for global types in Python Matter codegen (project-chip#1394
Browse files Browse the repository at this point in the history
)

The support for global types was added in commit af33f57. However,
it was added only in zapTypeToPythonClusterObjectType(). For proper
functionality support in getPythonFieldDefault() is also required.
  • Loading branch information
arkq authored Aug 14, 2024
1 parent 1e78f28 commit dca1ad0
Showing 1 changed file with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ function nsValueToNamespace(ns, clusterCount) {
return `${prefix}${asUpperCamelCase(ns)}${postfix}`;
}

// Not to be exported.
function nsValueToPythonNamespace(ns, clusterCount) {
if (clusterCount == 0) {
return 'Globals';
}
return ns;
}

/*
* @brief
*
Expand Down Expand Up @@ -809,7 +817,6 @@ function zapTypeToDecodableClusterObjectType(type, options) {

async function _zapTypeToPythonClusterObjectType(type, options) {
async function fn(pkgId) {
const ns = options.hash.ns;
const typeChecker = async (method) =>
zclHelper[method](this.global.db, type, pkgId).then(
(zclType) => zclType != 'unknown'
Expand All @@ -827,10 +834,10 @@ async function _zapTypeToPythonClusterObjectType(type, options) {
pkgId
);

if (enumObj.enumClusterCount == 0) {
// This is a global enum.
return `Globals.Enums.${type}`;
}
const ns = nsValueToPythonNamespace(
options.hash.ns,
enumObj.enumClusterCount
);

return ns + '.Enums.' + type;
}
Expand All @@ -846,10 +853,10 @@ async function _zapTypeToPythonClusterObjectType(type, options) {
pkgId
);

if (structObj.structClusterCount == 0) {
// This is a global struct.
return `Globals.Structs.${type}`;
}
const ns = nsValueToPythonNamespace(
options.hash.ns,
structObj.structClusterCount
);

return ns + '.Structs.' + type;
}
Expand Down Expand Up @@ -926,7 +933,6 @@ function zapTypeToPythonClusterObjectType(type, options) {

async function _getPythonFieldDefault(type, options) {
async function fn(pkgId) {
const ns = options.hash.ns;
const typeChecker = async (method) =>
zclHelper[method](this.global.db, type, pkgId).then(
(zclType) => zclType != 'unknown'
Expand All @@ -941,6 +947,17 @@ async function _getPythonFieldDefault(type, options) {
}

if (await typeChecker('isStruct')) {
const structObj = await zclQuery.selectStructByName(
this.global.db,
type,
pkgId
);

const ns = nsValueToPythonNamespace(
options.hash.ns,
structObj.structClusterCount
);

return 'field(default_factory=lambda: ' + ns + '.Structs.' + type + '())';
}

Expand Down

0 comments on commit dca1ad0

Please sign in to comment.