Skip to content

Commit

Permalink
tsp, support EnumMember as type (#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Aug 16, 2023
1 parent 1bf4ef6 commit 2a3e3c2
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public Client map(CodeModel codeModel) {
if (!packageInfos.containsKey(customTypesPackage)) {
packageInfos.put(customTypesPackage, new PackageInfo(
customTypesPackage,
String.format("Package containing classes for %s.\n%s", serviceClientName,
String.format("Package containing the data models for %s.\n%s", serviceClientName,
serviceClientDescription)));
}
}
Expand Down
4 changes: 3 additions & 1 deletion typespec-extension/changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Release History

## 0.8.9 (Unreleased)
## 0.8.9 (2023-08-16)

Compatible with compiler 0.47.

- Supported `EnumMember` as type.

## 0.8.8 (2023-07-21)

Compatible with compiler 0.46.
Expand Down
27 changes: 22 additions & 5 deletions typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
getEncode,
getOverloadedOperation,
isErrorModel,
EnumMember,
} from "@typespec/compiler";
import { getResourceOperation, getSegment } from "@typespec/rest";
import {
Expand Down Expand Up @@ -1383,14 +1384,14 @@ export class CodeModelBuilder {
}

case "String":
return this.processChoiceSchemaForLiteral(type, nameHint);
return this.processConstantSchemaForLiteral(type, nameHint);

case "Number":
// TODO: float
return this.processChoiceSchemaForLiteral(type, nameHint);
return this.processConstantSchemaForLiteral(type, nameHint);

case "Boolean":
return this.processChoiceSchemaForLiteral(type, nameHint);
return this.processConstantSchemaForLiteral(type, nameHint);

case "Enum":
return this.processChoiceSchema(type, this.getName(type), isFixed(this.program, type));
Expand Down Expand Up @@ -1422,6 +1423,10 @@ export class CodeModelBuilder {
} else {
return this.processObjectSchema(type, this.getName(type));
}

case "EnumMember":
// e.g. "type: TypeEnum.EnumValue1"
return this.processConstantSchemaForEnumMember(type, this.getName(type));
}
throw new Error(`Unrecognized type: '${type.kind}'.`);
}
Expand Down Expand Up @@ -1635,7 +1640,7 @@ export class CodeModelBuilder {
}
}

