Skip to content

Commit

Permalink
tsp, support flavor flag from typespec config (#2609)
Browse files Browse the repository at this point in the history
* add flavor option and make nonbranding as default

* remove branded flag related logic

* update according to comments

* update version

* update version and format

* update according to comments

* format

* add package-dir option and add the related check logics

* format

* update according to comments

* update according to comments
  • Loading branch information
haolingdong-msft authored Mar 8, 2024
1 parent 74f51a7 commit 47cac99
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class JavaSettings {
private static final Map<String, Object> SIMPLE_JAVA_SETTINGS = new HashMap<>();
private static Logger logger;
private final boolean useKeyCredential;
private final boolean branded;
private final String flavor;
private final boolean noCustomHeaders;

static void setHeader(String value) {
Expand Down Expand Up @@ -175,7 +175,7 @@ public static JavaSettings getInstance() {
getBooleanValue(host, "use-key-credential", false),
getBooleanValue(host, "null-byte-array-maps-to-empty-array", false),
getBooleanValue(host, "graal-vm-config", false),
getBooleanValue(host, "branded", true)
getStringValue(host, "flavor", "Azure")
);
}
return instance;
Expand Down Expand Up @@ -264,7 +264,7 @@ public static JavaSettings getInstance() {
* @param nullByteArrayMapsToEmptyArray If set to true, {@code ArrayType.BYTE_ARRAY} will return an empty array
* instead of null when the default value expression is null.
* @param generateGraalVmConfig If set to true, the generated client will have support for GraalVM.
* @param branded Whether to generate with Azure branding.
* @param flavor The brand name we use to geneate SDK.
*/
private JavaSettings(AutorestSettings autorestSettings,
Map<String, Object> modelerSettings,
Expand Down Expand Up @@ -327,7 +327,7 @@ private JavaSettings(AutorestSettings autorestSettings,
boolean useKeyCredential,
boolean nullByteArrayMapsToEmptyArray,
boolean generateGraalVmConfig,
boolean branded) {
String flavor) {

this.autorestSettings = autorestSettings;
this.modelerSettings = new ModelerSettings(modelerSettings);
Expand Down Expand Up @@ -425,7 +425,7 @@ private JavaSettings(AutorestSettings autorestSettings,
this.useKeyCredential = useKeyCredential;
this.nullByteArrayMapsToEmptyArray = nullByteArrayMapsToEmptyArray;
this.generateGraalVmConfig = generateGraalVmConfig;
this.branded = branded;
this.flavor = flavor;
}

/**
Expand All @@ -434,7 +434,7 @@ private JavaSettings(AutorestSettings autorestSettings,
* @return Whether to generate with Azure branding.
*/
public boolean isBranded() {
return branded;
return "azure".equalsIgnoreCase(this.flavor);
}

private final String keyCredentialHeaderName;
Expand Down Expand Up @@ -519,6 +519,12 @@ public boolean urlAsString() {
}

private final boolean uuidAsString;

/**
* Whether to use string for uuid.
*
* @return Whether to use string for uuid.
*/
public boolean uuidAsString() {
return uuidAsString;
}
Expand Down
6 changes: 6 additions & 0 deletions typespec-extension/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

## 0.15.0 (2024-03-08)

Compatible with compiler 0.54.

- Supported "flavor" from typespec config.

## 0.14.1 (2024-03-07)

Compatible with compiler 0.54.
Expand Down
8 changes: 4 additions & 4 deletions typespec-extension/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typespec-extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/typespec-java",
"version": "0.14.1",
"version": "0.15.0",
"description": "TypeSpec library for emitting Java client from the TypeSpec REST protocol binding",
"keywords": [
"TypeSpec"
Expand Down
6 changes: 5 additions & 1 deletion typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class CodeModelBuilder {
// HTTP Authentication should use "Basic token" or "Bearer token"
schemeOrApiKeyPrefix = pascalCase(schemeOrApiKeyPrefix);

if (!(this.options.branded === false)) {
if (this.isBranded()) {
// Azure would not allow BasicAuth or BearerAuth
this.logWarning(`{scheme.scheme} auth method is currently not supported.`);
continue;
Expand All @@ -389,6 +389,10 @@ export class CodeModelBuilder {
}
}

private isBranded(): boolean {
return !this.options["flavor"] || this.options["flavor"].toLocaleLowerCase() === "azure";
}

private isInternal(context: SdkContext, operation: Operation): boolean {
const access = getAccess(operation);
if (access) {
Expand Down
15 changes: 13 additions & 2 deletions typespec-extension/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import { fileURLToPath } from "url";
export interface EmitterOptions {
"namespace"?: string;
"output-dir"?: string;
"package-dir"?: string;

"branded"?: boolean;
"flavor"?: string;

"service-name"?: string;
"service-versions"?: string[];
Expand Down Expand Up @@ -59,8 +60,9 @@ const EmitterOptionsSchema: JSONSchemaType<EmitterOptions> = {
properties: {
"namespace": { type: "string", nullable: true },
"output-dir": { type: "string", nullable: true },
"package-dir": { type: "string", nullable: true },

"branded": { type: "boolean", nullable: true, default: true },
"flavor": { type: "string", nullable: true, default: "Azure" },

// service
"service-name": { type: "string", nullable: true },
Expand Down Expand Up @@ -106,6 +108,15 @@ export const $lib = createTypeSpecLibrary({
export async function $onEmit(context: EmitContext<EmitterOptions>) {
const program = context.program;
const options = context.options;
if (!options["flavor"]) {
if (options["package-dir"]?.toLocaleLowerCase().startsWith("azure")) {
// Azure package
options["flavor"] = "Azure";
} else {
// default
options["flavor"] = "Azure";
}
}
const builder = new CodeModelBuilder(program, context);
const codeModel = await builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ public TypeSpecPlugin(EmitterOptions options, boolean sdkIntegration) {
SETTINGS_MAP.put("polling", options.getPolling());
}

if (options.getBranded() == Boolean.FALSE) {
SETTINGS_MAP.put("branded", options.getBranded());
if (options.getFlavor() != null) {
SETTINGS_MAP.put("flavor", options.getFlavor());
}

if (options.getFlavor() != null && !"azure".equalsIgnoreCase(options.getFlavor())) {
SETTINGS_MAP.put("sdk-integration", false);
SETTINGS_MAP.put("license-header", "SMALL_TYPESPEC");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class EmitterOptions {
@JsonProperty(value="output-dir")
private String outputDir;

@JsonProperty(value = "branded")
private Boolean branded = true;
@JsonProperty(value = "flavor")
private String flavor = "Azure";

@JsonDeserialize(using = EmptyStringToNullDeserializer.class)
@JsonProperty(value="service-name")
Expand Down Expand Up @@ -146,10 +146,6 @@ public void setPolling(Map<String, JavaSettings.PollingDetails> polling) {
this.polling = polling;
}

public Boolean getBranded() {
return branded;
}

public Boolean getArm() {
return arm;
}
Expand All @@ -158,6 +154,10 @@ public String getModelsSubpackage() {
return modelsSubpackage;
}

public String getFlavor() {
return flavor;
}

public static class EmptyStringToNullDeserializer extends JsonDeserializer<String> {

@Override
Expand Down
2 changes: 1 addition & 1 deletion typespec-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@azure-tools/cadl-ranch-specs": "0.31.2",
"@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.14.1.tgz"
"@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.0.tgz"
},
"devDependencies": {
"@typespec/prettier-plugin-typespec": "~0.54.0",
Expand Down
1 change: 1 addition & 0 deletions typespec-tests/tspconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ options:
generate-samples: true
generate-tests: true
examples-directory: "{project-root}/tsp/examples"
flavor: "Azure"
dev-options:
generate-code-model: true
support-versioning: true
Expand Down

0 comments on commit 47cac99

Please sign in to comment.