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

Commit

Permalink
Polymorphism split multiple times (#113)
Browse files Browse the repository at this point in the history
* polymorphism split multiple times

* poly as resource

* polymorphism fix

* reserve work

* reserve work

* mark discriminator value after poly flatten

* some fixes of cli operations references

* try fix test

* poly gen for tree

set poly resourced

* version 0.6.2

* clean code

Co-authored-by: Changlong Liu <changlong.liu@microsoft.com>
  • Loading branch information
qiaozha and changlong-liu authored May 27, 2021
1 parent 8074010 commit 24a9313
Show file tree
Hide file tree
Showing 81 changed files with 115,738 additions and 31 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,28 @@ pipeline:
input: clicommon/cli-flatten-setter
output-artifact: clicommon-flatten-modifier-pre

clicommon/cli-poly-as-resource-modifier:
clicommon/pre/cli-poly-as-resource-modifier:
input: clicommon/pre/cli-flatten-modifier
output-artifact: clicommon-poly-as-resource-modifier
output-artifact: clicommon-poly-as-resource-modifier-pre

clicommon/cli-flatten-modifier:
input: clicommon/cli-poly-as-resource-modifier
input: clicommon/pre/cli-poly-as-resource-modifier
output-artifact: clicommon-flatten-modifier

clicommon/cli-modifier:
clicommon/cli-poly-as-resource-modifier:
input: clicommon/cli-flatten-modifier
output-artifact: clicommon-poly-as-resource-modifier

clicommon/post/cli-flatten-modifier:
input: clicommon/cli-poly-as-resource-modifier
output-artifact: clicommon-flatten-modifier-post

clicommon/cli-modifier:
input: clicommon/post/cli-flatten-modifier
output-artifact: clicommon-modifier-output



clicommon/cli-namer:
input: clicommon/cli-modifier
output-artifact: clicommon-namer-output
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@autorest/clicommon",
"version": "0.6.1",
"version": "0.6.2",
"description": "Autorest Azure Cli Common Module",
"main": "dist/index.js",
"engines": {
Expand Down
6 changes: 5 additions & 1 deletion src/copyHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Parameter, Operation, Schema, Languages } from '@azure-tools/codemodel';
import { isNullOrUndefined } from 'util';
import { NodeCliHelper } from './nodeHelper';
import { NodeCliHelper, NodeExtensionHelper } from './nodeHelper';

export class CopyHelper {
public static copyOperation(source: Operation, globalParameters?: Parameter[], customizedReqCopy?: (req: Request) => Request, customizedParamCopy?: (srcParam: Parameter) => Parameter): Operation {
Expand Down Expand Up @@ -45,6 +45,10 @@ export class CopyHelper {
}
}

if (!isNullOrUndefined(NodeExtensionHelper.getCliDiscriminatorValue(source))) {
NodeExtensionHelper.setCliDiscriminatorValue(copy, NodeExtensionHelper.getCliDiscriminatorValue(source));
}

return copy;
}

Expand Down
3 changes: 3 additions & 0 deletions src/flattenHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export class FlattenHelper {
vp.language.default.name = FlattenHelper.createFlattenedParameterDefaultName(property, prefix);
NodeExtensionHelper.setCliFlattenOrigin(vp, property);
NodeExtensionHelper.setCliFlattenPrefix(vp, prefix);
if (property.isDiscriminator) {
NodeExtensionHelper.setCliDiscriminatorValue(vp, parameter.schema['discriminatorValue']);
}
arr.push(vp);

// if prop is inherit, update path
Expand Down
3 changes: 2 additions & 1 deletion src/flattener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ export class Flattener {

private isCliOperation(desc: CliCommonSchema.CodeModel.NodeDescriptor): boolean {
// CliOperation is not in group.operations. So its index is equal or bigger than operation array(desc.parent)'s length
return desc.targetIndex >= desc.parent.length;
const operation = desc.target as Operation;
return !isNullOrUndefined(NodeExtensionHelper.getPolyAsResourceOriginalOperation(operation));
}

private fixPropertyCollisions() {
Expand Down
3 changes: 0 additions & 3 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,11 @@ export class Helper {

// collect operations in cli-operations
const operations = [];
const cliOps = [];
group.operations.forEach((op) => {
operations.push(op);
cliOps.push(...NodeExtensionHelper.getCliOperation((op), () => []));
});

// put all cli operations at the end of array. So we can use targetIndex and parent.length to know whehter this operation is in cli.
operations.push(...cliOps);

for (let j = operations.length - 1; j >= 0; j--) {
const op = operations[j];
Expand Down
30 changes: 19 additions & 11 deletions src/nodeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class NodeCliHelper {

// TODO: Consider add specific class for directive keys
public static readonly POLY_RESOURCE: string = 'poly-resource';
public static readonly POLY_RESOURCED: string = 'poly-resourced';
public static readonly CLI_FLATTEN: string = 'cli-flatten';
public static readonly CLI_FLATTENED: string = 'cli-flattened';
public static readonly CLI_M4_FLATTEN: string = 'cli-m4-flatten';
Expand Down Expand Up @@ -179,6 +180,14 @@ export class NodeCliHelper {
return NodeCliHelper.getCliProperty(node, NodeCliHelper.POLY_RESOURCE, () => false);
}

public static isPolyAsResourced(node: Parameter): boolean {
return NodeCliHelper.getCliProperty(node, NodeCliHelper.POLY_RESOURCED, () => false);
}

public static setPolyAsResourced(node: Parameter, value: boolean): void {
return NodeCliHelper.setCliProperty(node, NodeCliHelper.POLY_RESOURCED, value);
}

public static setPolyAsParamExpanded(param: Parameter, value: boolean): void {
NodeCliHelper.setCliProperty(param, NodeCliHelper.POLY_AS_PARAM_EXPANDED, value);
}
Expand Down Expand Up @@ -313,6 +322,7 @@ export class NodeExtensionHelper {

private static readonly CLI_FLATTEN_ORIGIN = 'cli-flatten-origin';
private static readonly CLI_FLATTEN_PREFIX = 'cli-flatten-prefix';
private static readonly CLI_DISCRIMINATOR_VALUE = 'cli-discriminator-value';

/**
* return the value of node.extensions['x-ms-client-flatten']
Expand Down Expand Up @@ -341,6 +351,14 @@ export class NodeExtensionHelper {
return NodeExtensionHelper.getExtensionsProperty(param, NodeExtensionHelper.CLI_FLATTEN_PREFIX, () => null);
}

public static setCliDiscriminatorValue(node: M4Node, value: string): void{
return NodeExtensionHelper.setExtensionsProperty(node, NodeExtensionHelper.CLI_DISCRIMINATOR_VALUE, value);
}

public static getCliDiscriminatorValue(param: Parameter): string {
return NodeExtensionHelper.getExtensionsProperty(param, NodeExtensionHelper.CLI_DISCRIMINATOR_VALUE, () => null);
}

public static setPolyAsResourceParam(op: Operation, polyParam: Parameter): void {
NodeExtensionHelper.setExtensionsProperty(op, NodeExtensionHelper.POLY_AS_RESOURCE_SUBCLASS_PARAM, polyParam);
}
Expand All @@ -361,7 +379,7 @@ export class NodeExtensionHelper {
NodeExtensionHelper.setExtensionsProperty(op, NodeExtensionHelper.POLY_AS_RESOURCE_ORIGINAL_OPERATION, ori);
}

public static getPolyAsResourceOriginalOperation(op: Operation): Schema {
public static getPolyAsResourceOriginalOperation(op: Operation): Operation {
return NodeExtensionHelper.getExtensionsProperty(op, NodeExtensionHelper.POLY_AS_RESOURCE_ORIGINAL_OPERATION, null);
}

Expand Down Expand Up @@ -397,16 +415,6 @@ export class NodeExtensionHelper {
return NodeExtensionHelper.getExtensionsProperty(param, NodeExtensionHelper.POLY_AS_PARAM_ORIGINIAL_PARAMETER, null);
}

public static addCliOperation(originalOperation: Operation, cliOperation: Operation): void {
const v: Operation[] = NodeExtensionHelper.getExtensionsProperty(originalOperation, NodeExtensionHelper.CLI_OPERATIONS, () => []);
v.push(cliOperation);
NodeExtensionHelper.setExtensionsProperty(originalOperation, NodeExtensionHelper.CLI_OPERATIONS, v);
}

public static getCliOperation(originalOperation: Operation, defaultValue: () => Operation[]): Operation[] {
return NodeExtensionHelper.getExtensionsProperty(originalOperation, NodeExtensionHelper.CLI_OPERATIONS, defaultValue);
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
public static setExtensionsProperty(node: M4Node, key: string, value: any): void {
if (isNullOrUndefined(node.extensions))
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/namer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ export class CommonNamer {
// To be backward compatiable, reassign poly operations and parameters' default name and cli name
for (const operationGroup of values(this.codeModel.operationGroups)) {
for (const operation of values(operationGroup.operations)) {
for (const op of values(NodeExtensionHelper.getCliOperation(operation, () => []))) {
this.applyNamingConventionOnCliOperation(operation, op);
for (const parameter of values(op.parameters)) {
const polyOriginOp = NodeExtensionHelper.getPolyAsResourceOriginalOperation(operation);
if (!isNullOrUndefined(polyOriginOp)) {
this.applyNamingConventionOnCliOperation(polyOriginOp, operation);
for (const parameter of values(operation.parameters)) {
this.applyNamingConvention(parameter);
}

for (const request of values(op.requests)) {
for (const request of values(operation.requests)) {
if (!isNullOrUndefined(request.parameters)) {
for (const parameter of values(request.parameters)) {
this.applyNamingConventionOnCliParameter(parameter);
Expand Down
27 changes: 21 additions & 6 deletions src/plugins/polyAsResourceModifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Host, Session, startSession } from "@azure-tools/autorest-extension-base";
import { CodeModel, Request, ObjectSchema, Operation, OperationGroup, Parameter, codeModelSchema } from "@azure-tools/codemodel";
import { CodeModel, Request, ObjectSchema, Operation, OperationGroup, Parameter, codeModelSchema, getAllProperties } from "@azure-tools/codemodel";
import { isNullOrUndefined } from "util";
import { Helper } from "../helper";
import { NodeHelper, NodeCliHelper, NodeExtensionHelper } from "../nodeHelper";
Expand Down Expand Up @@ -93,22 +93,37 @@ export class PolyAsResourceModifier {
return;
}
if (allPolyParam.length > 1) {
throw Error('multiple polymorphism parameter as resource found: ' + allPolyParam.map(p => p.language['cli']));
throw Error('multiple polymorphism parameter as resource found: op: ' + NodeCliHelper.getCliKey(op, null) + ' dup parameters: ' + allPolyParam.map(p => NodeCliHelper.getCliKey(p, null)));
}

const polyParam = allPolyParam[0];
const baseSchema = polyParam.schema as ObjectSchema;

for (const subClass of NodeHelper.getSubClasses(baseSchema, true)) {
const baseDiscriminatorValue = NodeCliHelper.getCliDiscriminatorValue(baseSchema);
const subClasses = NodeHelper.getSubClasses(baseSchema, false);

if (!isNullOrUndefined(baseDiscriminatorValue) && NodeCliHelper.isPolyAsResourced(polyParam)) {
return;
}

for (const subClass of subClasses) {

const discriminatorValue = NodeCliHelper.getCliDiscriminatorValue(subClass);

const op2: Operation = this.cloneOperationForSubclass(op, baseSchema, subClass);

Helper.logDebug(this.session, `${g.language.default.name}/${op.language.default.name} cloned for subclass ${discriminatorValue}`);
NodeExtensionHelper.addCliOperation(op, op2);
if (g.operations.indexOf(op2) === -1) {
g.operations.push(op2);
for (const p2 of this.findPolyParameters(this.getDefaultRequest(op2))) {
if (NodeCliHelper.isPolyAsResource(p2) && NodeExtensionHelper.getPolyAsResourceBaseSchema(p2) === baseSchema) {
NodeCliHelper.setPolyAsResourced(p2, true);
}
}
}
}

NodeCliHelper.setPolyAsResourced(polyParam, true);

NodeCliHelper.setHidden(op, true);
});
});
Expand All @@ -126,7 +141,7 @@ export class PolyAsResourceModifier {
return [];
}
return request.parameters?.filter(p =>
Helper.isObjectSchema(p.schema) && (p.schema as ObjectSchema).discriminator && NodeCliHelper.isPolyAsResource(p));
Helper.isObjectSchema(p.schema) && (p.schema as ObjectSchema).discriminator && NodeCliHelper.isPolyAsResource(p) && !NodeCliHelper.isPolyAsResourced(p));
}

private getDefaultRequest(operation: Operation): Request {
Expand Down
Loading

0 comments on commit 24a9313

Please sign in to comment.