UNPKG

@microsoft.azure/autorest.incubator

Version:
139 lines 7.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const schema_1 = require("../../common/code-model/schema"); const text_manipulation_1 = require("../../common/text-manipulation"); const message = require("../../csharp/lowlevel-generator/messages"); const array_1 = require("../../csharp/schema/array"); const binary_1 = require("../../csharp/schema/binary"); const boolean_1 = require("../../csharp/schema/boolean"); const byte_array_1 = require("../../csharp/schema/byte-array"); const char_1 = require("../../csharp/schema/char"); const date_1 = require("../../csharp/schema/date"); const date_time_1 = require("../../csharp/schema/date-time"); const duration_1 = require("../../csharp/schema/duration"); const enum_1 = require("../../csharp/schema/enum"); const integer_1 = require("../../csharp/schema/integer"); const object_1 = require("../../csharp/schema/object"); const string_1 = require("../../csharp/schema/string"); const Uuid_1 = require("../../csharp/schema/Uuid"); const wildcard_1 = require("../../csharp/schema/wildcard"); const known_format_1 = require("../../remodeler/known-format"); class SchemaDefinitionResolver { constructor() { this.cache = new Map(); } add(schema, value) { this.cache.set(schema.details.default.name, value); return value; } resolveTypeDeclaration(schema, required, state) { if (!schema) { throw new Error('SCHEMA MISSING?'); } // determine if we need a new model class for the type or just a known type object switch (schema.type) { case schema_1.JsonType.Array: // can be recursive! const elementType = this.resolveTypeDeclaration(schema.items, true, state.path('items')); return new array_1.ArrayOf(schema, required, elementType, schema.minItems, schema.maxItems, schema.uniqueItems); case schema_1.JsonType.Object: const result = this.cache.get(schema.details.default.name); if (result) { return result; } // can be recursive! // for certain, this should be a class of some sort. if (schema.additionalProperties && !text_manipulation_1.hasProperties(schema.properties)) { if (schema.additionalProperties === true) { // the object is a wildcard for all key/object-value pairs return new wildcard_1.UntypedWildcard(schema); } else { // the object is a wildcard for all key/<specific-type>-value pairs const wcSchema = this.resolveTypeDeclaration(schema.additionalProperties, false, state.path('additionalProperties')); return new wildcard_1.Wildcard(schema, wcSchema); } } // otherwise, if it has additionalProperties // it's a regular object, that has a catch-all for unspecified properties. // (handled in ModelClass itself) return this.add(schema, new object_1.ObjectImplementation(schema)); case schema_1.JsonType.String: switch (schema.format) { case known_format_1.StringFormat.Base64Url: case known_format_1.StringFormat.Byte: // member should be byte array // on wire format should be base64url return new byte_array_1.ByteArray(schema, required); case known_format_1.StringFormat.Binary: // represent as a stream // wire format is stream of bytes return new binary_1.Binary(schema, required); case known_format_1.StringFormat.Char: // a single character return new char_1.Char(schema, required); case known_format_1.StringFormat.Date: return new date_1.Date(schema, required); case known_format_1.StringFormat.DateTime: return new date_time_1.DateTime(schema, required); case known_format_1.StringFormat.DateTimeRfc1123: return new date_time_1.DateTime1123(schema, required); case known_format_1.StringFormat.Duration: return new duration_1.Duration(schema, required); case known_format_1.StringFormat.Uuid: return new Uuid_1.Uuid(schema, required); case known_format_1.StringFormat.Url: case known_format_1.StringFormat.Password: case known_format_1.StringFormat.None: case 'etag': case undefined: case null: if (schema.extensions['x-ms-enum']) { return new enum_1.EnumImplementation(schema, required); } if (schema.extensions['x-ms-header-collection-prefix']) { return new wildcard_1.Wildcard(schema, new string_1.String({}, required)); } // just a regular old string. return new string_1.String(schema, required); default: state.warning(`Schema with type:'${schema.type} and 'format:'${schema.format}' is not recognized.`, message.DoesNotSupportEnum); return new string_1.String(schema, required); } case schema_1.JsonType.Boolean: return new boolean_1.Boolean(schema, required); case schema_1.JsonType.Integer: switch (schema.format) { case known_format_1.IntegerFormat.Int64: case known_format_1.IntegerFormat.None: return new integer_1.Numeric(schema, required, required ? 'long' : 'long?'); case known_format_1.IntegerFormat.UnixTime: return new date_time_1.UnixTime(schema, required); case known_format_1.IntegerFormat.Int32: return new integer_1.Numeric(schema, required, required ? 'int' : 'int?'); } // fallback to int if the format isn't recognized return new integer_1.Numeric(schema, required, required ? 'int' : 'int?'); case schema_1.JsonType.Number: switch (schema.format) { case known_format_1.NumberFormat.None: case known_format_1.NumberFormat.Double: return new integer_1.Numeric(schema, required, required ? 'double' : 'double?'); case known_format_1.NumberFormat.Float: return new integer_1.Numeric(schema, required, required ? 'float' : 'float?'); case known_format_1.NumberFormat.Decimal: return new integer_1.Numeric(schema, required, required ? 'decimal' : 'decimal?'); } // fallback to float if the format isn't recognized return new integer_1.Numeric(schema, required, required ? 'float' : 'float?'); case undefined: // "any" case // this can happen when a model is just an all-of something else. (sub in the other type?) break; } state.error(`Schema '${schema.details.csharp.name}' is declared with invalid type '${schema.type}'`, message.UnknownJsonType); throw new Error('Unknown Model. Fatal.'); } } exports.SchemaDefinitionResolver = SchemaDefinitionResolver; //# sourceMappingURL=schema-resolver.js.map