UNPKG

@typespec/http-server-csharp

Version:

TypeSpec service code generator for c-sharp

234 lines 7.06 kB
export const HelperNamespace = "TypeSpec.Helpers.JsonConverters"; export class CSharpType { name; namespace; isBuiltIn; isValueType; isNullable; isClass; isCollection; constructor(input) { this.name = input.name; this.namespace = input.namespace; this.isBuiltIn = input.isBuiltIn !== undefined ? input.isBuiltIn : input.namespace === "System"; this.isValueType = input.isValueType !== undefined ? input.isValueType : false; this.isNullable = input.isNullable !== undefined ? input.isNullable : false; this.isClass = input.isClass !== undefined ? input.isClass : false; this.isCollection = input.isCollection !== undefined ? input.isCollection : false; } isNamespaceInScope(scope, visited) { if (this.isBuiltIn) return true; if (scope === undefined) return false; if (!visited) visited = new Set(); if (visited.has(scope)) return false; visited.add(scope); switch (scope.kind) { case "namespace": { if (scope.namespace.startsWith(this.namespace)) return true; return this.isNamespaceInScope(scope.parentScope, visited); } case "sourceFile": { for (const entry of scope.sourceFile.imports.keys()) { if (entry === this.namespace) { return true; } } return this.isNamespaceInScope(scope.sourceFile.globalScope, visited); } default: return false; } } getTypeReference(scope) { return `${this.isNamespaceInScope(scope) ? "" : this.namespace + "."}${this.name}`; } equals(other) { return this.name === other?.name && this.namespace === other?.namespace; } } export class CSharpValue { value; } export class StringValue extends CSharpValue { value; constructor(value) { super(); this.value = value; } emitValue(scope) { return `"${this.value}"`; } } export class RawValue extends CSharpValue { value; constructor(value) { super(); this.value = value; } emitValue(scope) { return `${this.value}`; } } export class NumericValue extends CSharpValue { value; constructor(value) { super(); this.value = value; } emitValue(scope) { return `${this.value ?? 0}`; } } export class BooleanValue extends CSharpValue { value; constructor(value) { super(); this.value = value; } emitValue(scope) { return `${this.value}`; } } export class NullValue extends CSharpValue { value = null; emitValue(scope) { return "null"; } } export class Parameter { type; optional; name; value; defaultValue; constructor(input) { this.name = input.name; this.type = input.type; this.optional = input.optional; this.value = input.value; this.defaultValue = input.defaultValue; } getDeclarationString(scope) { const sb = []; sb.push(`${this.type.getTypeReference(scope)}`); if (this.optional) sb.push("?"); sb.push(` ${this.name}`); if (this.defaultValue !== undefined) sb.push(` = ${this.defaultValue.emitValue(scope)}`); return sb.join(", "); } getCallString(scope) { if (!this.value) return ""; const sb = []; if (this.optional) sb.push(`${this.name} = ${this.value.emitValue(scope)}`); else sb.push(this.value.emitValue(scope)); return sb.join(", "); } } export class AttributeType extends CSharpType { getTypeReference(scope) { const ref = super.getTypeReference(scope); const suffixStart = ref.lastIndexOf("Attribute"); if (suffixStart < 1) return ref; return ref.slice(0, suffixStart); } } export class Attribute { type; parameters; constructor(type, parameters) { this.type = type; this.parameters = parameters === undefined ? [] : parameters; } getApplicationString(scope) { const sb = []; const parameters = []; sb.push(`[${this.type.getTypeReference(scope)}`); for (let i = 0; i < this.parameters.length; ++i) { parameters.push(this.parameters[i].getCallString(scope)); } if (parameters.length > 0) sb.push(`( ${parameters.join(", ")})`); sb.push("]"); return sb.join(""); } } export class CSharpDeclaration { type; emitter; constructor(type, emitter) { this.type = type; this.emitter = emitter; } } export class CSharpModel extends CSharpDeclaration { constructor(modelName, modelNamespace, emitter) { super(new CSharpType({ name: modelName, namespace: modelNamespace, isBuiltIn: false, isValueType: false, }), emitter); } properties = []; getDeclaration(scope) { return ""; } } export class CSharpEnum extends CSharpDeclaration { getDeclaration(scope) { return ""; } } export class CSharpController extends CSharpDeclaration { getDeclaration(scope) { return ""; } } export var CSharpSourceType; (function (CSharpSourceType) { CSharpSourceType[CSharpSourceType["Model"] = 0] = "Model"; CSharpSourceType[CSharpSourceType["Controller"] = 1] = "Controller"; CSharpSourceType[CSharpSourceType["RouteConstants"] = 2] = "RouteConstants"; CSharpSourceType[CSharpSourceType["Interface"] = 3] = "Interface"; })(CSharpSourceType || (CSharpSourceType = {})); export var NameCasingType; (function (NameCasingType) { NameCasingType[NameCasingType["Class"] = 0] = "Class"; NameCasingType[NameCasingType["Constant"] = 1] = "Constant"; NameCasingType[NameCasingType["Method"] = 2] = "Method"; NameCasingType[NameCasingType["Namespace"] = 3] = "Namespace"; NameCasingType[NameCasingType["Parameter"] = 4] = "Parameter"; NameCasingType[NameCasingType["Property"] = 5] = "Property"; NameCasingType[NameCasingType["Variable"] = 6] = "Variable"; })(NameCasingType || (NameCasingType = {})); export class LibrarySourceFile { constructor(params) { this.path = params.path || "generated/lib/"; this.filename = params.filename; const source = params.emitter.createSourceFile(`${this.path}/${this.filename}`); this.conditional = params.conditional || false; this.emitted = { path: source.path, contents: params.getContents(), }; source.meta = { emitted: this.emitted, conditional: this.conditional }; this.source = source; } conditional; filename; source; emitted; path; } //# sourceMappingURL=interfaces.js.map