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

Commit

Permalink
normalize discriminator value
Browse files Browse the repository at this point in the history
  • Loading branch information
RodgeFu committed Apr 4, 2020
1 parent f102e28 commit 126f27e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ cli:
appliedTo:
- name
- alias
- discriminatorValue
singularize:
- operationGroup
- operation
Expand Down
5 changes: 5 additions & 0 deletions src/nodeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class NodeHelper {
private static readonly JSON: string = "json";
public static readonly FLATTEN_FLAG: string = 'x-ms-client-flatten';
public static readonly DISCRIMINATOR_FLAG: string = 'discriminator';
public static readonly CLI_DISCRIMINATOR_VALUE: string = 'discriminatorValue';
public static readonly POLY_RESOURCE: string = 'poly-resource';
private static readonly POLY_AS_RESOURCE_SUBCLASS_PARAM = "cli-poly-as-resource-subclass-param";
private static readonly POLY_AS_RESOURCE_BASE_SCHEMA = 'cli-poly-as-resource-base-schema';
Expand All @@ -36,6 +37,10 @@ export class NodeHelper {
return !isNullOrUndefined(node.discriminator);
}

public static GetDiscriminatorValue(node: ObjectSchema) {
return NodeHelper.getCliProperty(node, this.CLI_DISCRIMINATOR_VALUE, () => node.discriminatorValue);
}

public static setJson(node: M4Node, isJson: boolean, modifyFlatten: boolean) {

if (modifyFlatten && isJson) {
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/namer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export class CommonNamer {
if (isNullOrUndefined(obj.language['cli']['description']))
obj.language['cli']['description'] = obj.language.default.description;

if (!isNullOrUndefined(obj['discriminatorValue'])) {
let dv: string = obj['discriminatorValue'];
// dv should be in pascal format, let's do a simple convert to snake
let newValue = dv.replace(/([A-Z][a-z0-9]+)|([A-Z]+(?=[A-Z][a-z0-9]+))|([A-Z]+$)/g, '_$1$2$3').substr(1).toLowerCase();
obj.language.cli.discriminatorValue = newValue;
}

let lan = 'cli';
if (!this.flag.has(obj.language[lan])) {
this.flag.add(obj.language[lan]);
Expand Down
11 changes: 6 additions & 5 deletions src/plugins/polyAsResourceModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ export class PolyAsResourceModifier {
Helper.logWarning("subclass is not ObjectSchema: " + subClass.language.default.name);
continue;
}
let discriminatorValue = NodeHelper.GetDiscriminatorValue(subClass);
if (NodeHelper.HasSubClass(subClass)) {
Helper.logWarning("skip subclass which also has subclass: " + subClass.language.default.name);
continue;
}

let op2: Operation = this.cloneOperationForSubclass(op,
`${op.language.default.name}_${key}` /*defaultName*/,
`${NodeHelper.getCliKey(op, op.language.default.name)}#${key}` /*cliKey*/,
`${NodeHelper.getCliName(op, op.language.default.name)}#${key}` /*cliName*/,
`${op.language.default.name}_${discriminatorValue}` /*defaultName*/,
`${NodeHelper.getCliKey(op, op.language.default.name)}#${discriminatorValue}` /*cliKey*/,
`${NodeHelper.getCliName(op, op.language.default.name)}#${discriminatorValue}` /*cliName*/,
baseSchema, subClass);

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

let polyParam = NodeHelper.getPolyAsResourceParam(op2);
Expand All @@ -156,7 +157,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, `${discriminatorValue}_`);
}
}

Expand Down

0 comments on commit 126f27e

Please sign in to comment.