UNPKG

@microsoft.azure/autorest.incubator

Version:
107 lines 6.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const media_types_1 = require("../../common/media-types"); const text_manipulation_1 = require("../../common/text-manipulation"); const expression_1 = require("../../csharp/code-dom/expression"); const clientruntime_1 = require("../../csharp/lowlevel-generator/clientruntime"); const mscorlib_1 = require("../../csharp/code-dom/mscorlib"); const primitive_1 = require("../../csharp/schema/primitive"); class ObjectImplementation { constructor(schema) { this.schema = schema; this.isXmlAttribute = false; this.isRequired = false; } deserializeFromContainerMember(mediaType, container, serializedName, defaultValue) { switch (mediaType) { case media_types_1.KnownMediaType.Json: { // JsonObject const tmp = `__${text_manipulation_1.camelCase(['json', ...text_manipulation_1.deconstruct(serializedName)])}`; return expression_1.toExpression(`If( ${expression_1.valueOf(container)}?.PropertyT<${clientruntime_1.ClientRuntime.JsonObject}>("${serializedName}"), out var ${tmp}) ? ${this.classDeclaration}.FromJson(${tmp}) : ${defaultValue}`); } case media_types_1.KnownMediaType.Xml: { // XElement/XElement or XElement/XAttribute const tmp = `__${text_manipulation_1.camelCase(['xml', ...text_manipulation_1.deconstruct(serializedName)])}`; // prefer specified XML name if available return expression_1.toExpression(`If( ${expression_1.valueOf(container)}?.Element("${this.schema.xml ? this.schema.xml.name || serializedName : serializedName}"), out var ${tmp}) ? ${this.classDeclaration}.FromXml(${tmp}) : ${defaultValue}`); } } return expression_1.toExpression(`${defaultValue} /* deserializeFromContainerMember doesn't support '${mediaType}' ${__filename} */`); } /** emits an expression to deserialze a container as the value itself. */ deserializeFromNode(mediaType, node, defaultValue) { try { const tmp = primitive_1.pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: { // we're always going to go thru FromJson; it'll handle nulls and polymorphism. return expression_1.toExpression(`${this.classDeclaration}.FromJson(${node}) `); } case media_types_1.KnownMediaType.Xml: { return expression_1.toExpression(`If( ${expression_1.valueOf(node)}, out var ${tmp}) ? ${this.classDeclaration}.FromXml(${tmp}) : ${defaultValue}`); } } } finally { primitive_1.popTempVar(); } return expression_1.toExpression(`null /* deserializeFromNode doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression serialize this to a HttpContent */ serializeToNode(mediaType, value, serializedName) { switch (mediaType) { case media_types_1.KnownMediaType.Json: { return expression_1.toExpression(`${value}?.ToJson(null)`); } case media_types_1.KnownMediaType.Xml: { return expression_1.toExpression(`${value}?.ToXml(null)`); } } return expression_1.toExpression(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression serialize this to the value required by the container */ serializeToContent(mediaType, value) { switch (mediaType) { case media_types_1.KnownMediaType.Json: { return expression_1.toExpression(`new System.Net.Http.StringContent( null != ${value} ? ${value}.ToJson(null).ToString() : "{}", System.Text.Encoding.UTF8);`); } case media_types_1.KnownMediaType.Xml: { return expression_1.toExpression(`new System.Net.Http.StringContent( null != ${value} ? ${value}.ToXml(null).ToString() : "", System.Text.Encoding.UTF8);`); } } return expression_1.toExpression(`null /* serializeToContent doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression to deserialize content from a string */ deserializeFromString(mediaType, content, defaultValue) { switch (mediaType) { case media_types_1.KnownMediaType.Json: { return this.deserializeFromNode(mediaType, `Carbon.Json.JsonNode.Parse(${content})`, defaultValue); } case media_types_1.KnownMediaType.Xml: { return this.deserializeFromNode(mediaType, `${mscorlib_1.System.Xml.Linq.XElement}.Parse(${content})`, defaultValue); } } return undefined; } /** emits the code required to serialize this into a container */ serializeToContainerMember(mediaType, value, container, serializedName) { switch (mediaType) { case media_types_1.KnownMediaType.Json: return `AddIf( null != ${value} ? (${clientruntime_1.ClientRuntime.JsonNode}) ${value}.ToJson(null) : null, "${serializedName}" ,${container}.Add );`; case media_types_1.KnownMediaType.Xml: // prefer specified XML name if available return `AddIf( null != ${value} ? ${value}.ToXml(new ${mscorlib_1.System.Xml.Linq.XElement}("${this.schema.xml ? this.schema.xml.name || serializedName : serializedName}")) : null, ${container}.Add );`; } return `/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`; } validatePresence(property) { return `await listener.AssertNotNull(${text_manipulation_1.nameof(property.value)}, ${property}); `.trim(); } validateValue(property) { return `await listener.AssertObjectIsValid(${text_manipulation_1.nameof(property.value)}, ${property}); `; } get declaration() { return `${this.schema.details.csharp.namespace}.${this.schema.details.csharp.interfaceName}`; } get classDeclaration() { return `${this.schema.details.csharp.namespace}.${this.schema.details.csharp.name}`; } } exports.ObjectImplementation = ObjectImplementation; //# sourceMappingURL=object.js.map