UNPKG

@microsoft.azure/autorest.incubator

Version:
216 lines 13.4 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 mscorlib_1 = require("../../csharp/code-dom/mscorlib"); const variable_1 = require("../../csharp/code-dom/variable"); const primitive_1 = require("../../csharp/schema/primitive"); const clientruntime_1 = require("../../csharp/lowlevel-generator/clientruntime"); const for_1 = require("../../csharp/code-dom/statements/for"); const if_1 = require("../../csharp/code-dom/statements/if"); const schema_1 = require("../../common/code-model/schema"); class ArrayOf { constructor(schema, isRequired, elementType, minItems, maxItems, unique) { this.schema = schema; this.isRequired = isRequired; this.elementType = elementType; this.minItems = minItems; this.maxItems = maxItems; this.unique = unique; this.isXmlAttribute = false; this.xmlName = ""; } get isWrapped() { return this.schema.xml && this.schema.xml.wrapped || false; } get wrapperName() { return this.schema.xml && this.isWrapped ? this.schema.xml.name : undefined; } get serializedName() { return this.schema.xml ? this.schema.xml.name : undefined; } get declaration() { return `${this.elementType.declaration}[]`; } /** emits an expression to deserialize a property from a member inside a container */ deserializeFromContainerMember(mediaType, container, serializedName, defaultValue) { switch (mediaType) { case media_types_1.KnownMediaType.Json: { // json array 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.JsonArray}>("${serializedName}"), out var ${tmp}) ? ${this.deserializeFromNode(mediaType, tmp, expression_1.toExpression('null'))} : ${defaultValue}`); } case media_types_1.KnownMediaType.Xml: { // this.xmlName = this.serializedName || serializedName; // XElement/XElement const tmp = `__${text_manipulation_1.camelCase(['xml', ...text_manipulation_1.deconstruct(serializedName)])}`; if (this.isWrapped) { // wrapped xml arrays will have a container around them. return expression_1.toExpression(`${this.deserializeFromNode(mediaType, `${expression_1.valueOf(container)}?.Element("${this.serializedName || serializedName}")`, defaultValue)}`); } else { // whereas non-wrapped will have all the elements in the container directly. return expression_1.toExpression(`${this.deserializeFromNode(mediaType, `${expression_1.valueOf(container)}`, defaultValue)}`); } } } return expression_1.toExpression(`null /* 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(); const each = primitive_1.pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: { const deser = `System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select( ${tmp}, (${each})=> ${this.elementType.deserializeFromNode(mediaType, each, expression_1.toExpression('null'))} ) )`; return expression_1.toExpression(`If( ${expression_1.valueOf(node)}, out var ${tmp}) ? new System.Func<${this.elementType.declaration}[]>(()=> ${deser} )() : ${defaultValue}`); } case media_types_1.KnownMediaType.Xml: { // XElement should be a container of items, right? // if the reference doesn't define an XML schema then use its default name const defaultName = this.elementType.schema.details.default.name; const deser = `System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select( ${tmp}.Elements("${this.elementType.schema.xml ? this.elementType.schema.xml.name || defaultName : defaultName}"), (${each})=> ${this.elementType.deserializeFromNode(mediaType, each, expression_1.toExpression('null'))} ) )`; return expression_1.toExpression(`If(${expression_1.valueOf(node)} , out var ${tmp}) ? new System.Func<${this.elementType.declaration}[]>(()=> ${deser} )() : ${defaultValue}`); } case media_types_1.KnownMediaType.Header: { // node is an array of string values // return toExpression(`!!! ${tmp}, (${each})=> ${this.elementType.deserializeFromNode(mediaType, each, toExpression('null'))} ) )`); return expression_1.toExpression(`System.Linq.Enumerable.ToArray(${node})`); } } } finally { primitive_1.popTempVar(); primitive_1.popTempVar(); } return expression_1.toExpression(`null /* deserializeFromNode 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 an expression serialize this to a HttpContent */ serializeToNode(mediaType, value, serializedName) { try { const each = primitive_1.pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: { const serArray = `System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(${value}, (${each}) => ${this.elementType.serializeToNode(mediaType, each, serializedName)}))`; return expression_1.toExpression(`null != ${value} ? new ${clientruntime_1.ClientRuntime.XNodeArray}(${serArray}) : null`); } case media_types_1.KnownMediaType.Xml: { if (this.isWrapped) { const name = this.elementType.schema.xml ? this.elementType.schema.xml.name || serializedName : serializedName; return expression_1.toExpression(`null != ${value} ? new System.Xml.Linq.XElement("${name}", System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(${value}, (${each}) => ${this.elementType.serializeToNode(mediaType, each, name)}))`); } else { throw new Error("Can't set an Xml Array to the document without wrapping it."); } } case media_types_1.KnownMediaType.Cookie: case media_types_1.KnownMediaType.QueryParameter: return expression_1.toExpression(`if (${value} != null && ${value}.Length > 0) { queryParameters.Add("${serializedName}=" + System.Uri.EscapeDataString(System.Linq.Enumerable.Aggregate(${value}, (current, each) => current + "," + (string.IsNullOrEmpty(each) ? System.Uri.EscapeDataString(each) : System.String.Empty)))); }`); case media_types_1.KnownMediaType.Header: case media_types_1.KnownMediaType.Text: case media_types_1.KnownMediaType.UriParameter: return expression_1.toExpression(`(null != ${value} ? System.Uri.EscapeDataString(System.Linq.Enumerable.Aggregate(${value}, (current,each)=> current + "," + ${this.elementType.serializeToNode(mediaType, 'each', '')})) : ${mscorlib_1.System.String.Empty})`); } } finally { primitive_1.popTempVar(); } 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) { try { const each = primitive_1.pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: { return expression_1.toExpression(`new System.Net.Http.StringContent( null != ${value} ? new ${clientruntime_1.ClientRuntime.XNodeArray}(${this.serializeToNode(mediaType, value, '')}).ToString() : "", System.Text.Encoding.UTF8)`); } case media_types_1.KnownMediaType.Xml: { // if the reference doesn't define an XML schema then use its default name const defaultName = this.elementType.schema.details.default.name; return expression_1.toExpression(`new System.Net.Http.StringContent( ${this.serializeToNode(mediaType, value, this.schema.xml ? this.schema.xml.name || defaultName : defaultName)}).ToString() : "", System.Text.Encoding.UTF8)`); } case media_types_1.KnownMediaType.Cookie: case media_types_1.KnownMediaType.QueryParameter: case media_types_1.KnownMediaType.Header: case media_types_1.KnownMediaType.Text: case media_types_1.KnownMediaType.UriParameter: return expression_1.toExpression(`(null != ${value} ? System.Uri.EscapeDataString(System.Linq.Enumerable.Aggregate(${value}, (current,each)=> current + "," + ${this.elementType.serializeToNode(mediaType, 'each', '')})) : ${mscorlib_1.System.String.Empty})`); } } finally { primitive_1.popTempVar(); } return expression_1.toExpression(`null /* serializeToContent doesn't support '${mediaType}' ${__filename}*/`); } /** emits the code required to serialize this into a container */ serializeToContainerMember(mediaType, value, container, serializedName) { try { const each = primitive_1.pushTempVar(); const tmp = primitive_1.pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: const $this = this; return if_1.If(`null != ${value}`, function* () { const t = new variable_1.LocalVariable(tmp, mscorlib_1.Var, { initializer: `new ${clientruntime_1.ClientRuntime.XNodeArray}()` }); yield t.declarationStatement; yield for_1.ForEach(each, expression_1.toExpression(value), `AddIf(${$this.elementType.serializeToNode(mediaType, each, '')} ,${tmp}.Add);`); yield `${container}.Add("${serializedName}",${tmp});`; }); case media_types_1.KnownMediaType.Xml: if (this.isWrapped) { return `AddIf( ${this.serializeToNode(mediaType, value, this.elementType.schema.xml ? this.elementType.schema.xml.name || "!!!" : serializedName)}):null, ${container}.Add);`; } else { return if_1.If(`null != ${value}`, for_1.ForEach(each, expression_1.toExpression(value), `AddIf(${this.elementType.serializeToNode(mediaType, each, serializedName)}, ${container}.Add);`)); } case media_types_1.KnownMediaType.Header: if (this.elementType.schema.type === schema_1.JsonType.String) { return this.isRequired ? `${expression_1.valueOf(container)}.Add("${serializedName}",${value});` : if_1.If(`null != ${value}`, `${expression_1.valueOf(container)}.Add("${serializedName}",${value});`); } break; } } finally { primitive_1.popTempVar(); primitive_1.popTempVar(); } return (`/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`); } validatePresence(property) { if (this.isRequired) { return `await listener.AssertNotNull(${text_manipulation_1.nameof(property.value)}, ${property}); `; } return ``; } validateValue(property) { // check if the underlyingType has validation. if (!this.elementType.validateValue(new variable_1.LocalVariable(`${property} [{ __i }]`, mscorlib_1.Var))) { return ''; } return ` if (${property} != null ) { for (int __i = 0; __i < ${property}.Length; __i++) { ${this.elementType.validateValue(new variable_1.LocalVariable(`${property}[__i]`, mscorlib_1.Var))} } } `.trim(); } } exports.ArrayOf = ArrayOf; //# sourceMappingURL=array.js.map