Skip to content

Commit

Permalink
Some fixes for tsp emitter (#1363)
Browse files Browse the repository at this point in the history
Handle types that are already handled in getBuiltInType.
Add fix-const-stuttering option to keep compat with autorest.go.
  • Loading branch information
jhendrixMSFT authored May 21, 2024
1 parent 9712968 commit 9fb0113
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
3 changes: 2 additions & 1 deletion packages/typespec-go/.scripts/tspcompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const armlargeinstance = pkgRoot + 'test/tsp/AzureLargeInstance.Management';
generate('armlargeinstance', armlargeinstance, 'test/armlargeinstance', ['stutter=AzureLargeInstance']);

const armdatabasewatcher = pkgRoot + 'test/tsp/DatabaseWatcher.Management';
generate('armdatabasewatcher', armdatabasewatcher, 'test/armdatabasewatcher', ['remove-unreferenced-types=false']);
generate('armdatabasewatcher', armdatabasewatcher, 'test/armdatabasewatcher', ['remove-unreferenced-types=false', 'fix-const-stuttering=false']);

const armloadtestservice = pkgRoot + 'test/tsp/LoadTestService.Management';
generate('armloadtestservice', armloadtestservice, 'test/armloadtestservice');
Expand Down Expand Up @@ -175,6 +175,7 @@ function generate(moduleName, input, outputDir, perTestOptions) {
'inject-spans=true',
'head-as-boolean=true',
'remove-unreferenced-types=true',
'fix-const-stuttering=true',
];

let allOptions = fixedOptions;
Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-go/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface GoEmitterOptions {
'slice-elements-byval'?: boolean;
'single-client'?: boolean;
'stutter'?: string;
'fix-const-stuttering'?: boolean;
'remove-unreferenced-types'?: boolean;
}

Expand All @@ -37,6 +38,7 @@ const EmitterOptionsSchema: JSONSchemaType<GoEmitterOptions> = {
'slice-elements-byval': { type: 'boolean', nullable: true },
'single-client': { type: 'boolean', nullable: true },
'stutter': { type: 'string', nullable: true },
'fix-const-stuttering': { type: 'boolean', nullable: true },
'remove-unreferenced-types': { type: 'boolean', nullable: true },
},
required: [],
Expand Down
15 changes: 9 additions & 6 deletions packages/typespec-go/src/tcgcadapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function tcgcToGoCodeModel(context: EmitContext<GoEmitterOptions>): go.Co
codeModel.options.sliceElementsByval = true;
}

fixStutteringTypeNames(sdkContext.experimental_sdkPackage, codeModel, context.options.stutter);
fixStutteringTypeNames(sdkContext.experimental_sdkPackage, codeModel, context.options);

const ta = new typeAdapter(codeModel);
ta.adaptTypes(sdkContext, context.options['remove-unreferenced-types'] === true);
Expand All @@ -52,11 +52,11 @@ export function tcgcToGoCodeModel(context: EmitContext<GoEmitterOptions>): go.Co
return codeModel;
}

function fixStutteringTypeNames(sdkPackage: tcgc.SdkPackage<tcgc.SdkHttpOperation>, codeModel: go.CodeModel, customPrefix?: string): void {
function fixStutteringTypeNames(sdkPackage: tcgc.SdkPackage<tcgc.SdkHttpOperation>, codeModel: go.CodeModel, options: GoEmitterOptions): void {
let stutteringPrefix = codeModel.packageName;

if (customPrefix) {
stutteringPrefix = customPrefix;
if (options.stutter) {
stutteringPrefix = options.stutter;
} else {
// if there's a well-known prefix, remove it
if (stutteringPrefix.startsWith('arm')) {
Expand Down Expand Up @@ -115,8 +115,11 @@ function fixStutteringTypeNames(sdkPackage: tcgc.SdkPackage<tcgc.SdkHttpOperatio
return newName;
};

for (const sdkEnum of sdkPackage.enums) {
sdkEnum.name = renameType(sdkEnum.name);
// to keep compat with autorest.go, this is off by default
if (options['fix-const-stuttering'] === true) {
for (const sdkEnum of sdkPackage.enums) {
sdkEnum.name = renameType(sdkEnum.name);
}
}

for (const modelType of sdkPackage.models) {
Expand Down
5 changes: 5 additions & 0 deletions packages/typespec-go/src/tcgcadapter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ export class typeAdapter {
case 'int16':
case 'int32':
case 'int64':
case 'uint8':
case 'uint16':
case 'uint32':
case 'uint64':
case 'plainDate':
case 'plainTime':
case 'string':
case 'uri':
case 'url':
case 'uuid':
return this.getBuiltInType(type);
Expand Down
42 changes: 21 additions & 21 deletions packages/typespec-go/test/armdatabasewatcher/zz_constants.go

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

2 changes: 1 addition & 1 deletion packages/typespec-go/test/armdatabasewatcher/zz_models.go

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

0 comments on commit 9fb0113

Please sign in to comment.