private processChoiceSchemaForLiteral(
private processConstantSchemaForLiteral(
type: StringLiteral | NumericLiteral | BooleanLiteral,
name: string,
): ConstantSchema {
Expand All @@ -1651,6 +1656,18 @@ export class CodeModelBuilder {
);
}

private processConstantSchemaForEnumMember(type: EnumMember, name: string): ConstantSchema {
const valueType = this.processChoiceSchema(type.enum, this.getName(type.enum), isFixed(this.program, type.enum));

return this.codeModel.schemas.add(
new ConstantSchema(name, this.getDoc(type), {
summary: this.getSummary(type),
valueType: valueType,
value: new ConstantValue(type.value ?? type.name),
}),
);
}

private processChoiceSchemaForUnion(type: Union, variants: UnionVariant[], name: string): ChoiceSchema {
const kind = variants[0].type.kind;
const valueType =
Expand Down Expand Up @@ -2093,7 +2110,7 @@ export class CodeModelBuilder {
return getSummary(this.program, target);
}

private getName(target: Model | Enum | ModelProperty | Scalar | Operation): string {
private getName(target: Model | Enum | EnumMember | ModelProperty | Scalar | Operation): string {
// TODO: once getLibraryName API in typespec-client-generator-core can get projected name from language and client, as well as can handle template case, use getLibraryName API
const languageProjectedName = getProjectedName(this.program, target, "java");
if (languageProjectedName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public Mono<Response<String>> getColorModelWithResponse(RequestOptions requestOp
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -130,6 +133,9 @@ public Mono<Response<BinaryData>> setColorModelWithResponse(String color, Reques
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -159,6 +165,9 @@ public Mono<Response<BinaryData>> setPriorityWithResponse(String priority, Reque
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -187,6 +196,9 @@ public Mono<Response<BinaryData>> getRunningOperationWithResponse(RequestOptions
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public Response<String> getColorModelWithResponse(RequestOptions requestOptions)
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -128,6 +131,9 @@ public Response<BinaryData> setColorModelWithResponse(String color, RequestOptio
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -157,6 +163,9 @@ public Response<BinaryData> setPriorityWithResponse(String priority, RequestOpti
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -185,6 +194,9 @@ public Response<BinaryData> getRunningOperationWithResponse(RequestOptions reque
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,9 @@ public Response<String> getColorModelWithResponse(RequestOptions requestOptions)
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -819,6 +822,9 @@ public Mono<Response<BinaryData>> setColorModelWithResponseAsync(String color, R
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -848,6 +854,9 @@ public Response<BinaryData> setColorModelWithResponse(String color, RequestOptio
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -878,6 +887,9 @@ public Mono<Response<BinaryData>> setPriorityWithResponseAsync(String priority,
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -907,6 +919,9 @@ public Response<BinaryData> setPriorityWithResponse(String priority, RequestOpti
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -937,6 +952,9 @@ public Mono<Response<BinaryData>> getRunningOperationWithResponseAsync(RequestOp
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -966,6 +984,9 @@ public Response<BinaryData> getRunningOperationWithResponse(RequestOptions reque
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down Expand Up @@ -996,6 +1017,9 @@ public Mono<Response<BinaryData>> getOperationWithResponseAsync(String state, Re
* age: long (Required)
* priority: String(100/0) (Required)
* color: String(Red/Blue/Green) (Required)
* priorityValue: String(100/0) (Required)
* colorValue: String(Red/Blue/Green) (Required)
* colorModelValue: String(Red/Blue/Green) (Required)
* }
* }</pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ public final class Operation {
@JsonProperty(value = "color")
private ColorModel color;

/*
* The priorityValue property.
*/
@Generated
@JsonProperty(value = "priorityValue")
private Priority priorityValue = Priority.LOW;

/*
* The colorValue property.
*/
@Generated
@JsonProperty(value = "colorValue")
private Color colorValue = Color.GREEN;

/*
* The colorModelValue property.
*/
@Generated
@JsonProperty(value = "colorModelValue")
private ColorModel colorModelValue = ColorModel.BLUE;

/**
* Creates an instance of Operation class.
*
Expand All @@ -62,6 +83,9 @@ private Operation(
@JsonProperty(value = "color") ColorModel color) {
best = true;
age = 50L;
priorityValue = Priority.LOW;
colorValue = Color.GREEN;
colorModelValue = ColorModel.BLUE;
this.name = name;
this.priority = priority;
this.color = color;
Expand Down Expand Up @@ -116,4 +140,34 @@ public Priority getPriority() {
public ColorModel getColor() {
return this.color;
}

/**
* Get the priorityValue property: The priorityValue property.
*
* @return the priorityValue value.
*/
@Generated
public Priority getPriorityValue() {
return this.priorityValue;
}

/**
* Get the colorValue property: The colorValue property.
*
* @return the colorValue value.
*/
@Generated
public Color getColorValue() {
return this.colorValue;
}

/**
* Get the colorModelValue property: The colorModelValue property.
*
* @return the colorModelValue value.
*/
@Generated
public ColorModel getColorModelValue() {
return this.colorModelValue;
}
}
3 changes: 3 additions & 0 deletions typespec-tests/tsp/enum.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ model Operation {
age: 50;
priority: Priority;
color: ColorModel;
priorityValue: Priority.Low;
colorValue: Color.Green;
colorModelValue: ColorModel.Blue;
}

@route("/enum")
Expand Down

0 comments on commit 2a3e3c2

Please sign in to comment.