UNPKG

@microsoft.azure/autorest.incubator

Version:
128 lines 7.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const access_modifier_1 = require("../../../csharp/code-dom/access-modifier"); const constructor_1 = require("../../../csharp/code-dom/constructor"); const expression_1 = require("../../../csharp/code-dom/expression"); const field_1 = require("../../../csharp/code-dom/field"); const interface_1 = require("../../../csharp/code-dom/interface"); const method_1 = require("../../../csharp/code-dom/method"); const dotnet = require("../../../csharp/code-dom/mscorlib"); const namespace_1 = require("../../../csharp/code-dom/namespace"); const operator_1 = require("../../../csharp/code-dom/operator"); const parameter_1 = require("../../../csharp/code-dom/parameter"); const property_1 = require("../../../csharp/code-dom/property"); const struct_1 = require("../../../csharp/code-dom/struct"); class EnumClass extends struct_1.Struct { get schema() { return this.implementation.schema; } deserializeFromContainerMember(mediaType, container, serializedName, defaultValue) { return this.implementation.deserializeFromContainerMember(mediaType, container, serializedName, defaultValue); } deserializeFromNode(mediaType, node, defaultValue) { return this.implementation.deserializeFromNode(mediaType, node, defaultValue); } /** emits an expression to deserialize content from a string */ deserializeFromString(mediaType, content, defaultValue) { return this.implementation.deserializeFromString(mediaType, content, defaultValue); } serializeToNode(mediaType, value, serializedName) { return this.implementation.serializeToNode(mediaType, value, serializedName); } /** emits an expression serialize this to a HttpContent */ serializeToContent(mediaType, value) { return this.implementation.serializeToContent(mediaType, value); } serializeToContainerMember(mediaType, value, container, serializedName) { return this.implementation.serializeToContainerMember(mediaType, value, container, serializedName); } get isXmlAttribute() { return this.implementation.isXmlAttribute; } get isRequired() { return this.implementation.isRequired; } constructor(schemaWithFeatures, state, objectInitializer) { if (!schemaWithFeatures.schema.details.csharp.enum) { throw new Error(`ENUM AINT XMSENUM: ${schemaWithFeatures.schema.details.csharp.name}`); } super(state.project.supportNamespace, schemaWithFeatures.schema.details.csharp.enum.name, undefined, { interfaces: [new interface_1.Interface(new namespace_1.Namespace('System'), 'IEquatable', { genericParameters: [`${schemaWithFeatures.schema.details.csharp.enum.name}`] })], }); this.implementation = schemaWithFeatures; this.addGeneratedCodeAttribute = true; this.apply(objectInitializer); // add known enum values for (const evalue of schemaWithFeatures.schema.details.csharp.enum.values) { const field = this.addField(new field_1.InitializedField(evalue.name, this, new expression_1.StringExpression(evalue.value))); field.static = access_modifier_1.Modifier.Static; field.description = evalue.description; } // add backingField const backingField = this.add(new property_1.Property('value', dotnet.String, { getAccess: access_modifier_1.Access.Private, setAccess: access_modifier_1.Access.Private })); // add private constructor const p = new parameter_1.Parameter('underlyingValue', dotnet.String); const ctor = this.addMethod(new constructor_1.Constructor(this, { access: access_modifier_1.Access.Private, parameters: [p], })).add(`this.${backingField.value} = ${p.value};`); // add toString Method this.addMethod(new method_1.Method('ToString', dotnet.String, { override: access_modifier_1.Modifier.Override, description: `Returns string representation for ${this.name}` })).add(`return this.${backingField.value};`); // add Equals Method(thistype) this.addMethod(new method_1.Method('Equals', dotnet.Bool, { description: `Compares values of enum type ${this.name}`, parameters: [new parameter_1.Parameter('e', this)] })).add(`return ${backingField.value}.Equals(e.${backingField.value});`); // add Equals Method(object) this.addMethod(new method_1.Method('Equals', dotnet.Bool, { override: access_modifier_1.Modifier.Override, description: `Compares values of enum type ${this.name} (override for Object)`, parameters: [new parameter_1.Parameter('obj', dotnet.Object)] })).add(`return obj is ${this.name} && Equals((${this.name})obj);`); // add implicit operator(string) this.addMethod(new operator_1.Operator(`implicit operator ${this.name}`, { static: access_modifier_1.Modifier.Static, description: `Implicit operator to convert string to ${this.name}`, parameters: [new parameter_1.Parameter('value', dotnet.String)] })).add(`return new ${this.name}(value);`); // add implicit operator(thistype) this.addMethod(new operator_1.Operator(`implicit operator string`, { static: access_modifier_1.Modifier.Static, description: `Implicit operator to convert ${this.name} to string`, parameters: [new parameter_1.Parameter('e', this)] })).add(`return e.${backingField.value};`); // add operator == this.addMethod(new method_1.Method(`operator ==`, dotnet.Bool, { static: access_modifier_1.Modifier.Static, description: `Overriding == operator for enum ${this.name}`, parameters: [new parameter_1.Parameter('e1', this), new parameter_1.Parameter('e2', this)] })).add(`return e2.Equals(e1);`); // add opeator != this.addMethod(new method_1.Method(`operator !=`, dotnet.Bool, { static: access_modifier_1.Modifier.Static, description: `Overriding != operator for enum ${this.name}`, parameters: [new parameter_1.Parameter('e1', this), new parameter_1.Parameter('e2', this)] })).add(`return !e2.Equals(e1);`); // add getHashCode this.addMethod(new method_1.Method(`GetHashCode`, dotnet.Int, { override: access_modifier_1.Modifier.Override, description: `Returns hashCode for enum ${this.name}`, })).add(`return this.${backingField.value}.GetHashCode();`); } validateValue(property) { return this.implementation.validateValue(property); } validatePresence(property) { return this.implementation.validatePresence(property); } } exports.EnumClass = EnumClass; //# sourceMappingURL=enum.js.map