UNPKG

@microsoft.azure/autorest.incubator

Version:
137 lines 7.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const text_manipulation_1 = require("../../common/text-manipulation"); const media_types_1 = require("../../common/media-types"); const expression_1 = require("../../csharp/code-dom/expression"); const if_1 = require("../../csharp/code-dom/statements/if"); const for_1 = require("../../csharp/code-dom/statements/for"); const primitive_1 = require("../../csharp/schema/primitive"); class Wildcard { constructor(schema, leafType) { this.schema = schema; this.leafType = leafType; this.isXmlAttribute = false; /** is the value of this type permitted to be NULL */ this.isRequired = false; } get declaration() { return `System.Collections.Generic.IDictionary<string,${this.leafType.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: break; case media_types_1.KnownMediaType.Xml: return expression_1.toExpression(`System.Linq.Enumerable.ToDictionary( ${expression_1.valueOf(container)}?.Elements() ?? System.Linq.Enumerable.Empty<System.Xml.Linq.XElement>(), element => element.Name.ToString(), element => ${this.leafType.deserializeFromNode(mediaType, 'element', expression_1.toExpression('null'))} )`); case media_types_1.KnownMediaType.Header: { const prefix = this.schema.extensions['x-ms-header-collection-prefix']; if (prefix) { // this is a catch for a specific set of headers return expression_1.toExpression(`System.Linq.Enumerable.ToDictionary(System.Linq.Enumerable.Where(${expression_1.valueOf(container)}, header => header.Key.StartsWith("${serializedName}",System.StringComparison.InvariantCulture)), header => header.Key.Substring(${serializedName.length}), header => System.Linq.Enumerable.FirstOrDefault(header.Value))`); } return expression_1.toExpression(`System.Linq.Enumerable.ToDictionary( ${expression_1.valueOf(container)}?.Elements() ?? System.Linq.Enumerable.Empty<System.Xml.Linq.XElement>(), element => element.Name.ToString(), element => ${this.leafType.deserializeFromNode(mediaType, 'element', expression_1.toExpression('null'))} )`); } } return expression_1.toExpression(`null /* deserializeFromContainerMember (wildcard) doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression to deserialze a container as the value itself. */ deserializeFromNode(mediaType, node, defaultValue) { /* try { const tmp = pushTempVar(); switch( mediaType) { case KnownMediaType.Header: { // node is an array of string values return toExpression(`System.Linq.Enumerable.FirstOrDefault(${node}) is string ${tmp} ? System.Convert.FromBase64String(${tmp}) : ${defaultValue}`); } } } finally { popTempVar(); } */ return expression_1.toExpression(`null /* deserializeFromNode (wildcard) doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression serialize this to the value required by the container */ serializeToNode(mediaType, value, serializedName) { return expression_1.toExpression(`null /* serializeToNode (wildcard) doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression serialize this to the value required by the container */ serializeToContent(mediaType, value) { return expression_1.toExpression(`null /* serializeToContent (wildcard) doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression to deserialize content from a string */ deserializeFromString(mediaType, content, defaultValue) { return expression_1.toExpression(`null /* deserializeFromString (wildcard)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(); switch (mediaType) { case media_types_1.KnownMediaType.Json: break; case media_types_1.KnownMediaType.Xml: return if_1.If(`null != ${value}`, for_1.ForEach(each, expression_1.toExpression(value), `AddIf( ${this.leafType.serializeToNode(mediaType, `${each}.Value`, `$$$`)},${container}.Add );`.replace('"$$$"', `${each}.Key`))); case media_types_1.KnownMediaType.Header: const prefix = this.schema.extensions['x-ms-header-collection-prefix']; if (prefix) { return if_1.If(`null != ${value}`, for_1.ForEach(each, expression_1.toExpression(value), `${expression_1.valueOf(container)}.Add("${prefix}"+${each}.Key,${each}.Value);`)); } // return If(`null != ${value} `, ForEach(each, toExpression(value), `${valueOf(container)}.Add("${serializedName}",${each}););`.replace('"$$$"', `${each}.Key`))); } } finally { primitive_1.popTempVar(); } return `/* serializeToContainerMember (wildcard) doesn't support '${mediaType}' ${__filename}*/`; } validatePresence(property) { return ``; } validateValue(property) { return ``; } } exports.Wildcard = Wildcard; class UntypedWildcard { constructor(schema) { this.schema = schema; this.isXmlAttribute = false; /** is the value of this type permitted to be NULL */ this.isRequired = false; } /** emits an expression to deserialize a property from a member inside a container */ deserializeFromContainerMember(mediaType, container, serializedName, 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) { return expression_1.toExpression(`null /* deserializeFromNode doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression serialize this to the value required by the container */ serializeToNode(mediaType, value, serializedName) { 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) { return expression_1.toExpression(`null /* serializeToContent doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression to deserialize content from a string */ deserializeFromString(mediaType, content, defaultValue) { return expression_1.toExpression(`null /* deserializeFromString doesn't support '${mediaType}' ${__filename}*/`); } /** emits the code required to serialize this into a container */ serializeToContainerMember(mediaType, value, container, serializedName) { return `/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`; } get declaration() { return `System.Collections.Generic.Dictionary<string,object>`; } validatePresence(property) { return `await listener.AssertNotNull(${text_manipulation_1.nameof(property.value)},${property});`.trim(); } validateValue(property) { return `/* untyped wildcard validate value for ${property} */`; } } exports.UntypedWildcard = UntypedWildcard; //# sourceMappingURL=wildcard.js.map