@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
87 lines • 4.55 kB
JavaScript
;
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 primitive_1 = require("../../csharp/schema/primitive");
const if_1 = require("../../csharp/code-dom/statements/if");
class ByteArray {
constructor(schema, isRequired) {
this.schema = schema;
this.isRequired = isRequired;
this.isXmlAttribute = false;
}
get declaration() {
return `byte[]`;
}
/** 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.Xml: {
const xTmp = `__${text_manipulation_1.camelCase(['xml', ...text_manipulation_1.deconstruct(serializedName)])}`;
return expression_1.toExpression(`If( ${expression_1.valueOf(container)}?.Element("${serializedName}")?.Value, out var ${xTmp}) ? System.Convert.FromBase64String(${xTmp}) : ${defaultValue}`);
}
case media_types_1.KnownMediaType.Header: {
const tmp = `__${text_manipulation_1.camelCase(['header', ...text_manipulation_1.deconstruct(serializedName)])}`;
return expression_1.toExpression(`System.Linq.Enumerable.FirstOrDefault(${serializedName}) is string ${tmp} ? System.Convert.FromBase64String(${tmp}) : ${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();
switch (mediaType) {
case media_types_1.KnownMediaType.Header: { // node is an array of string values
return expression_1.toExpression(`System.Linq.Enumerable.FirstOrDefault(${node}) is string ${tmp} ? System.Convert.FromBase64String(${tmp}) : ${defaultValue}`);
}
}
}
finally {
primitive_1.popTempVar();
}
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) {
try {
const b = primitive_1.pushTempVar();
switch (mediaType) {
case media_types_1.KnownMediaType.Xml: {
return `AddIf( null != ${value} ? new System.Xml.Linq.XElement("${serializedName}", System.Convert.ToBase64String(${value})) : null, ${container}.Add);`;
}
case media_types_1.KnownMediaType.Header: {
return if_1.If(`null != ${value}`, `${expression_1.valueOf(container)}.Add("${serializedName}", System.Convert.ToBase64String(${value}));`);
}
}
}
finally {
primitive_1.popTempVar();
}
return `/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`;
}
validateValue(property) {
return ``;
}
validatePresence(property) {
if (this.isRequired) {
return `await listener.AssertNotNull(${text_manipulation_1.nameof(property.value)},${property});`.trim();
}
return ``;
}
}
exports.ByteArray = ByteArray;
//# sourceMappingURL=byte-array.js.map