UNPKG

ern-api-gen

Version:

Electrode Native API generator

136 lines 5.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const XmlExampleGenerator_1 = __importDefault(require("./XmlExampleGenerator")); const properties_1 = require("../models/properties"); const Json_1 = __importDefault(require("../java/Json")); const javaUtil_1 = require("../java/javaUtil"); class ExampleGenerator { constructor(examples) { this.examples = examples; } generate(examples, mediaTypes, property) { const output = []; const processedModels = javaUtil_1.newHashSet(); if (examples == null) { if (mediaTypes == null) { mediaTypes = ['application/json']; } for (const mediaType of mediaTypes) { const kv = javaUtil_1.newHashMap(['contentType', mediaType]); if (property != null && mediaType.startsWith('application/json')) { const rexample = this.resolvePropertyToExample(mediaType, property, processedModels); const example = JSON.stringify(rexample, null, 2); if (example != null) { kv.put('example', example); output.push(kv); } } else if (property != null && mediaType.startsWith('application/xml')) { const example = new XmlExampleGenerator_1.default(this.examples).toXml(property); if (example != null) { kv.put('example', example); output.push(kv); } } } } else { for (const [contentType, example] of javaUtil_1.asMap(examples)) { output.push(javaUtil_1.newHashMap(['contentType', contentType], ['example', Json_1.default.pretty(example)])); } } if (output.length === 0) { output.push(javaUtil_1.newHashMap(['output', 'none'])); } return JSON.stringify(output); } resolvePropertyToExample(mediaType, property, processedModels) { if (property.getExample() != null) { return property.getExample(); } else if (property instanceof properties_1.BooleanProperty) { return 'true'; } else if (property instanceof properties_1.ArrayProperty) { const innerType = property.getItems(); if (innerType != null) { return [ this.resolvePropertyToExample(mediaType, innerType, processedModels), ]; } } else if (property instanceof properties_1.DateProperty) { return '2000-01-23T04:56:07.000+00:00'; } else if (property instanceof properties_1.DateTimeProperty) { return '2000-01-23T04:56:07.000+00:00'; } else if (property instanceof properties_1.DecimalProperty) { return Number(1.3579).toPrecision(5); } else if (property instanceof properties_1.DoubleProperty) { return Number(3.149).toPrecision(3); } else if (property instanceof properties_1.FileProperty) { return ''; } else if (property instanceof properties_1.FloatProperty) { return Number(1.23).toPrecision(2); } else if (property instanceof properties_1.IntegerProperty) { return Number(123).toPrecision(1); } else if (property instanceof properties_1.LongProperty) { return Number(123456789).toPrecision(9); } else if (property instanceof properties_1.MapProperty) { const mp = javaUtil_1.newHashMap(); if (property.getName() != null) { mp.put(property.getName(), this.resolvePropertyToExample(mediaType, property.getAdditionalProperties(), processedModels)); } else { mp.put('key', this.resolvePropertyToExample(mediaType, property.getAdditionalProperties(), processedModels)); } return mp; } else if (property instanceof properties_1.ObjectProperty) { return '{}'; } else if (property instanceof properties_1.RefProperty) { const simpleName = property.getSimpleRef(); const model = this.examples.get(simpleName); if (model != null) { return this.resolveModelToExample(simpleName, mediaType, model, processedModels); } } else if (property instanceof properties_1.UUIDProperty) { return '046b6c7f-0b8a-43b9-b35d-6489e6daee91'; } else if (property instanceof properties_1.StringProperty) { return 'aeiou'; } return ''; } resolveModelToExample(name, mediaType, model, processedModels) { if (processedModels.contains(name)) { return ''; } if (model != null) { processedModels.add(name); const values = javaUtil_1.newHashMap(); if (model.getProperties() != null) { for (const [propertyName, property] of model.getProperties()) { values.put(propertyName, this.resolvePropertyToExample(mediaType, property, processedModels)); } } return values; } return ''; } } exports.default = ExampleGenerator; //# sourceMappingURL=ExampleGenerator.js.map