Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
[Fix] add property for flattened params (#879)
Browse files Browse the repository at this point in the history
* add property for flattened params

* Fix issue
  • Loading branch information
kairu-ms authored Aug 18, 2021
1 parent 130ea45 commit 67f4aa7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
5 changes: 5 additions & 0 deletions src/generate/codemodel/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface SchemaModel {
Schema_Type(Schema): string;
Schema_Description(Schema): string;
Schema_FlattenedFrom(Schema): Schema;
Schema_PythonFlattenedTrace(Schema): Schema[];
Schema_IsPositional(Schema): boolean;
Schema_IsListOfSimple(schema: Schema): boolean;
Schema_IsList(schema: Schema): boolean;
Expand Down Expand Up @@ -168,6 +169,10 @@ export class SchemaModelImpl implements SchemaModel {
return schema?.language['cli']?.pythonFlattenedFrom;
}

public Schema_PythonFlattenedTrace(schema: Schema): Schema[] {
return schema?.language['cli']?.pythonFlattenedTrace;
}

public Schema_IsPositional(schema: Schema): boolean {
return schema?.language?.['cli']?.positional;
}
Expand Down
68 changes: 41 additions & 27 deletions src/generate/renders/generated/CliCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
const flattenedFrom = schemaHandler.Schema_FlattenedFrom(
methodParameterHandler.MethodParameter['targetProperty'],
);
const flattenedTrace = schemaHandler.Schema_PythonFlattenedTrace(
methodParameterHandler.MethodParameter['targetProperty'],
);
if (
methodParameterHandler.MethodParameter['originalParameter'] ===
originalParameterStack.last ||
Expand Down Expand Up @@ -259,35 +262,46 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
originalParameterStack.last?.language?.['cli']?.[
'moved-from-python'
] !== true &&
originalParameterStack.last.schema !== flattenedFrom
!isNullOrUndefined(flattenedTrace)
) {
const newParam = new Parameter(
flattenedFrom.language.python.name,
flattenedFrom.language.python.description,
flattenedFrom,
);
newParam.language['cli'] = flattenedFrom.language['cli'];

originalParameterStack.push(newParam);
originalParameterNameStack.push(flattenedFrom.language.python.name);
originalParameterCheckEmpty.push(
!needGeneric &&
!parameterHandler.Parameter_IsRequired(
originalParameterStack.last,
),
);
if (!needGeneric) {
outputBody = outputBody.concat(
ConstructValuation(
model,
required,
needGeneric,
prefixIndent,
originalParameterNameStack,
null,
'{}',
),
let newParams = [];
for (const flattenParam of flattenedTrace.slice().reverse()) {
if (originalParameterStack.last.schema === flattenParam) {
break;
}
const newParam = new Parameter(
flattenParam.language.python.name,
flattenParam.language.python.description,
flattenParam,
);
newParam.language['cli'] = flattenParam.language['cli'];
newParams = [newParam, ...newParams];
}

for (const newParam of newParams) {
originalParameterStack.push(newParam);
originalParameterNameStack.push(
newParam.schema.language.python.name,
);
originalParameterCheckEmpty.push(
!needGeneric &&
!parameterHandler.Parameter_IsRequired(
originalParameterStack.last,
),
);
if (!needGeneric) {
outputBody = outputBody.concat(
ConstructValuation(
model,
required,
needGeneric,
prefixIndent,
originalParameterNameStack,
null,
'{}',
),
);
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ export class CodeModelMerger {
// here we use a level traversal algorithm to traverse all the subnodes of the param node in python codemodel.
const OutLayerProp = [];
const cliFlattenTrace = param.language['cli'].cliM4Path;

const pythonFlattenedTrace = [];

// OutLayerProp is the queue in level traversal algorithm.
// each node of OutLayerProp is a pair, and the first element is the target traversal node, the second element is current cli flatten trace from the top to that node.
OutLayerProp.push([param.schema, cliFlattenTrace]);
Expand All @@ -201,6 +204,14 @@ export class CodeModelMerger {
if (isNullOrUndefined(outProp)) {
continue;
}
if (
preFlattenTrace ===
fnode.language?.cli?.cliFlattenTrace
.slice(0, pythonFlattenedTrace.length + 1)
.join(';')
) {
pythonFlattenedTrace.push(param);
}
for (const prop of getAllProperties(outProp)) {
if (foundProp) {
break;
Expand All @@ -214,6 +225,7 @@ export class CodeModelMerger {
) {
OutLayerProp.push([prop.schema, curFlattenTrace]);
}

// because flatten will delete the original schema if no other places have refer to that schema.
// in the case of flattened schema is not being deleted, and we can find the targeting node. we simply need to record language.python and pythonFlattenedFrom for future parameter construction when calling python SDK.
if (
Expand All @@ -231,6 +243,14 @@ export class CodeModelMerger {
} else if (
!isNullOrUndefined(fnode.language?.cli?.cliFlattenTrace)
) {
if (
curFlattenTrace ===
fnode.language?.cli?.cliFlattenTrace
.slice(0, pythonFlattenedTrace.length + 1)
.join(';')
) {
pythonFlattenedTrace.push(prop);
}
// in the case of flattened schema has been deleted, we need to use cli flatten trace to identify the flattened schema.
for (const trace of values(
fnode.language.cli.cliFlattenTrace,
Expand Down Expand Up @@ -261,6 +281,7 @@ export class CodeModelMerger {
}
}
}
fnode.language.cli.pythonFlattenedTrace = pythonFlattenedTrace;
}
}
}
Expand Down

0 comments on commit 67f4aa7

Please sign in to comment.