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

Commit

Permalink
Add flatten param (#67)
Browse files Browse the repository at this point in the history
* copy split directive

* support flatten param

* make poly operation has same default name/cli name as before

* fix namer

* fix comment

* refactor helper

* add cli-split-operation-extend-poly-resource

Co-authored-by: xichen <xichen@microsoft.com>
  • Loading branch information
shawncx and msxichen authored Jun 15, 2020
1 parent 7e273e5 commit 3f66ab5
Show file tree
Hide file tree
Showing 23 changed files with 1,136 additions and 585 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ pipeline-model: v3

pipeline:

modelerfour/new-transform:
input: clicommon/cli-flatten-setter
clicommon/cli-prenamer:
input: modelerfour
output-artifact: clicommon-prenamer
Expand All @@ -28,17 +25,28 @@ pipeline:
clicommon/cli-flatten-setter:
input: clicommon/pre/cli-complex-marker
output-artifact: clicommon-flatten-setter

modelerfour/new-transform:
input: clicommon/cli-flatten-setter

clicommon:
clicommon/cli-modeler-post-processor:
input: modelerfour/identity
output-artifact: clicommon-output
output-artifact: clicommon-modeler-post-processor

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

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

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

clicommon/cli-complex-marker:
input: clicommon
output-artifact: clicommon-complex-marker

#clicommon/cli-poly-as-param-modifier:
Expand All @@ -58,8 +66,10 @@ pipeline:
- clicommon/cli-prenamer
- clicommon/cli-split-operation
- clicommon/cli-flatten-setter
- clicommon/cli-modeler-post-processor
#- clicommon/cli-poly-as-param-modifier
- clicommon/cli-poly-as-resource-modifier
- clicommon/cli-flatten-modifier
- clicommon/cli-complex-marker
- clicommon/pre/cli-complex-marker
- clicommon/cli-visibility-cleaner
Expand All @@ -72,16 +82,19 @@ scope-clicommon:
- clicommon-prenamer
- clicommon-split-operation
- clicommon-flatten-setter
- clicommon-modeler-post-processor
- clicommon-poly-as-resource-modifier
- clicommon-flatten-modifier
#- clicommon-poly-as-param-modifier
- clicommon-complex-marker
- clicommon-complex-marker-pre
- clicommon-visibility-cleaner

modelerfour:
#group-parameters: true
#flatten-models: true
#flatten-payloads: true
# group-parameters: true
# flatten-models: true
# flatten-payloads: true
# lenient-model-deduplication: true

# standardize to snake in modelerfour for selecting and formatting in clicommon
# further naming will be done in clicommon to corresonding convention
Expand Down Expand Up @@ -147,6 +160,8 @@ cli:
# 2. If operation with split name has already existed in operation group, you will get
# a warning and this split name will be skipped.
cli-split-operation-enabled: true
# if ture, `poly-resource` on the parameter will be extended by splitted operations
cli-split-operation-extend-poly-resource: true
polymorphism:
# if true, polymorphism parameter with 'poly-resource' marked as true will be
# expanded into multiple operations for each subclasses
Expand Down Expand Up @@ -205,6 +220,3 @@ cli:
choiceValue: 'pascal'
constant: 'pascal'
```
12 changes: 12 additions & 0 deletions doc/cli-directive.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ For groupName, operationName, parameterName, typeName, propertyName, usually you
- isRegex: true | false
- split-operation-names
- split operation into multiple operations with given names
- add 'split-operation-names" under 'language->cli'
- optional
- value format:
- opName1
- opName2
- ...
- cli-flatten
- flatten object
- add 'cli-flatten: ..." under 'language->cli'
- optional

## How to troubleshooting
> Add --debug in your command line to have more intermedia output files for troubleshooting
Expand Down Expand Up @@ -203,6 +209,12 @@ cli:
split-operation-names:
- Create
- Update
# flatten parameter
- where:
group: OperationGroupName
op: CreateOrUpdate#Update
param: properties
cli-flatten: true
```

2 changes: 1 addition & 1 deletion src/copyHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CopyHelper {
const copy = new Request(source);
copy.extensions = CopyHelper.copy(source.extensions);
copy.language = CopyHelper.deepCopy(source.language);
copy.parameters = copy.parameters?.map((p) => customizedParamCopy == null ? CopyHelper.copyParameter(p) : customizedParamCopy(p));
copy.parameters = source.parameters?.map((p) => customizedParamCopy == null ? CopyHelper.copyParameter(p) : customizedParamCopy(p));
copy.updateSignatureParameters();
return copy;
}
Expand Down
36 changes: 24 additions & 12 deletions src/flattenHelper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { getAllProperties, ImplementationLocation, ObjectSchema, Parameter, Property, Request, VirtualParameter } from "@azure-tools/codemodel";
import { values } from "@azure-tools/linq";
import { isNull, isNullOrUndefined } from "util";
import { isNullOrUndefined } from "util";
import { NodeHelper, NodeExtensionHelper, NodeCliHelper } from "./nodeHelper";

export class FlattenHelper {

public static flattenParameter(req: Request, param: Parameter, path: Property[], prefix: string) {
if (!(param.schema instanceof ObjectSchema))
throw Error(`Try to flatten non-object schema: param = '${param.language.default.name}', schema= '${param.schema.language.default.name}'`);

FlattenHelper.flattenPorperties(req, param, param.schema as ObjectSchema, path, prefix);
req.updateSignatureParameters();
}

public static createFlattenedParameterDefaultName(baseProperty: Property, prefix: string): string {
return `${prefix}_${baseProperty.language.default.name}`;
}

public static createFlattenedParameterCliName(baseProperty: Property, prefix: string): string {
return `${prefix}_${baseProperty.language['cli'].name}`;
}

private static *getFlattenedParameters(parameter: Parameter, property: Property, path: Array<Property> = []): Iterable<VirtualParameter> {
if (property.readOnly) {
// skip read-only properties
Expand Down Expand Up @@ -33,6 +50,8 @@ export class FlattenHelper {
private static flattenPorperties(request: Request, parameter: Parameter, schema: ObjectSchema, path: Property[], prefix: string) {
// hide the original parameter
parameter.flattened = true;
NodeExtensionHelper.setCliFlattened(parameter, true);

// we need this for the further flatten be recognized by python codegen
let protocal: any = {
http: {
Expand All @@ -49,8 +68,10 @@ export class FlattenHelper {
continue;
}
for (const vp of this.getFlattenedParameters(parameter, property, path)) {
vp.language.default.name = `${prefix}${vp.language.default.name}`;
vp.language['cli'].name = `${prefix}${vp.language['cli'].name}`;
vp.language.default.name = FlattenHelper.createFlattenedParameterDefaultName(property, prefix);
NodeCliHelper.setCliFlattenedNames(vp, [NodeCliHelper.getCliKey(parameter, null), NodeCliHelper.getCliKey(property, null)]);
NodeExtensionHelper.setCliFlattenOrigin(vp, property);
NodeExtensionHelper.setCliFlattenPrefix(vp, prefix);
arr.push(vp);
}
}
Expand All @@ -72,13 +93,4 @@ export class FlattenHelper {
request.parameters = request.parameters.concat(arr2);
}
}


public static flattenParameter(req: Request, param: Parameter, path: Property[], prefix: string) {
if (!(param.schema instanceof ObjectSchema))
throw Error(`Try to flatten non-object schema: param = '${param.language.default.name}', schema= '${param.schema.language.default.name}'`);

FlattenHelper.flattenPorperties(req, param, param.schema as ObjectSchema, path, prefix);
req.updateSignatureParameters();
}
}
Loading

0 comments on commit 3f66ab5

Please sign in to comment.