@autorest/go
Version:
AutoRest Go Generator
41 lines • 1.96 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 helpers from './helpers.js';
/**
* Creates the content for the interfaces.go file.
*
* @param pkg contains the package content
* @returns the text for the file or the empty string
*/
export function generateInterfaces(pkg) {
if (pkg.interfaces.length === 0) {
// no polymorphic types
return '';
}
const indent = new helpers.Indentation();
let text = helpers.contentPreamble(pkg);
for (const iface of pkg.interfaces) {
const methodName = `Get${iface.rootType.name}`;
text += `// ${iface.name} provides polymorphic access to related types.\n`;
text += `// Call the interface's ${methodName}() method to access the common type.\n`;
text += '// Use a type switch to determine the concrete type. The possible types are:\n';
const possibleTypeNames = new Array();
possibleTypeNames.push(`*${iface.rootType.name}`);
for (const possibleType of iface.possibleTypes) {
possibleTypeNames.push(`*${possibleType.name}`);
}
possibleTypeNames.sort(helpers.sortAscending);
text += helpers.comment(possibleTypeNames.join(', '), '// - ');
text += `\ntype ${iface.name} interface {\n`;
if (iface.parent) {
text += `${indent.get()}${iface.parent.name}\n`;
}
text += `${indent.get()}// ${methodName} returns the ${iface.rootType.name} content of the underlying type.\n`;
text += `${indent.get()}${methodName}() *${iface.rootType.name}\n`;
text += '}\n\n';
}
return text;
}
//# sourceMappingURL=interfaces.js.map