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

Restyle Python Cluster Object - Field Defaults #12629

Closed
wants to merge 11 commits into from
81 changes: 81 additions & 0 deletions src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,86 @@ function zapTypeToPythonClusterObjectType(type, options)
return _zapTypeToPythonClusterObjectType.call(this, 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');

if (await typeChecker('isEnum')) {
return '0';
}

if (await typeChecker('isBitmap')) {
return '0';
}

if (await typeChecker('isStruct')) {
return 'field(default_factory=lambda: ' + ns + '.Structs.' + type + '())';
}

if (StringHelper.isCharString(type)) {
return '""';
}

if (StringHelper.isOctetString(type)) {
return 'b""';
}

if ([ 'single', 'double' ].includes(type.toLowerCase())) {
return '0.0';
}

if (type.toLowerCase() == 'boolean') {
return 'False'
}

// #10748: asUnderlyingZclType will emit wrong types for int{48|56|64}(u), so we process all int values here.
if (type.toLowerCase().match(/^int\d+$/)) {
return '0'
}

if (type.toLowerCase().match(/^int\d+u$/)) {
return '0'
}

resolvedType = await zclHelper.asUnderlyingZclType.call({ global : this.global }, type, options);
{
basicType = ChipTypesHelper.asBasicType(resolvedType);
if (basicType.match(/^int\d+_t$/)) {
return '0'
}
if (basicType.match(/^uint\d+_t$/)) {
return '0'
}
}
}

let promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this));
if ((this.isList || this.isArray || this.entryType) && !options.hash.forceNotList) {
promise = promise.then(typeStr => `field(default_factory=lambda: [])`);
}

const isNull = (this.isNullable && !options.hash.forceNotNullable);
const isOptional = (this.isOptional && !options.hash.forceNotOptional);

if (isNull && isOptional) {
promise = promise.then(typeStr => `None`);
} else if (isNull) {
promise = promise.then(typeStr => `NullValue`);
} else if (isOptional) {
promise = promise.then(typeStr => `None`);
}

return templateUtil.templatePromise(this.global, promise)
}

function getPythonFieldDefault(type, options)
{
return _getPythonFieldDefault.call(this, type, options)
}

async function getResponseCommandName(responseRef, options)
{
let pkgId = await templateUtil.ensureZclPackageId(this);
Expand Down Expand Up @@ -620,3 +700,4 @@ exports.zapTypeToDecodableClusterObjectType = zapTypeToDecodableClusterObjectTyp
exports.zapTypeToPythonClusterObjectType = zapTypeToPythonClusterObjectType;
exports.getResponseCommandName = getResponseCommandName;
exports.isWeaklyTypedEnum = isWeaklyTypedEnum;
exports.getPythonFieldDefault = getPythonFieldDefault;
Loading