node.js - Object Factory Node Module with nested Objects using Generators -


i'm writing object factory node module in es6. i'm using stamptit library https://github.com/stampit-org/stampit base objects started enhance them , return own objects through factory.

lets have these 2 files in '../../models/':

mybasejsondata.json

{    "addresses": {        "type": "addressjsondata.json",        "multi": true,        "required": true    },    .    .    . } 

addressjsondata.json

{    "street_name": "sample",    "zip_code": "75024"    .    .    . } 

this current code comments:

import stampit 'stampit'; import * fs 'fs';  // allows me run node's async internal functions generator function runnodefunc( gen, iter) {   (iter = gen((err, data) => (err && iter.throw(err)) ||               iter.next(data))).next(); }  // loop through object's keys , values function* objectentries(obj) {   let propkeys = reflect.ownkeys(obj);    for(let propkey of propkeys) {     yield [propkey, obj[propkey]];   } }  // function coroutine(generatorfunction) { //   return function (...args) { //     let generatorobject = generatorfunction(...args); //     generatorobject.next(); //     return generatorobject; //   }; // }  function createmodel(defmodel) {   let model = stampit();   let customtypes = new map(defmodel.linked_custom_types);    for(let [key, value] of objectentries(defmodel)) {     if(customtypes.has(value.type)) {       // not work here because runnodefunc in getmodels       // not pause execution       let cusmod = getmodel(value.type);       model = model.props({[key]: cusmod});     } else {       // if it's multi type create array       model = model.static({[key]: value.multi ? [] : ''});     }   } }  function getmodel(filename) {   console.log(filename);   // ideally, function allow external yield   runnodefunc(function* readfile(resume) {     let mod = yield fs.readfile(`../../models/${filename}.json`,                                 resume);     createmodel(json.parse(mod));   }); }  getmodel('mybasejsondata'); // export default bodhimodel; 

basically, each 1 of json files going used construct new object. i'm doing simple mappings using subproperties of each file(for example, addresses.multi tells me take many objects of "addressjsondata" type), goal add more complexity objects.

i'm not sure using 'stampit' library, create own objects , feels redundant decision can change later.

i have sort of recursive process create these complex objects. know can implement

yield *createmodel(subtype) 

inside createmodel function this:

function createmodel(filename) {   let model = stampit();   // call getmodels here! *1   let customtypes = new map(defmodel.linked_custom_types);    for(let [key, value] of objectentries(defmodel)) {     if(customtypes.has(value.type)) {       // add here *2       let cusmod = yield *createmodel(value.type);       model = model.props({[key]: cusmod});     } else {       model = model.static({[key]: value.multi ? [] : ''});     }   } } 

by adding *2, i'm passing control. i've used tree traversals etc. tricky part in *1 i'm using function surrounded helper function use generator fetch files. in not pause execution since getmodels function not generator itself.

i've tried many ways, , haven't found how can keep helper function runnodefunc , implement recursive generator. if have bite bullet , wrap in callback will, want ask if method of object creation optimal , if i'm doing things can simplified. i'm concerned have generators create object module supposed return.

i forgot mention, there reference "linked_custom_types" in createmodel function. these nested objects particular main model being generated.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -