Merge pull request #236 from webzard-io/fix/bug

fix cannot addModuleId to stateMap bug
This commit is contained in:
yz-yu 2022-01-28 13:39:23 +08:00 committed by GitHub
commit 8b1a8a7845
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 9 deletions

View File

@ -31,7 +31,7 @@ describe('format to module schema', () => {
type: 'test/v1/child', type: 'test/v1/child',
properties: { properties: {
test: '{{ value.BE_CAREFUL.toFixed(2) }}', test: '{{ value.BE_CAREFUL.toFixed(2) }}',
add: '{{ input1.value }} / {{ BE_CAREFUL.value }}' add: '{{ input1.value }} / {{ BE_CAREFUL.value }}',
}, },
traits: [], traits: [],
@ -59,6 +59,33 @@ describe('format to module schema', () => {
`); `);
}); });
it('will add module id in stateMap', () => {
expect(
addModuleId({
version: 'test/v1',
kind: 'Module',
metadata: {
name: 'test',
},
spec: {
properties: {},
events: [],
stateMap: {
value: 'input1.value',
},
},
impl: [
{
id: 'input1',
type: 'test/v1/input',
properties: {},
traits: [],
},
],
}).spec.stateMap
).toEqual({ value: '{{ $moduleId }}__input1.value' });
});
it('will remove module id in expression', () => { it('will remove module id in expression', () => {
expect( expect(
removeModuleId({ removeModuleId({
@ -88,7 +115,7 @@ describe('format to module schema', () => {
type: 'test/v1/child', type: 'test/v1/child',
properties: { properties: {
test: '{{ value.BE_CAREFUL.toFixed(2) }}', test: '{{ value.BE_CAREFUL.toFixed(2) }}',
add: '{{ {{ $moduleId }}__input1.value }} / {{ {{ $moduleId }}__BE_CAREFUL.value }}' add: '{{ {{ $moduleId }}__input1.value }} / {{ {{ $moduleId }}__BE_CAREFUL.value }}',
}, },
traits: [], traits: [],
@ -115,4 +142,31 @@ describe('format to module schema', () => {
] ]
`); `);
}); });
it('will remove module id in stateMap', () => {
expect(
removeModuleId({
version: 'test/v1',
kind: 'Module',
metadata: {
name: 'test',
},
spec: {
properties: {},
events: [],
stateMap: {
value: '{{ $moduleId }}__input1.value',
},
},
impl: [
{
id: '{{ $moduleId }}__input1',
type: 'test/v1/input',
properties: {},
traits: [],
},
],
}).spec.stateMap
).toEqual({ value: 'input1.value' });
});
}); });

View File

@ -24,24 +24,32 @@ export function addModuleId(module: Module): Module {
ids.push((c.properties.template as any).id); ids.push((c.properties.template as any).id);
} }
}); });
function traverse(tree: Record<string, any>) { function traverse(tree: Record<string, any>, isValueExp = false) {
for (const key in tree) { for (const key in tree) {
const val = tree[key]; const val = tree[key];
if (typeof val === 'string') { if (typeof val === 'string') {
if (ids.includes(val)) { if (isValueExp) {
// case 1: value is expression, replace it
const newField = replaceIdsInExp(val, ids);
tree[key] = newField;
} else if (ids.includes(val)) {
// case 2: value is equal to a component id
tree[key] = `${ModuleIdPrefix}${val}`; tree[key] = `${ModuleIdPrefix}${val}`;
} else { } else {
// case 3: value is normal string, try to replace the componentIds in this string
const newField = replaceIdsInProperty(val, ids); const newField = replaceIdsInProperty(val, ids);
tree[key] = newField; tree[key] = newField;
} }
} else if (typeof val === 'object') { } else if (typeof val === 'object') {
traverse(val); // case 4: value is object, recurse it
traverse(val, isValueExp);
} }
} }
} }
traverse(module.impl); traverse(module.impl);
traverse(module.spec.stateMap); // value of stateMap is expression, not property
traverse(module.spec.stateMap, true);
return module; return module;
} }
@ -65,8 +73,7 @@ export function removeModuleId(module: Module): Module {
// example: replaceIdsInExp('{{input1.value}} + {{input2.value}}', ids: ['input1']]) // example: replaceIdsInExp('{{input1.value}} + {{input2.value}}', ids: ['input1']])
function replaceIdsInProperty(property: string, ids: string[]): string { function replaceIdsInProperty(property: string, ids: string[]): string {
const expRegExp = new RegExp('{{(.*?)}}', 'g'); const matches = [...property.matchAll(/{{(.*?)}}/g)];
const matches = [...property.matchAll(expRegExp)];
if (matches.length === 0) return property; if (matches.length === 0) return property;
@ -106,7 +113,7 @@ function replaceIdsInExp(exp: string, ids: string[]): string | void {
}); });
if (identifierPos.length === 0) { if (identifierPos.length === 0) {
null; return exp;
} }
const newExp = identifierPos.reverse().reduce((result, { start, end, replaceStr }) => { const newExp = identifierPos.reverse().reduce((result, { start, end, replaceStr }) => {
return result.slice(0, start) + `${replaceStr}` + result.slice(end); return result.slice(0, start) + `${replaceStr}` + result.slice(end);