Skip to content

Commit

Permalink
use namespace from code-model
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Nov 11, 2024
1 parent c7dc05a commit e6731bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
50 changes: 45 additions & 5 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class CodeModelBuilder {
private program: Program;
private typeNameOptions: TypeNameOptions;
private namespace: string;
private baseJavaNamespace: string;
private sdkContext!: SdkContext;
private options: EmitterOptions;
private codeModel: CodeModel;
Expand Down Expand Up @@ -206,7 +207,7 @@ export class CodeModelBuilder {

this.namespace = getNamespaceFullName(this.serviceNamespace) || "Azure.Client";
// java namespace
const javaNamespace = this.getJavaNamespace(this.namespace);
this.baseJavaNamespace = this.getJavaNamespace(this.namespace)!;

const namespace1 = this.namespace;
this.typeNameOptions = {
Expand All @@ -233,7 +234,7 @@ export class CodeModelBuilder {
namespace: this.namespace,
},
java: {
namespace: javaNamespace,
namespace: this.baseJavaNamespace,
},
},
});
Expand Down Expand Up @@ -267,6 +268,8 @@ export class CodeModelBuilder {

this.processSchemaUsage();

this.processJavaNamespace();

this.deduplicateSchemaName();

return this.codeModel;
Expand Down Expand Up @@ -424,6 +427,16 @@ export class CodeModelBuilder {
this.codeModel.schemas.constants?.forEach((it) => this.resolveSchemaUsage(it));
}

private processJavaNamespace() {
// post process for usage and namespace ("implementation.models")
this.codeModel.schemas.objects?.forEach((it) => this.updateJavaNamespace(it));
this.codeModel.schemas.groups?.forEach((it) => this.updateJavaNamespace(it));
this.codeModel.schemas.choices?.forEach((it) => this.updateJavaNamespace(it));
this.codeModel.schemas.sealedChoices?.forEach((it) => this.updateJavaNamespace(it));
this.codeModel.schemas.ors?.forEach((it) => this.updateJavaNamespace(it));
this.codeModel.schemas.constants?.forEach((it) => this.updateJavaNamespace(it));
}

private deduplicateSchemaName() {
// deduplicate model name
const nameCount = new Map<string, number>();
Expand Down Expand Up @@ -483,6 +496,28 @@ export class CodeModelBuilder {
}
}

private updateJavaNamespace(schema: Schema) {
if (
schema instanceof ObjectSchema ||
schema instanceof GroupSchema ||
schema instanceof ChoiceSchema ||
schema instanceof SealedChoiceSchema ||
schema instanceof OrSchema ||
schema instanceof ConstantSchema
) {
const schemaUsage: SchemaContext[] | undefined = schema.usage;

if (schema.language.java?.namespace) {
if (schemaUsage?.includes(SchemaContext.Internal)) {
schema.language.java.namespace = schema.language.java.namespace + ".implementation";
}
if (this.isBranded()) {
schema.language.java.namespace = schema.language.java.namespace + ".models";
}
}
}
}

private processClients() {
// preprocess group-etag-headers
this.options["group-etag-headers"] = this.options["group-etag-headers"] ?? true;
Expand Down Expand Up @@ -2447,16 +2482,21 @@ export class CodeModelBuilder {
private getJavaNamespace(namespace: string | undefined): string | undefined {
const tspNamespace = this.namespace;
const baseJavaNamespace = this.emitterContext.options.namespace;

let javaNamespace;
if (!namespace) {
return undefined;
javaNamespace = undefined;
} else if (
baseJavaNamespace &&
(namespace === tspNamespace || namespace.startsWith(tspNamespace + "."))
) {
return baseJavaNamespace + namespace.slice(tspNamespace.length).toLowerCase();
// make sure the mapping of typespec service namespace to options.namespace is maintained
// e.g. "Microsoft.StandbyPool" to "com.azure.resourcemanager.standbypool"
javaNamespace = baseJavaNamespace + namespace.slice(tspNamespace.length).toLowerCase();
} else {
return "com." + namespace.toLowerCase();
javaNamespace = "com." + namespace.toLowerCase();
}
return javaNamespace;
}

private logWarning(msg: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo
return ClassType.STRING;
} else {
String enumPackage = settings.getPackage(settings.getModelsSubpackage());
if (settings.isCustomType(enumTypeName)) {
if (enumType.getLanguage().getJava().getNamespace() != null) {
enumPackage = enumType.getLanguage().getJava().getNamespace();
} else if (settings.isCustomType(enumTypeName)) {
enumPackage = settings.getPackage(settings.getCustomTypesSubpackage());
} else if (settings.isDataPlaneClient()
&& (enumType.getUsage() != null && enumType.getUsage().contains(SchemaContext.INTERNAL))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ private ClassType createClassType(ObjectSchema compositeType) {

String classPackage;
String className = compositeType.getLanguage().getJava().getName();
if (settings.isCustomType(compositeType.getLanguage().getJava().getName())) {
if (compositeType.getLanguage().getJava().getNamespace() != null) {
classPackage = compositeType.getLanguage().getJava().getNamespace();
} else if (settings.isCustomType(compositeType.getLanguage().getJava().getName())) {
classPackage = settings.getPackage(settings.getCustomTypesSubpackage());
} else if (settings.isFluent() && isInnerModel(compositeType)) {
className += "Inner";
Expand Down

0 comments on commit e6731bd

Please sign in to comment.