UNPKG

@microsoft.azure/autorest.incubator

Version:
154 lines 9.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const dictionary_1 = require("../../../common/dictionary"); const text_manipulation_1 = require("../../../common/text-manipulation"); const access_modifier_1 = require("../../../csharp/code-dom/access-modifier"); const class_1 = require("../../../csharp/code-dom/class"); const constructor_1 = require("../../../csharp/code-dom/constructor"); const expression_1 = require("../../../csharp/code-dom/expression"); const method_1 = require("../../../csharp/code-dom/method"); const dotnet = require("../../../csharp/code-dom/mscorlib"); const parameter_1 = require("../../../csharp/code-dom/parameter"); const parameter_modifier_1 = require("../../../csharp/code-dom/parameter-modifier"); const case_1 = require("../../../csharp/code-dom/statements/case"); const if_1 = require("../../../csharp/code-dom/statements/if"); const return_1 = require("../../../csharp/code-dom/statements/return"); const statement_1 = require("../../../csharp/code-dom/statements/statement"); const switch_1 = require("../../../csharp/code-dom/statements/switch"); const ternery_1 = require("../../../csharp/code-dom/ternery"); const clientruntime_1 = require("../../../csharp/lowlevel-generator/clientruntime"); const media_types_1 = require("../../../common/media-types"); const tweak_model_1 = require("../../../remodeler/tweak-model"); const primitive_1 = require("../../../csharp/schema/primitive"); class XmlSerializableClass extends class_1.Class { constructor(modelClass, objectInitializer) { super(modelClass.namespace, modelClass.name); this.modelClass = modelClass; this.apply(objectInitializer); this.partial = true; this.addPartialMethods(); // set up the declaration for the toXml method. const container = new parameter_1.Parameter('container', dotnet.System.Xml.Linq.XElement); const mode = new parameter_1.Parameter('serializationMode', clientruntime_1.ClientRuntime.SerializationMode); const toXmlMethod = this.addMethod(new method_1.Method('ToXml', dotnet.System.Xml.Linq.XElement, { parameters: [container, mode], })); // setup the declaration for the xml deserializer constructor const xmlParameter = new parameter_1.Parameter('xml', dotnet.System.Xml.Linq.XElement); const deserializerConstructor = this.addMethod(new constructor_1.Constructor(this, { parameters: [xmlParameter], access: access_modifier_1.Access.Internal })); const serializeStatements = new statement_1.Statements(); const deserializeStatements = new statement_1.Statements(); for (const each of dictionary_1.values(modelClass.backingFields)) { serializeStatements.add(`${each.field.value}?.ToXml(container, ${mode.use});`); deserializeStatements.add(`${each.field.value} = new ${each.className}(xml);`); } primitive_1.pushTempVar(); for (const { value: property } of dictionary_1.items(modelClass.schema.properties)) { const prop = modelClass.$(property.details.csharp.name); const serializeStatement = prop.type.serializeToContainerMember(media_types_1.KnownMediaType.Xml, prop, container, prop.serializedName); if (property.details.csharp[tweak_model_1.HeaderProperty] === tweak_model_1.HeaderPropertyType.Header) { // it's a header only property. Don't serialize unless the mode has Microsoft.Rest.ClientRuntime.SerializationMode.IncludeHeaders enabled serializeStatements.add(if_1.If({ value: `${mode.use}.HasFlag(Microsoft.Rest.ClientRuntime.SerializationMode.IncludeHeaders)` }, serializeStatement)); } else { if (property.schema.readOnly) { serializeStatements.add(if_1.If({ value: `${mode.use}.HasFlag(Microsoft.Rest.ClientRuntime.SerializationMode.IncludeReadOnly)` }, serializeStatement)); } else { serializeStatements.add(serializeStatement); } } deserializeStatements.add(prop.assignPrivate(prop.type.deserializeFromContainerMember(media_types_1.KnownMediaType.Xml, xmlParameter, prop.serializedName, prop))); } primitive_1.popTempVar(); const $this = this; // generate the implementation for toXml toXmlMethod.add(function* () { const schema = $this.modelClass.schema; const serializedName = schema.xml && schema.xml.name ? schema.xml.name : $this.modelClass.schema.details.default.name; yield `${container} = ${container} ?? new ${dotnet.System.Xml.Linq.XElement.declaration}(${new expression_1.StringExpression(serializedName)});`; yield text_manipulation_1.EOL; yield `bool returnNow = false;`; yield `${$this.btj.name}(ref ${container}, ref returnNow);`; yield if_1.If({ value: `returnNow` }, `return ${container};`); // get serialization statements yield serializeStatements; yield `${$this.atj.name}(ref ${container});`; yield return_1.Return(container); }); // and let's fill in the deserializer constructor statements now. deserializerConstructor.add(function* () { yield `bool returnNow = false;`; yield `${$this.bfj.name}(xml, ref returnNow);`; yield if_1.If({ value: `returnNow` }, `return;`); yield deserializeStatements; yield `${$this.afj.name}(xml);`; }); } get definition() { const $this = this.modelClass; // gotta write this just before we write out the class, since we had to wait until everyone had reported to their parents. const d = this.modelClass.discriminators; const isp = this.modelClass.isPolymorphic; // create the FromXml method const node = new parameter_1.Parameter('node', dotnet.System.Xml.Linq.XElement); const fromXml = this.addMethod(new method_1.Method('FromXml', this.modelClass.modelInterface, { parameters: [node], static: access_modifier_1.Modifier.Static })); fromXml.add(function* () { const xml = expression_1.IsDeclaration(node, dotnet.System.Xml.Linq.XElement, 'xml'); if (isp) { yield if_1.If(if_1.Not(xml.check), return_1.Return(dotnet.Null)); yield '// Polymorphic type -- select the appropriate constructor using the discriminator'; /** go thru the list of polymorphic values for the discriminator, and call the target class's constructor for that */ if ($this.schema.discriminator) { yield switch_1.Switch({ value: `xml.Element("${$this.schema.discriminator.propertyName}").Value` }, function* () { for (const { key, value } of dictionary_1.items(d)) { yield case_1.TerminalCase(`"${key}"`, function* () { yield return_1.Return(value.newInstance(xml)); }); } }); } yield return_1.Return($this.newInstance(xml)); } else { // just tell it to create the instance (providing that it's a XElement) yield return_1.Return(ternery_1.Ternery(xml.check, $this.newInstance(xml), dotnet.Null)); } }); return super.definition; } get fileName() { return `${super.fileName}.xml`; } addPartialMethods() { // add partial methods for future customization this.btj = this.addMethod(new method_1.PartialMethod('BeforeToXml', dotnet.Void, { access: access_modifier_1.Access.Default, parameters: [ new parameter_1.Parameter('container', dotnet.System.Xml.Linq.XElement, { modifier: parameter_modifier_1.ParameterModifier.Ref, description: 'The XElement that the serialization result will be placed in.' }), new parameter_1.Parameter('returnNow', dotnet.Bool, { modifier: parameter_modifier_1.ParameterModifier.Ref, description: 'Determines if the rest of the serialization should be processed, or if the method should return instantly.' }), ], })); this.atj = this.addMethod(new method_1.PartialMethod('AfterToXml', dotnet.Void, { access: access_modifier_1.Access.Default, parameters: [ new parameter_1.Parameter('container', dotnet.System.Xml.Linq.XElement, { modifier: parameter_modifier_1.ParameterModifier.Ref, description: 'The XElement that the serialization result will be placed in.' }), ], })); this.bfj = this.addMethod(new method_1.PartialMethod('BeforeFromXml', dotnet.Void, { access: access_modifier_1.Access.Default, parameters: [ new parameter_1.Parameter('xml', dotnet.System.Xml.Linq.XElement, { description: 'The XmlNode that should be deserialized into this object.' }), new parameter_1.Parameter('returnNow', dotnet.Bool, { modifier: parameter_modifier_1.ParameterModifier.Ref, description: 'Determines if the rest of the deserialization should be processed, or if the method should return instantly.' }), ], })); this.afj = this.addMethod(new method_1.PartialMethod('AfterFromXml', dotnet.Void, { access: access_modifier_1.Access.Default, parameters: [ new parameter_1.Parameter('xml', dotnet.System.Xml.Linq.XElement, { description: 'The XmlNode that should be deserialized into this object.' }), ], })); } } exports.XmlSerializableClass = XmlSerializableClass; //# sourceMappingURL=model-class-xml.js.map