Skip to content

Commit a455e57

Browse files
Remove hardcoded Darwin API rename maps. (#867)
1 parent edf8d35 commit a455e57

File tree

1 file changed

+64
-40
lines changed
  • src-electron/generator/matter/darwin/Framework/CHIP/templates

1 file changed

+64
-40
lines changed

src-electron/generator/matter/darwin/Framework/CHIP/templates/helper.js

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -135,29 +135,21 @@ function asObjectiveCNumberType(label, type, asLowerCased) {
135135
return templateUtil.templatePromise(this.global, promise);
136136
}
137137

138-
const compatClusterNameMap = {
139-
UnitTesting: 'TestCluster',
140-
};
141-
142-
function compatClusterNameRemapping(cluster) {
138+
function compatClusterNameRemapping(cluster, options) {
143139
cluster = appHelper.asUpperCamelCase(cluster, {
144140
hash: { preserveAcronyms: false },
145141
});
146142

147-
if (cluster in compatClusterNameMap) {
148-
cluster = compatClusterNameMap[cluster];
143+
const old = oldName.call(this, cluster, options);
144+
145+
if (old) {
146+
return old;
149147
}
150148

151149
return cluster;
152150
}
153151

154-
const compatAttributeNameMap = {
155-
Descriptor: {
156-
DeviceTypeList: 'DeviceList',
157-
},
158-
};
159-
160-
function compatAttributeNameRemapping(cluster, attribute) {
152+
function compatAttributeNameRemapping(cluster, attribute, options) {
161153
cluster = appHelper.asUpperCamelCase(cluster, {
162154
hash: { preserveAcronyms: false },
163155
});
@@ -166,15 +158,32 @@ function compatAttributeNameRemapping(cluster, attribute) {
166158
hash: { preserveAcronyms: false },
167159
});
168160

169-
if (cluster in compatAttributeNameMap) {
170-
if (attribute in compatAttributeNameMap[cluster]) {
171-
attribute = compatAttributeNameMap[cluster][attribute];
172-
}
161+
const old = oldName.call(this, cluster, {
162+
hash: {
163+
...options.hash,
164+
attribute: attribute,
165+
},
166+
});
167+
168+
if (old) {
169+
return old;
173170
}
174171

175172
return attribute;
176173
}
177174

175+
/**
176+
* Figure out whether the entity represented by cluster+options (could be a
177+
* cluster, attribute, command, etc) has an old name that it was renamed from,
178+
* and if so return it.
179+
*/
180+
function oldName(cluster, options) {
181+
const data = fetchAvailabilityData(this.global);
182+
const path = makeAvailabilityPath(cluster, options);
183+
184+
return findDataForPath(data, ['renames', ...path]);
185+
}
186+
178187
async function asObjectiveCClass(type, cluster, options) {
179188
let pkgIds = await templateUtil.ensureZclPackageIds(this);
180189
let isStruct = await zclHelper
@@ -198,7 +207,7 @@ async function asObjectiveCClass(type, cluster, options) {
198207

199208
if (isStruct) {
200209
if (options.hash.compatRemapClusterName) {
201-
cluster = compatClusterNameRemapping.call(this, cluster);
210+
cluster = compatClusterNameRemapping.call(this, cluster, { hash: {} });
202211
} else {
203212
let preserveAcronyms = true;
204213
if ('preserveAcronyms' in options.hash) {
@@ -317,6 +326,35 @@ function fetchAvailabilityData(global) {
317326
return availabilityData;
318327
}
319328

329+
function findDataForPath(availabilityData, path) {
330+
let foundData = undefined;
331+
for (let releaseData of availabilityData) {
332+
let names = [...path];
333+
let currentValue = releaseData;
334+
while (currentValue != undefined && names.length != 0) {
335+
currentValue = currentValue[names.shift()];
336+
}
337+
338+
if (currentValue === undefined) {
339+
continue;
340+
}
341+
342+
if (foundData !== undefined) {
343+
throw new Error(
344+
`Found two releases matching path: ${JSON.stringify(path)}`
345+
);
346+
}
347+
348+
// Store for now so we can do the "only one thing matches" check above on
349+
// later releases.
350+
foundData = currentValue;
351+
352+
// Go on to the next release
353+
}
354+
355+
return foundData;
356+
}
357+
320358
function findReleaseForPath(availabilityData, path, options) {
321359
if (options.hash.isForIds) {
322360
// Ids include all clusters, not just the ones we have real support for.
@@ -354,7 +392,7 @@ function findReleaseForPath(availabilityData, path, options) {
354392
let item = containerNames.pop();
355393
let currentContainer = releaseData;
356394
while (currentContainer !== undefined && containerNames.length != 0) {
357-
currentContainer = currentContainer?.[containerNames.shift()];
395+
currentContainer = currentContainer[containerNames.shift()];
358396
}
359397

360398
if (currentContainer === undefined) {
@@ -594,13 +632,6 @@ async function availability(clusterName, options) {
594632
return 'MTR_NEWLY_AVAILABLE';
595633
}
596634

597-
if (introducedVersions === '' && deprecatedVersions === undefined) {
598-
// TODO: For now, to minimize changes to code by not outputting availability on
599-
// things that don't already have it. Eventually this block should go
600-
// away.
601-
return '';
602-
}
603-
604635
if (deprecatedVersions === undefined) {
605636
let availabilityStrings = Object.entries(introducedVersions).map(
606637
([os, version]) => `${os}(${version})`
@@ -617,19 +648,12 @@ async function availability(clusterName, options) {
617648
}
618649

619650
if (deprecatedVersions === 'future') {
620-
// TODO: For now, to minimize changes to code by not outputting
621-
// availability on things that don't already have it. Eventually this
622-
// condition and the return after it should go away and the return inside
623-
// the if should become unconditional.
624-
if (introducedVersions != '') {
625-
let availabilityStrings = Object.entries(introducedVersions).map(
626-
([os, version]) => `${os}(${version})`
627-
);
628-
return `API_AVAILABLE(${availabilityStrings.join(
629-
', '
630-
)})\nMTR_NEWLY_DEPRECATED("${options.hash.deprecationMessage}")`;
631-
}
632-
return `MTR_NEWLY_DEPRECATED("${options.hash.deprecationMessage}")`;
651+
let availabilityStrings = Object.entries(introducedVersions).map(
652+
([os, version]) => `${os}(${version})`
653+
);
654+
return `API_AVAILABLE(${availabilityStrings.join(
655+
', '
656+
)})\nMTR_NEWLY_DEPRECATED("${options.hash.deprecationMessage}")`;
633657
}
634658

635659
// Make sure the set of OSes we were introduced and deprecated on is the same.

0 commit comments

Comments
 (0)