@autorest/go
Version:
AutoRest Go Generator
258 lines • 7.95 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/** bit flags indicating how a model/polymorphic type is used */
export var UsageFlags;
(function (UsageFlags) {
/** the type is unreferenced */
UsageFlags[UsageFlags["None"] = 0] = "None";
/** the type is received over the wire */
UsageFlags[UsageFlags["Input"] = 1] = "Input";
/** the type is sent over the wire */
UsageFlags[UsageFlags["Output"] = 2] = "Output";
})(UsageFlags || (UsageFlags = {}));
/**
* returns the Go type declaration for the specified LiteralType
*
* @param literal the type for which to emit the declaration
* @returns the Go type declaration
*/
export function getLiteralTypeDeclaration(literal) {
switch (literal.kind) {
case 'constant':
return literal.name;
case 'encodedBytes':
return '[]byte';
case 'scalar':
return literal.type;
case 'string':
return literal.kind;
case 'time':
return 'time.Time';
}
}
/**
* returns the Go type declaration for the specified type.
* any value in pkgName is prefixed to the underlying type name.
*
* @param type the type for which to emit the declaration
* @param pkgName optional package name prefix for the type
* @returns the Go type declaration
*/
export function getTypeDeclaration(type, pkgName) {
switch (type.kind) {
case 'any':
case 'string':
return type.kind;
case 'constant':
case 'interface':
case 'model':
case 'polymorphicModel':
if (pkgName) {
return `${pkgName}.${type.name}`;
}
return type.name;
case 'encodedBytes':
case 'rawJSON':
return '[]byte';
case 'literal':
return getTypeDeclaration(type.type, pkgName);
case 'map':
return `map[string]${type.valueTypeByValue ? '' : '*'}` + getTypeDeclaration(type.valueType, pkgName);
case 'qualifiedType': {
// strip packageName to just the leaf package as required
let pkg = type.packageName;
const pathChar = pkg.lastIndexOf('/');
if (pathChar) {
pkg = pkg.substring(pathChar + 1);
}
return pkg + '.' + type.exportName;
}
case 'scalar':
return type.type;
case 'slice':
return `[]${type.elementTypeByValue ? '' : '*'}` + getTypeDeclaration(type.elementType, pkgName);
case 'time':
return 'time.Time';
}
}
/** narrows type to a LiteralType within the conditional block */
export function isLiteralValueType(type) {
switch (type.kind) {
case 'constant':
case 'encodedBytes':
case 'scalar':
case 'string':
case 'time':
return true;
default:
return false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// base types
///////////////////////////////////////////////////////////////////////////////////////////////////
export class StructField {
constructor(name, type, byValue) {
this.name = name;
this.type = type;
this.byValue = byValue;
this.docs = {};
}
}
export class Struct {
constructor(name) {
this.fields = new Array();
this.name = name;
this.docs = {};
}
}
class ModelBase extends Struct {
constructor(name, annotations, usage) {
super(name);
this.annotations = annotations;
this.usage = usage;
this.fields = new Array();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
export class Any {
constructor() {
this.kind = 'any';
}
}
export class Constant {
constructor(name, type, valuesFuncName) {
this.kind = 'constant';
this.name = name;
this.type = type;
this.values = new Array();
this.valuesFuncName = valuesFuncName;
this.docs = {};
}
}
export class ConstantValue {
constructor(name, type, value) {
this.kind = 'constantValue';
this.name = name;
this.type = type;
this.value = value;
this.docs = {};
}
}
export class EncodedBytes {
constructor(encoding) {
this.kind = 'encodedBytes';
this.encoding = encoding;
}
}
export class Interface {
// WireTypes and rootType are required. however, we have a chicken-and-egg
// problem as creating a PolymorphicType requires the necessary InterfaceType.
// so these fields MUST be populated after creating the InterfaceType.
constructor(name, discriminatorField) {
this.kind = 'interface';
this.name = name;
this.discriminatorField = discriminatorField;
this.possibleTypes = new Array();
this.docs = {};
}
}
export class Literal {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
constructor(type, literal) {
this.kind = 'literal';
this.type = type;
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */
this.literal = literal;
}
}
export class Map {
constructor(valueType, valueTypeByValue) {
this.kind = 'map';
this.valueType = valueType;
this.valueTypeByValue = valueTypeByValue;
}
}
export class ModelAnnotations {
constructor(omitSerDe, multipartForm) {
this.omitSerDeMethods = omitSerDe;
this.multipartFormData = multipartForm;
}
}
export class ModelField extends StructField {
constructor(name, type, byValue, serializedName, annotations) {
super(name, type, byValue);
this.serializedName = serializedName;
this.annotations = annotations;
}
}
export class ModelFieldAnnotations {
constructor(required, readOnly, isAddlProps, isDiscriminator) {
this.required = required;
this.readOnly = readOnly;
this.isAdditionalProperties = isAddlProps;
this.isDiscriminator = isDiscriminator;
}
}
export class Model extends ModelBase {
constructor(name, annotations, usage) {
super(name, annotations, usage);
this.kind = 'model';
this.fields = new Array();
}
}
export class PolymorphicModel extends ModelBase {
constructor(name, iface, annotations, usage) {
super(name, annotations, usage);
this.kind = 'polymorphicModel';
this.interface = iface;
}
}
export class QualifiedType {
constructor(exportName, packageName) {
this.kind = 'qualifiedType';
this.exportName = exportName;
this.packageName = packageName;
}
}
export class RawJSON {
constructor() {
this.kind = 'rawJSON';
}
}
export class Scalar {
constructor(type, encodeAsString) {
this.kind = 'scalar';
this.type = type;
this.encodeAsString = encodeAsString;
}
}
export class Slice {
constructor(elementType, elementTypeByValue) {
this.kind = 'slice';
this.elementType = elementType;
this.elementTypeByValue = elementTypeByValue;
}
}
export class String {
constructor() {
this.kind = 'string';
}
}
export class Time {
constructor(format, utc) {
this.kind = 'time';
this.format = format;
this.utc = utc;
}
}
export class XMLInfo {
constructor() {
this.attribute = false;
this.text = false;
}
}
//# sourceMappingURL=type.js.map