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

Commit

Permalink
1. handle circle for flattening
Browse files Browse the repository at this point in the history
2. put new operation of splite under original operation.extension
  • Loading branch information
RodgeFu committed Apr 4, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d743072 commit cf4c8da
Showing 4 changed files with 108 additions and 9 deletions.
43 changes: 42 additions & 1 deletion src/nodeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { codeModelSchema, ChoiceSchema, ChoiceValue, Extensions, CodeModel, ObjectSchema, Operation, OperationGroup, Parameter, Property, SealedChoiceSchema, Schema, ConstantSchema, SchemaType } from "@azure-tools/codemodel";
import { codeModelSchema, ChoiceSchema, ChoiceValue, Extensions, CodeModel, ObjectSchema, Operation, OperationGroup, Parameter, Property, SealedChoiceSchema, Schema, ConstantSchema, SchemaType, ArraySchema, DictionarySchema } from "@azure-tools/codemodel";
import { isArray, isNull, isNullOrUndefined, isObject, isString, isUndefined } from "util";
import { CliConst, M4Node, M4NodeType, NamingType, CliCommonSchema } from "./schema";
import { Dictionary } from "@azure-tools/codegen/node_modules/@azure-tools/linq";

export class NodeHelper {
private static readonly CLI: string = "cli";
@@ -11,7 +12,10 @@ export class NodeHelper {
private static readonly CLI_REMOVED: string = "removed";
private static readonly CLI_COMPLEXITY: string = "cli-complexity";
private static readonly CLI_SIMPLIFIER_INDICATOR: string = "cli-simplify-indicator";
private static readonly CLI_IN_CIRCLE: string = "cli-in-circle";
private static readonly CLI_MARK: string = "cli-mark";
private static readonly CLI_IS_VISIBLE: string = "cli-is-visible";
private static readonly CLI_OPERATIONS: string = "cli-operations";
private static readonly JSON: string = "json";
public static readonly FLATTEN_FLAG: string = 'x-ms-client-flatten';
public static readonly DISCRIMINATOR_FLAG: string = 'discriminator';
@@ -103,6 +107,10 @@ export class NodeHelper {
return isNullOrUndefined(node?.language[NodeHelper.CLI]) ? defaultValue : node.language[NodeHelper.CLI][NodeHelper.NAME];
}

public static getDefaultNameWithType(node: ObjectSchema | DictionarySchema | ArraySchema) {
return `${node.language.default.name}(${node instanceof ObjectSchema ? node.type : node instanceof DictionarySchema ? (node.elementType.language.default.name + '^dictionary') : (node.elementType.language.default.name + '^array')})`;
}

public static setHidden(node: M4Node, value: boolean) {
NodeHelper.setCliProperty(node, NodeHelper.CLI_HIDDEN, value);
}
@@ -271,4 +279,37 @@ export class NodeHelper {
public static clearSimplifyIndicator(schema: ObjectSchema) {
return this.clearCliProperty(schema, this.CLI_SIMPLIFIER_INDICATOR);
}

public static setInCircle(schema: ObjectSchema | ArraySchema | DictionarySchema, inCircle: boolean): boolean {
this.setCliProperty(schema, this.CLI_IN_CIRCLE, inCircle);
return inCircle;
}

public static getInCircle(schema: ObjectSchema | ArraySchema | DictionarySchema): boolean {
return this.getCliProperty(schema, this.CLI_IN_CIRCLE, () => undefined);
}

public static clearInCircle(schema: ObjectSchema | ArraySchema | DictionarySchema) {
return this.clearCliProperty(schema, this.CLI_IN_CIRCLE);
}


public static setMark(node: M4Node, mark: string): string {
NodeHelper.setCliProperty(node, NodeHelper.CLI_MARK, mark);
return mark;
}

public static getMark(node: M4Node): string {
return NodeHelper.getCliProperty(node, NodeHelper.CLI_MARK, () => undefined);
}

public static clearMark(node: M4Node) {
NodeHelper.clearCliProperty(node, NodeHelper.CLI_MARK);
}

public static addCliOperation(originalOperation: Operation, cliOperation: Operation) {
let v : Operation[] = NodeHelper.getExtensionsProperty(originalOperation, this.CLI_OPERATIONS, () => []);
v.push(cliOperation);
NodeHelper.setExtensionsProperty(originalOperation, this.CLI_OPERATIONS, v);
}
}
63 changes: 59 additions & 4 deletions src/plugins/complexMarker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Host, Session, startSession } from "@azure-tools/autorest-extension-base";
import { CodeModel, Request, codeModelSchema, Metadata, ObjectSchema, isObjectSchema, Property, Extensions, Scheme, ComplexSchema, Operation, OperationGroup, Parameter, VirtualParameter, ImplementationLocation, ArraySchema, DictionarySchema, AnySchema, ConstantSchema } from "@azure-tools/codemodel";
import { CodeModel, Request, codeModelSchema, Metadata, ObjectSchema, isObjectSchema, Property, Extensions, Scheme, ComplexSchema, Operation, OperationGroup, Parameter, VirtualParameter, ImplementationLocation, ArraySchema, DictionarySchema, AnySchema, ConstantSchema, getAllProperties } from "@azure-tools/codemodel";
import { isNullOrUndefined, isArray, isNull } from "util";
import { Helper } from "../helper";
import { CliConst, M4Node, CliCommonSchema } from "../schema";
import { Dumper } from "../dumper";
import { Dictionary, values } from '@azure-tools/linq';
import { values } from '@azure-tools/linq';
import { NodeHelper } from "../nodeHelper";
import { FlattenHelper } from "../flattenHelper";

@@ -128,7 +128,7 @@ class ComplexMarker {

NodeHelper.setSimplifyIndicator(schema, flag);

for (let p of schema.properties) {
for (let p of getAllProperties(schema)) {
if (p.readOnly)
continue;
if (p.schema instanceof ConstantSchema)
@@ -158,34 +158,89 @@ class ComplexMarker {
return NodeHelper.setSimplifyIndicator(schema, indicator);
}

public setInCircle(schema: ObjectSchema | DictionarySchema | ArraySchema, stack: (ObjectSchema | DictionarySchema | ArraySchema)[], tag: string) {

let flag = NodeHelper.getMark(schema);
if (!isNullOrUndefined(flag)) {
if (flag === tag) {
// we find a circle
let msg = "Circle Found: " + NodeHelper.getDefaultNameWithType(schema);
for (let i = stack.length - 1; i >= 0; i--) {
msg += '->' + NodeHelper.getDefaultNameWithType(stack[i]);
NodeHelper.setInCircle(stack[i], true);
if (stack[i] === schema)
break;
}
Helper.logDebug(msg);
}
else {
// we have been here before when iterating other schema
}
}
else {
NodeHelper.setMark(schema, tag);

if (schema instanceof ArraySchema || schema instanceof DictionarySchema) {
if (schema.elementType instanceof ObjectSchema ||
schema.elementType instanceof DictionarySchema ||
schema.elementType instanceof ArraySchema) {
stack.push(schema);
this.setInCircle(schema.elementType, stack, tag);
stack.splice(stack.length - 1, 1);
}
}
else if (schema instanceof ObjectSchema) {
for (let prop of getAllProperties(schema)) {
if (prop.schema instanceof ObjectSchema ||
prop.schema instanceof DictionarySchema ||
prop.schema instanceof ArraySchema) {
stack.push(schema);
this.setInCircle(prop.schema, stack, tag);
stack.splice(stack.length - 1, 1);
}
}
}
}
NodeHelper.setMark(schema, "checked");
}

public process() {

this.session.model.schemas.objects.forEach(obj => {
NodeHelper.clearComplex(obj);
NodeHelper.clearSimplifyIndicator(obj);
NodeHelper.clearMark(obj);
});

this.session.model.schemas.dictionaries?.forEach(dict => {
NodeHelper.clearComplex(dict);
NodeHelper.clearMark(dict);
});

this.session.model.schemas.arrays?.forEach(arr => {
NodeHelper.clearComplex(arr);
NodeHelper.clearMark(arr);
})

let tag = 1;
this.session.model.schemas.objects.forEach(obj => {
this.calculateObject(obj);
this.setSimplifyIndicator(obj);
this.setInCircle(obj, [], tag.toString());
tag++;
});

this.session.model.schemas.dictionaries?.forEach(dict => {
this.calculateDict(dict);
this.setInCircle(dict, [], tag.toString());
tag++;
});

this.session.model.schemas.arrays?.forEach(arr => {
this.calculateArray(arr);
this.setInCircle(arr, [], tag.toString());
tag++;
})

}
}

4 changes: 3 additions & 1 deletion src/plugins/flattenSetter/flattenSetter.ts
Original file line number Diff line number Diff line change
@@ -138,7 +138,9 @@ export class FlattenSetter {
if (prop.readOnly)
continue;
if (prop.schema instanceof ObjectSchema) {
if (!NodeHelper.HasSubClass(prop.schema) && NodeHelper.getComplexity(prop.schema) !== CliCommonSchema.CodeModel.Complexity.object_simple)
if (!NodeHelper.HasSubClass(prop.schema) &&
NodeHelper.getComplexity(prop.schema) !== CliCommonSchema.CodeModel.Complexity.object_simple &&
NodeHelper.getInCircle(prop.schema) !== true)
NodeHelper.setFlatten(prop, true, overwritten);
}
else if (prop.schema instanceof ArraySchema) {
7 changes: 4 additions & 3 deletions src/plugins/polyAsResourceModifier.ts
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ export class PolyAsResourceModifier {

// we need to modify the operations array, so get a copy of it first
let operations = g.operations.filter(op => op.requests?.length == 1);

operations.forEach(op => {

let request = getDefaultRequest(op);
@@ -145,8 +145,9 @@ export class PolyAsResourceModifier {
`${NodeHelper.getCliKey(op, op.language.default.name)}#${key}` /*cliKey*/,
`${NodeHelper.getCliName(op, op.language.default.name)}#${key}` /*cliName*/,
baseSchema, subClass);
g.addOperation(op2);

Helper.logDebug(`${g.language.default.name}/${op.language.default.name} cloned for subclass ${key}`);
NodeHelper.addCliOperation(op, op2);

let polyParam = NodeHelper.getPolyAsResourceParam(op2);
if (isNullOrUndefined(polyParam))
@@ -155,7 +156,7 @@ export class PolyAsResourceModifier {
let req = getDefaultRequest(op2);
if (NodeHelper.getJson(subClass) !== true) {
let path = isNullOrUndefined(polyParam['targetProperty']) ? [] : [polyParam['targetProperty']];
FlattenHelper.flattenParameter(req, polyParam, path, `${subClass.discriminatorValue}_`);
FlattenHelper.flattenParameter(req, polyParam, path, `${subClass.discriminatorValue.toLowerCase()}_`);
}
}

0 comments on commit cf4c8da

Please sign in to comment.