package tst import ( "fmt" "strings" . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common" // . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/gen" TS "git.eugeniocarvalho.dev/eugeniucarvalho/gg/generators/typescript" ) func GenSchemas(p *Project) error { var ( attribName string propName string attribs []TS.CodeInterface attrib *TS.Group entityInfo *EntityInfo arrayProperties = []*Propertie{} ) appendBasePatchClass() for _, entity := range p.Schemas { attribs = []TS.CodeInterface{} arrayProperties = []*Propertie{} entityInfo = p.ResponseEntity(entity.ID) if entityInfo.IsGeneric { continue } for _, meta := range entity.Properties { propName = strings.Title(meta.ID) meta.FillTags(p, propName) // Registra a relaao entre as entidades // if meta.Relation { // SR.Add(&Relation{ // Source: meta.GetType(), // Target: entity.ID, // Attr: strings.Replace(meta.Tags["bson"], ",omitempty", "", 1), // Collection: entity.Collection, // }) // } if meta.Array { arrayProperties = append(arrayProperties, meta) } // Adiciona as tags caso sejam definidas if meta.Tags != nil { if name, ok := meta.Tags["json"]; ok { attribName = strings.Split(name, ",")[0] if attribName == "-" { attribName = meta.ID } } } else { attribName = meta.ID } if attribName == "id" { attribName = "_id" } meta.Name = attribName // Adiciona a crescricao como comentario attrib = &TS.Group{} if meta.Description != "" { // propertie.Comment(meta.Description) attrib.Comment(meta.Description) } if attribName != "" { // attrib.Add(TS.Public().Id(attribName).Op(":").Id(TS.ConvType(meta.Type))) attrib.Add(TS.Id(attribName).Op(":").Id(TS.ConvType(meta.Type))) if meta.Array { attrib.Add(TS.Index()) } attrib.Endl() attribs = append(attribs, attrib) } } // Fim do loop dos atributos // attribs = append(attribs, TS.Constructor(TS.Raw("opts: any")).Block(TS.Raw("opts && Object.assign(this,opts);"))) module.Line().Export().Class().Id(strings.Title(entity.ID)).Block( // module.Line().Export().Interface().Id(strings.Title(entity.ID)).Block( // TS.Constructor(TS.Id(`options: `)), attribs..., ).Line() createPathClass(entity, arrayProperties) } // Fim do for de schema return nil } func createPathClass(entity *Entity, arrayProperties []*Propertie) { module. Line(). Export(). Class(). Id(strings.Title(entity.ID + "Patch")). Extends(). Id("BasePatch"). BlockFun(func(g *TS.Group) { createClassProperties(g, entity, arrayProperties) createSetInitializeMethod(g, entity, arrayProperties) // createPatchMethod(g, entity, arrayProperties) createAddAndRemoveMethods(g, entity, arrayProperties) // g.Raw(` // protected emit(timeout = 0) { // super.emit(this.Patchs(), timeout); // } // `) }). Line() } func createClassProperties(g *TS.Group, entity *Entity, arrayProperties []*Propertie) { for _, meta := range arrayProperties { g.Line().Add( TS.Raw(fmt.Sprintf("%s = new Map();", meta.ID, strings.Title(entity.ID))), ) } } func createSetInitializeMethod(g *TS.Group, entity *Entity, arrayProperties []*Propertie) { destructuring := []string{} for _, meta := range arrayProperties { destructuring = append(destructuring, fmt.Sprintf("%s=[]", meta.ID)) } destructuringString := strings.Join(destructuring, ",") g.Add(TS. Line(). Line(). // Do(). Constructor(TS.Raw(fmt.Sprintf("{%s} : %s", destructuringString, entity.ID))). BlockFun(func(block *TS.Group) { // block.Add(TS.Raw(`this.instance = instance;`).Line()) block.Add(TS.Raw(`super();`).Line()) // .Raw(`const {`) for _, meta := range arrayProperties { // block.Add(TS.Raw(fmt.Sprintf("if ($instance.%s) { this._set(this.%s, $instance.%s, '',-1);}", meta.ID, meta.ID, meta.ID)).Line()) block.Add(TS.Raw(fmt.Sprintf("if (%s.length) { this._set(this.%s, %s, '',-1);}", meta.ID, meta.ID, meta.ID)).Line()) } }), ) } func createPatchMethod(g *TS.Group, entity *Entity, arrayProperties []*Propertie) { g.Add(TS.Line(). Raw(`Patchs()`). BlockFun(func(block *TS.Group) { block.Add(TS.Raw(`const patchs = {`)) for _, meta := range arrayProperties { block.Add(TS. Line(). Raw(fmt.Sprintf("Add%s : [],", strings.Title(meta.ID))). Line(). Raw(fmt.Sprintf("Remove%s : [],", strings.Title(meta.ID))), ) } block. Line(). Raw(`};`). Line(). Raw(`return patchs;`). Line() }), ) } func createAddAndRemoveMethods(g *TS.Group, entity *Entity, arrayProperties []*Propertie) { const timeout = 50 for _, meta := range arrayProperties { g.Add(TS. Line(). Id(fmt.Sprintf("Add%s", strings.Title(meta.ID))). Params(TS.Id("itens"), TS.Id(fmt.Sprintf("timeout = %d", timeout))). // Params(TS.Id("itens"), TS.Id("timeout")). Block( TS.Raw(fmt.Sprintf("this._set(this.%s, itens, 'add', timeout);return this;", meta.ID)), ). Line(). Id(fmt.Sprintf("Remove%s", strings.Title(meta.ID))). Params(TS.Id("itens"), TS.Id(fmt.Sprintf("timeout = %d", timeout))). // Params(TS.Id("itens"), TS.Id("timeout")). Block( TS.Raw(fmt.Sprintf("this._set(this.%s, itens, 'remove', timeout);return this;", meta.ID)), ), ) } } func appendBasePatchClass() { module.Line().Raw(` export class BasePatch { protected $set = {}; protected $subscription: any; public locked = false; public change$ = new EventEmitter(); public $transformPatch = (patch) => patch; Set(options, timeout = 0) { this.$set = {...this.$set, ...options}; if (timeout >= 0){ this.emit(this.patch(), timeout); } } protected emit(patch:any , timeout = 0) { if (this.$subscription) { clearTimeout(this.$subscription); } this.locked = true; this.$subscription = setTimeout(() => this.change$.emit(patch), timeout); } protected __set(list, add, rem) { list.forEach(element => { const item = element.item; switch (element.action) { case 'add': add.push(item); break; case 'remove': if (element.type !== 'new') { rem.push(item._id || item); } break; } }); } public from(patches, timeout = 50){ const {$set = {}} = patches; this.Set($set, timeout); Object.keys(patches).map(prop => { if (prop.startsWith('$')) { return;} if (typeof this[prop] === 'function') { this[prop](patches[prop], timeout); } }); } protected _set(_map: Map, itens, action, timeout = 50) { (itens || []).map(item => { const id = (item._id || item); const has = _map.has(id) , el = _map.get(id); switch (action) { case 'add': if (!has) { _map.set(id, { item: item, action: 'add', type: 'new', }); } else if (el.action === 'remove') { el.action = 'add'; } break; case 'remove': if (has) { el.action = 'remove'; break; } // tslint:disable-next-line:no-switch-case-fall-through default: _map.set(id, { item, action, type: '', }); } }); if (timeout >= 0){ this.emit(this.patch(), timeout); } } patch() { const $this = this; const patchs = {}; if (Object.getOwnPropertyNames(this.$set).length) { patchs['$set'] = this.$set; } Object.keys($this).map(prop => { if (prop.startsWith('$')) { return; } const adds = [], remove = []; const captalized = prop[0].toUpperCase() + prop.slice(1); if ($this[prop].size) { this.__set($this[prop], adds, remove); if (adds.length){ patchs[`).Raw("`Add${captalized}`").Raw(`] = adds; } if (remove.length){ patchs[`).Raw("`Remove${captalized}`").Raw(`] = remove; } } }); return this.$transformPatch(patchs); } } `) } // public Patchs() { // const patchs = { // AddMethods: [], // AddSamples: [], // AddCaracterizations: [], // RemoveMethods: [], // RemoveSamples: [], // RemoveCaracterizations: [], // $set: this.$set, // }; // console.log('paths', this); // this.__set(this._samples, patchs.AddSamples, patchs.RemoveSamples); // this.__set(this._methods, patchs.AddMethods, patchs.RemoveMethods); // patchs.RemoveSamples.map(s => { // this.RemoveCaracterizations(this._form.caracterizations.filter(c => { // return c.sample === s.id; // })); // }); // patchs.RemoveMethods.map(m => { // this.RemoveCaracterizations(this._form.caracterizations.filter(c => { // return c.method === m.id; // })); // }); // this.__set(this._caracterizations, patchs.AddCaracterizations, patchs.RemoveCaracterizations); // return patchs; // } // public Patchs(patchs) { // const patchs = { // AddMethods: [], // AddSamples: [], // AddCaracterizations: [], // RemoveMethods: [], // RemoveSamples: [], // RemoveCaracterizations: [], // $set: this.$set, // }; // console.log('paths', this); // this.__set(this._samples, patchs.AddSamples, patchs.RemoveSamples); // this.__set(this._methods, patchs.AddMethods, patchs.RemoveMethods); // patchs.RemoveSamples.map(s => { // this.RemoveCaracterizations(this._form.caracterizations.filter(c => { // return c.sample === s.id; // })); // }); // patchs.RemoveMethods.map(m => { // this.RemoveCaracterizations(this._form.caracterizations.filter(c => { // return c.method === m.id; // })); // }); // this.__set(this._caracterizations, patchs.AddCaracterizations, patchs.RemoveCaracterizations); // return patchs; // } // export class FormPatch { // $set = {}; // _form: CaracterizationForm; // _samples = new Map(); // _methods = new Map(); // _caracterizations = new Map(); // setSolicitation(form: CaracterizationForm) { // // this._set(this._methods, form.methods.map(m => { return { id: m.id } }), ''); // this._form = form; // this._set(this._samples, form.samples, ''); // this._set(this._methods, form.methods, ''); // this._set(this._caracterizations, form.caracterizations, ''); // } // protected __set(list, add, rem) { // list.forEach((i) => { // switch (i.action) { // case 'add': // add.push(i.item); // break; // case 'remove': // (i.type !== 'new') && rem.push(i.item); // break; // } // }); // } // public Patchs() { // const patchs = { // AddMethods: [], // AddSamples: [], // AddCaracterizations: [], // RemoveMethods: [], // RemoveSamples: [], // RemoveCaracterizations: [], // $set: this.$set, // }; // console.log('paths', this); // this.__set(this._samples, patchs.AddSamples, patchs.RemoveSamples); // this.__set(this._methods, patchs.AddMethods, patchs.RemoveMethods); // patchs.RemoveSamples.map(s => { // this.RemoveCaracterizations(this._form.caracterizations.filter(c => { // return c.sample === s.id; // })); // }); // patchs.RemoveMethods.map(m => { // this.RemoveCaracterizations(this._form.caracterizations.filter(c => { // return c.method === m.id; // })); // }); // this.__set(this._caracterizations, patchs.AddCaracterizations, patchs.RemoveCaracterizations); // return patchs; // } // protected _set(_map: Map, itens, action) { // (itens || []).map(item => { // const id = item.id || (item.method + item.sample); // const has = _map.has(id) // , el = _map.get(id); // switch (action) { // case 'add': // if (!has) { // _map.set(id, { // item: item, // action: 'add', // type: 'new', // }); // } else if (el.action === 'remove') { // el.action = 'add'; // } // break; // case 'remove': // if (has) { // el.action = 'remove'; // } // break; // default: // _map.set(id, { // item: item, // action: '', // type: '', // }); // } // }); // } // AddMethods(itens) { // this._set(this._methods, itens, 'add'); // } // AddSamples(itens) { // this._set(this._samples, itens, 'add'); // } // AddCaracterizations(itens) { // this._set(this._caracterizations, itens, 'add'); // } // RemoveMethods(itens) { // this._set(this._methods, itens, 'remove'); // } // RemoveSamples(itens) { // this._set(this._samples, itens, 'remove'); // } // RemoveCaracterizations(itens) { // this._set(this._caracterizations, itens, 'remove'); // } // }