@autorest/go
Version:
AutoRest Go Generator
65 lines • 2.22 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as go from '../../codemodel.go/src/index.js';
import { values } from '@azure-tools/linq';
import { sortAscending } from './helpers.js';
// tracks packages that need to be imported
export class ImportManager {
constructor() {
this.imports = new Array();
}
// adds a package for importing if not already in the list
// accepts an optional package alias.
add(imp, alias) {
for (const existing of values(this.imports)) {
if (existing.imp === imp) {
return;
}
}
this.imports.push({ imp: imp, alias: alias });
}
// returns the number of packages in the list
length() {
return this.imports.length;
}
// returns the import list as Go source code
text() {
if (this.imports.length === 0) {
return '';
}
else if (this.imports.length === 1) {
const first = this.imports[0];
return `import ${this.alias(first)}"${first.imp}"\n\n`;
}
this.imports.sort((a, b) => { return sortAscending(a.imp, b.imp); });
let text = 'import (\n';
for (const imp of values(this.imports)) {
text += `\t${this.alias(imp)}"${imp.imp}"\n`;
}
text += ')\n\n';
return text;
}
addImportForType(type) {
if (go.isMapType(type)) {
this.addImportForType(type.valueType);
}
else if (go.isSliceType(type)) {
this.addImportForType(type.elementType);
}
else if (go.isQualifiedType(type)) {
this.add(type.packageName);
}
else if (go.isTimeType(type)) {
this.add('time');
}
}
alias(entry) {
if (entry.alias) {
return `${entry.alias} `;
}
return '';
}
}
//# sourceMappingURL=imports.js.map