UNPKG

@autorest/go

Version:
341 lines 10.7 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as path from 'path'; import { getPackageName } from './module.js'; /** 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 = {})); /////////////////////////////////////////////////////////////////////////////////////////////////// // helpers /////////////////////////////////////////////////////////////////////////////////////////////////// /** * 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 'constantDef': return literal.literal.type.kind; 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. * if the type is defined in a package that's different from * the provided scope, the type declaration will include * the type's package name prefix. * * @param type the type for which to emit the declaration * @param scope the scope in which the type declaration is emitted * @returns the Go type declaration */ export function getTypeDeclaration(type, scope) { switch (type.kind) { case 'any': case 'string': return type.kind; case 'client': case 'clientOptions': case 'constant': case 'constantValue': case 'interface': case 'model': case 'paramGroup': case 'polymorphicModel': case 'responseEnvelope': { let pkg; const typeName = type.kind === 'paramGroup' ? type.groupName : type.name; switch (type.kind) { case 'constantValue': pkg = type.type.pkg; break; case 'responseEnvelope': pkg = type.method.receiver.type.pkg; break; default: pkg = type.pkg; } if (pkg !== scope) { // type is being referenced from a different package // then where it's defined, so add its package prefix return `${getPackageName(pkg)}.${typeName}`; } return typeName; } case 'constantDef': return type.literal.type.kind; case 'encodedBytes': case 'rawJSON': return '[]byte'; case 'literal': return getTypeDeclaration(type.type, scope); case 'map': return `map[string]${type.valueTypeByValue ? '' : '*'}` + getTypeDeclaration(type.valueType, scope); case 'scalar': return type.type; case 'slice': return `[]${type.elementTypeByValue ? '' : '*'}` + getTypeDeclaration(type.elementType, scope); case 'time': return 'time.Time'; case 'armClientOptions': case 'etag': case 'multipartContent': case 'readCloser': case 'readSeekCloser': case 'tokenCredential': // strip module to just the leaf package as required return `${path.basename(type.module)}.${type.name}`; } } /** 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; } } /////////////////////////////////////////////////////////////////////////////////////////////////// // exported base types /////////////////////////////////////////////////////////////////////////////////////////////////// export class StructField { constructor(name, type, byValue) { this.name = name; this.type = type; this.byValue = byValue; this.docs = {}; } } export class QualifiedType { constructor(name, module) { this.name = name; this.module = module; } } class StructBase { constructor(pkg, name) { this.name = name; this.fields = new Array(); this.docs = {}; this.pkg = pkg; } } class ModelBase extends StructBase { constructor(pkg, name, annotations, usage) { super(pkg, name); this.annotations = annotations; this.usage = usage; this.fields = new Array(); } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// export class Any { constructor() { this.kind = 'any'; } } export class ArmClientOptions extends QualifiedType { constructor() { super('ClientOptions', 'github.com/Azure/azure-sdk-for-go/sdk/azcore/arm'); this.kind = 'armClientOptions'; } } export class Constant { constructor(pkg, name, type, valuesFuncName) { this.kind = 'constant'; this.name = name; this.pkg = pkg; this.type = type; this.values = new Array(); this.valuesFuncName = valuesFuncName; this.docs = {}; } } export class ConstantDef { constructor(name, literal) { this.kind = 'constantDef'; this.name = name; this.literal = literal; } } 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 ETag extends QualifiedType { constructor() { super('ETag', 'github.com/Azure/azure-sdk-for-go/sdk/azcore'); this.kind = 'etag'; } } 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(pkg, name, discriminatorField) { this.kind = 'interface'; this.name = name; this.pkg = pkg; this.discriminatorField = discriminatorField; this.possibleTypes = new Array(); this.docs = {}; } } export class Literal { constructor(type, literal) { this.kind = 'literal'; this.type = type; 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; this.unmarshalEmptyStringAsNil = false; } } export class Model extends ModelBase { constructor(pkg, name, annotations, usage) { super(pkg, name, annotations, usage); this.kind = 'model'; this.fields = new Array(); } } export class MultipartContent extends QualifiedType { constructor(contentType) { super('MultipartContent', 'github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming'); this.kind = 'multipartContent'; this.contentType = contentType; } } export class PolymorphicModel extends ModelBase { constructor(pkg, name, iface, annotations, usage) { super(pkg, name, annotations, usage); this.kind = 'polymorphicModel'; this.interface = iface; } } export class RawJSON { constructor() { this.kind = 'rawJSON'; } } export class ReadCloser extends QualifiedType { constructor() { super('ReadCloser', 'io'); this.kind = 'readCloser'; } } export class ReadSeekCloser extends QualifiedType { constructor() { super('ReadSeekCloser', 'io'); this.kind = 'readSeekCloser'; } } 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 Struct extends StructBase { constructor(pkg, name) { super(pkg, name); this.kind = 'struct'; } } export class Time extends QualifiedType { constructor(format, utc) { super('Time', 'time'); this.kind = 'time'; this.format = format; this.utc = utc; } } export class TokenCredential extends QualifiedType { constructor(scopes) { super('TokenCredential', 'github.com/Azure/azure-sdk-for-go/sdk/azcore'); this.kind = 'tokenCredential'; this.scopes = scopes; } } export class XMLInfo { constructor() { this.attribute = false; this.text = false; } } //# sourceMappingURL=type.js.map