UNPKG

@wuapi/generator

Version:
187 lines (186 loc) 7.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chance_1 = __importDefault(require("chance")); const lodash_1 = __importDefault(require("lodash")); class DemoGenerator { get entity() { return this.path.asEntityOf(this.project); } constructor(project, path) { this.project = project; this.path = path; this.chance = (0, chance_1.default)(); } // genericTypeName(type) { switch (type.type) { case "TBoolean": return "Boolean"; case "TInteger": return "Integer"; case "TLong": return "Long"; case "TID": return "Long"; case "TDouble": return "Double"; case "TString": return "String"; case "TURL": return "String"; case "TDateTime": return "Long"; case "TSSMap": return "HashMap<String, String>"; case "TEnum": return type.enu.name; case "TObject": return type.entity.name; case "TUnknown": return type.unknown; case "TList": return `List<${this.genericTypeName(type.member)}>`; default: throw new Error(`Unknown generic type "${type.type}".`); } } /** * Generate field value * * @param ft The $FieldType object preresenting this field. * @param config The demo configuration of this field. * * @returns The field value as string */ generateField(ft, config) { let fixed = config?.["fixed"]; if (fixed) { return fixed; } switch (ft.type) { case "TBoolean": return this.chance.bool().toString(); case "TLong": return this.chance.integer({ min: 0, max: 10000 }).toString() + "L"; case "TID": return this.chance.integer({ min: 1000000, max: 9000000 }).toString() + "L"; case "TDouble": return this.chance.floating({ min: 0, max: 100 }).toString(); case "TInteger": switch (config?.["style"]) { case "age": return this.chance.age().toString(); default: return this.chance.integer({ min: 0, max: 100 }).toString(); } case "TString": switch (config?.["style"]) { case "short": return `"${this.chance.word()}"`; case "long": return `"${this.chance.paragraph()}"`; case "name": return `"${this.chance.first()}"`; case "fullname": return `"${this.chance.name()}"`; case "phone": return `"${this.chance.phone()}"`; case "country": return `"${this.chance.country()}"`; default: return `"${this.chance.sentence()}"`; } case "TURL": switch (config?.["style"]) { case "avatar": return `"${this.chance.avatar({ protocol: 'https' })}"`; default: return `"${this.chance.url()}"`; } case "TDateTime": switch (config?.["style"]) { case "ms": return this.chance.hammertime().toString() + "L"; default: return this.chance.timestamp().toString() + "L"; } //case "TSSMap" : break case "TEnum": { let enuPath = ft.enu; let items = enuPath.asEnumOf(this.project).flat(); let itemName = items[this.chance.integer({ min: 0, max: items.length - 1 })].name; return `${enuPath.name}.${itemName}`; } //case "TObject" : break //case "TUnknown" : break //case "TList" : break default: throw new Error(`Unknown generic type "${ft.type}".`); } } /** * Write function getter of thie field. * * @param b The BraceCaller for output * @param isPublic true if the output function is public, private otherwise. */ asFunction(b, isPublic = true) { b.bra(`${isPublic ? "public" : "private"} ${this.path.name} get${this.path.name}`).add((b) => { this.asFunctionBody(b); }); } /** * Write function body to output * * @param b The BraceCaller for output * @param idx The index holder */ asFunctionBody(b, idx) { idx = idx ?? { n: 0 }; const idxN = idx.n; let fieldMap = {}; let fieldRef = {}; // 1st loop, find all fields, assemble generics this.entity.fromAncestorToMe(this.project, (ent) => { lodash_1.default.forIn(ent.genericMap, (f, key) => { const found = lodash_1.default.findKey(fieldMap, (o) => { return (o.type.type == "TUnknown") && (o.type.unknown == key); }); if (found) { fieldMap[found] = f; } }); lodash_1.default.forIn(ent.fieldsLocal, (f, key) => { fieldMap[key] = f; }); }); // 2nd loop, recursively generate objects. lodash_1.default.forIn(fieldMap, (f, key) => { switch (f.type.type) { case "TObject": fieldRef[key] = [++idx.n]; new DemoGenerator(this.project, f.type.entity).asFunctionBody(b, idx); break; case "TList": if (f.type.member.type == "TObject") { let member = f.type.member.entity; fieldRef[key] = []; for (let i = 0; i < 3; i++) { fieldRef[key].push(++idx.n); new DemoGenerator(this.project, member).asFunctionBody(b, idx); } } break; } }); // 3rd loop, write to output b(`${this.path.name} object${idxN} = new ${this.path.name}(); `); lodash_1.default.forIn(fieldMap, (f, key) => { switch (f.type.type) { case "TSSMap": b(`object${idxN}.${key} = new HashMap();`); for (let i = 0; i < 3; i++) { b(`object${idxN}.${key}.put("${this.chance.word()}", "${this.chance.word()}");`); } break; case "TObject": b(`object${idxN}.${key} = object${fieldRef[key][0]};`); break; case "TList": b(`object${idxN}.${key} = new ArrayList<${this.genericTypeName(f.type.member)}>();`); if (f.type.member.type == "TObject") { for (let i = 0; i < fieldRef[key].length; i++) { b(`object${idxN}.${key}.add(object${fieldRef[key][i]});`); } } else { for (let i = 0; i < 3; i++) { b(`object${idxN}.${key}.add(${this.generateField(f.type.member, f.config)});`); } } break; default: b(`object${idxN}.${key} = ${this.generateField(f.type, f.config)};`); break; } }); // Finally, write return clause. b(""); if (idxN == 0) { b("return object0;"); } } } exports.default = DemoGenerator;