UNPKG

@microsoft.azure/autorest.incubator

Version:
154 lines 9.18 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"); let __tmpVar; let __max = 0; function pushTempVar() { if (!__tmpVar) { __tmpVar = 1; __max = 1; } else { __tmpVar++; __max++; } return `__${String.fromCharCode(123 - __max)}`; } exports.pushTempVar = pushTempVar; function popTempVar() { if (__tmpVar) { __tmpVar--; } if (__tmpVar === 0) { __tmpVar = undefined; __max = 0; } } exports.popTempVar = popTempVar; class Primitive { constructor(schema) { this.schema = schema; this.ToStringMethod = ".ToString(System.Globalization.CultureInfo.InvariantCulture)"; } /** validatePresence on primitives is generally not required; the nullability determines requiredness... */ validatePresence(property) { return ``; } get baseType() { return this.declaration.replace('?', ''); } castJsonTypeToPrimitive(tmpValue, defaultValue) { return `(${this.declaration})${tmpValue}`; } castXmlTypeToPrimitive(tmpValue, defaultValue) { return `(${this.declaration})${tmpValue}`; } 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<${this.jsonType}>("${serializedName}"), out var ${tmp}) ? ${this.castJsonTypeToPrimitive(tmp, defaultValue.value)} : ${defaultValue}`); } case media_types_1.KnownMediaType.Xml: { // XElement/XElement or XElement/XAttribute const tmp = `__${text_manipulation_1.camelCase(['xml', ...text_manipulation_1.deconstruct(serializedName)])}`; return expression_1.toExpression(this.isXmlAttribute ? `If( ${expression_1.valueOf(container)}?.Attribute("${serializedName}"), out var ${tmp}) ? ${this.castXmlTypeToPrimitive(tmp, defaultValue.value)} : ${defaultValue}` : `If( ${expression_1.valueOf(container)}?.Element("${serializedName}"), out var ${tmp}) ? ${this.castXmlTypeToPrimitive(tmp, defaultValue.value)} : ${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} ? ${this.baseType}.TryParse( ${tmp}, out ${this.baseType} ${tmp}Value ) ? ${tmp}Value : ${defaultValue} : ${defaultValue}`); } } return expression_1.toExpression(`${defaultValue} /* deserializeFromContainerMember doesn't support '${mediaType}' ${__filename} */`); } deserializeFromNode(mediaType, node, defaultValue) { try { const tmp = pushTempVar(); switch (mediaType) { case media_types_1.KnownMediaType.Json: // node should be a json type return expression_1.toExpression(`${node} is ${this.jsonType} ${tmp} ? ${this.castJsonTypeToPrimitive(tmp, defaultValue.value)} : ${defaultValue}`); case media_types_1.KnownMediaType.Xml: // XElement or XAttribute return expression_1.toExpression(this.isXmlAttribute ? `${node} is ${mscorlib_1.System.Xml.Linq.XAttribute} ${tmp} ? ${this.castXmlTypeToPrimitive(tmp, defaultValue.value)} : ${defaultValue}` : `${node} is ${mscorlib_1.System.Xml.Linq.XElement} ${tmp} ? ${this.castXmlTypeToPrimitive(tmp, defaultValue.value)}: ${defaultValue}`); case media_types_1.KnownMediaType.Header: return expression_1.toExpression(`System.Linq.Enumerable.FirstOrDefault(${node}) is string ${tmp} ? ${this.baseType}.TryParse( ${tmp}, out ${this.baseType} ${tmp}Value ) ? ${tmp}Value : ${defaultValue} : ${defaultValue}`); } } finally { popTempVar(); } return expression_1.toExpression(`null /* deserializeFromContainer doens't support '${mediaType}' ${__filename}*/`); } /** emits an expression to deserialize content from a string */ deserializeFromString(mediaType, content, defaultValue) { return expression_1.toExpression(``); } /** 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 this.isRequired ? expression_1.toExpression(`(${clientruntime_1.ClientRuntime.JsonNode}) new ${this.jsonType}(${value})`) : expression_1.toExpression(`null != ${value} ? (${clientruntime_1.ClientRuntime.JsonNode}) new ${this.jsonType}((${this.baseType})${value}) : null`); case media_types_1.KnownMediaType.Xml: return this.isRequired ? expression_1.toExpression(`new ${mscorlib_1.System.Xml.Linq.XElement}("${serializedName}",${value})`) : 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 this.isRequired ? expression_1.toExpression(`queryParameters.Add($"${serializedName}={${value}}");`) : expression_1.toExpression(`if (${value} != null) { queryParameters.Add($"${serializedName}={${value}}"); }`); case media_types_1.KnownMediaType.Header: case media_types_1.KnownMediaType.Text: case media_types_1.KnownMediaType.UriParameter: return expression_1.toExpression(this.isRequired ? `(${value}${this.ToStringMethod}` : `(null == ${value} ? ${mscorlib_1.System.String.Empty} : ((${this.baseType})${value})${this.ToStringMethod})`); } return expression_1.toExpression(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`); } serializeToContainerMember(mediaType, value, container, serializedName) { switch (mediaType) { case media_types_1.KnownMediaType.Json: // container : JsonObject return `AddIf( ${this.serializeToNode(mediaType, value, serializedName)}, "${serializedName}" ,${expression_1.valueOf(container)}.Add );`; case media_types_1.KnownMediaType.Xml: // container : XElement return `AddIf( ${this.serializeToNode(mediaType, value, serializedName)}, ${expression_1.valueOf(container)}.Add );`; case media_types_1.KnownMediaType.Header: // container : HttpRequestHeaders return this.isRequired ? `${expression_1.valueOf(container)}.Add("${serializedName}",${value}${this.ToStringMethod});` : if_1.If(`null != ${value}`, `${expression_1.valueOf(container)}.Add("${serializedName}",((${this.baseType})${value})${this.ToStringMethod});`); 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}.${this.ToStringMethod}}` : `{null == ${value} ? ${mscorlib_1.System.String.Empty} : $"${serializedName}={((${this.baseType})${value}).${this.ToStringMethod}}"}`; 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}.${this.ToStringMethod}}` : `{null == ${value} ? "": $"${serializedName}={((${this.baseType})${value}).${this.ToStringMethod}}"}`; } return (`/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`); } } exports.Primitive = Primitive; //# sourceMappingURL=primitive.js.map