UNPKG

@microsoft.azure/autorest.incubator

Version:
142 lines 8.24 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 if_1 = require("../../csharp/code-dom/statements/if"); const clientruntime_1 = require("../../csharp/lowlevel-generator/clientruntime"); const primitive_1 = require("../../csharp/schema/primitive"); /** A ETD for the c# string type. */ class String { constructor(schema, isRequired) { this.schema = schema; this.isRequired = isRequired; this.isXmlAttribute = false; } deserializeFromContainerMember(mediaType, container, serializedName, defaultValue) { switch (mediaType) { case media_types_1.KnownMediaType.Json: { // container should be a JsonObject const tmpVar = `__${text_manipulation_1.camelCase(['json', ...text_manipulation_1.deconstruct(serializedName)])}`; return expression_1.toExpression(`If( ${expression_1.valueOf(container)}?.PropertyT<${clientruntime_1.ClientRuntime.JsonString}>("${serializedName}"), out var ${tmpVar}) ? (string)${tmpVar} : (string)${defaultValue}`); } case media_types_1.KnownMediaType.Xml: { const xTmp = `__${text_manipulation_1.camelCase(['xml', ...text_manipulation_1.deconstruct(serializedName)])}`; return expression_1.toExpression(`If( ${expression_1.valueOf(container)}?.Element("${serializedName}"), out var ${xTmp}) ? (string)${xTmp} : (string)${defaultValue}`); } case media_types_1.KnownMediaType.Header: { // HttpResponseHeaders const tmp = `__${text_manipulation_1.camelCase(['header', ...text_manipulation_1.deconstruct(serializedName)])}`; return expression_1.toExpression(`System.Linq.Enumerable.FirstOrDefault(${serializedName}) is string ${tmp} ? ${tmp} : (string)${defaultValue}`); } } return expression_1.toExpression(`${defaultValue} /* deserializeFromContainerMember doesn't support '${mediaType}' ${__filename}*/`); } deserializeFromNode(mediaType, node, defaultValue) { try { const tmp = primitive_1.pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: // node should be a JsonString return expression_1.toExpression(`${node} is ${clientruntime_1.ClientRuntime.JsonString} ${tmp} ? (string)${tmp} : ${defaultValue}`); case media_types_1.KnownMediaType.Xml: return expression_1.toExpression(`${node} is ${mscorlib_1.System.Xml.Linq.XElement} ${tmp} ? (string)${tmp} : ${defaultValue}`); case media_types_1.KnownMediaType.Header: return expression_1.toExpression(`System.Linq.Enumerable.FirstOrDefault(${node}) is string ${tmp} ? ${tmp} : (string)${defaultValue}`); } } finally { primitive_1.popTempVar(); } return expression_1.toExpression(`null /* deserializeFromContainer doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression serialize this to a HttpContent */ serializeToContent(mediaType, value) { return expression_1.toExpression(`null /* serializeToContent doesn't support '${mediaType}' ${__filename}*/`); } serializeToNode(mediaType, value, serializedName) { switch (mediaType) { case media_types_1.KnownMediaType.Json: return expression_1.toExpression(`null != ${value} ? (${clientruntime_1.ClientRuntime.JsonNode}) new ${clientruntime_1.ClientRuntime.JsonString}(${value}) : null`); case media_types_1.KnownMediaType.Xml: return expression_1.toExpression(`null != ${value} ? new ${mscorlib_1.System.Xml.Linq.XElement}("${serializedName}",${value}) : null`); case media_types_1.KnownMediaType.Cookie: case media_types_1.KnownMediaType.QueryParameter: return expression_1.toExpression(`if (!string.IsNullOrEmpty(${value})) { queryParameters.Add($"${serializedName}={System.Uri.EscapeDataString(${value})}"); }`); case media_types_1.KnownMediaType.Header: case media_types_1.KnownMediaType.Text: case media_types_1.KnownMediaType.UriParameter: return expression_1.toExpression(`(string.IsNullOrEmpty(${value}) ? System.Uri.EscapeDataString(${value}) : ${mscorlib_1.System.String.Empty})`); } return expression_1.toExpression(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`); } /** emits an expression to deserialize content from a string */ deserializeFromString(mediaType, content, defaultValue) { return expression_1.toExpression(``); } serializeToContainerMember(mediaType, value, container, serializedName) { switch (mediaType) { case media_types_1.KnownMediaType.Json: return `AddIf( ${this.serializeToNode(mediaType, value, serializedName)}, "${serializedName}" ,${container}.Add );`; case media_types_1.KnownMediaType.Xml: return `AddIf( ${this.serializeToNode(mediaType, value, serializedName)}, ${container}.Add );`; case media_types_1.KnownMediaType.Header: // container : HttpRequestHeaders return this.isRequired ? `${expression_1.valueOf(container)}.Add("${serializedName}",${value}.ToString());` : if_1.If(`null != ${value}`, `${expression_1.valueOf(container)}.Add("${serializedName}",${value});`); case media_types_1.KnownMediaType.QueryParameter: // gives a name=value for use inside a c# template string($"foo{someProperty}") as a query parameter return this.isRequired ? `${serializedName}={${value}.ToString()}` : `{null == ${value} ? ${mscorlib_1.System.String.Empty} : $"${serializedName}={${value}.ToString()}"}`; case media_types_1.KnownMediaType.UriParameter: // gives a name=value for use inside a c# template string($"foo{someProperty}") as a query parameter return this.isRequired ? `(${serializedName}={${value}.ToString()})` : `(null == ${value} ? "": $"${serializedName}={${value}.ToString()}")`; } return (`/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`); } get declaration() { return 'string'; } validateValue(property) { return ` ${this.validateMinLength(property)} ${this.validateMaxLength(property)} ${this.validateRegex(property)} ${this.validateEnum(property)} `.trim(); } validatePresence(property) { return `await listener.AssertNotNull(${text_manipulation_1.nameof(property.value)},${property});`.trim(); } validateMinLength(property) { if (!this.schema.minLength) { return ''; } return `await listener.AssertMinimumLength(${text_manipulation_1.nameof(property.value)},${property},${this.schema.minLength});`; } validateMaxLength(property) { if (!this.schema.maxLength) { return ''; } return `await listener.AssertMaximumLength(${text_manipulation_1.nameof(property.value)},${property},${this.schema.maxLength});`; } validateRegex(property) { if (!this.schema.pattern) { return ''; } return `await listener.AssertRegEx(${text_manipulation_1.nameof(property.value)},${property},@"${this.schema.pattern}");`; } validateEnum(property) { if (!this.schema.enum || this.schema.enum.length === 0) { return ''; } return `await listener.AssertEnum(${text_manipulation_1.nameof(property.value)},${property},${this.schema.enum.joinWith((v) => `@"${v}"`)});`; } } exports.String = String; //# sourceMappingURL=string.js.map