Last active
August 29, 2015 14:04
-
-
Save mlconnor/a87ca81b0ca0c7493588 to your computer and use it in GitHub Desktop.
Concept for allowing JSON to inherit from other defs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var _ = require('lodash'); | |
var defs = fs.readFileSync('servicedefs.json', {encoding:'utf8'}); | |
var defsJson = JSON.parse(defs); | |
var indexedDefs = _.indexBy(defsJson, 'id'); | |
_.each(indexedDefs, function(def, index) { | |
var current = def; | |
while ( _.has(def, 'under') ) { | |
var ref = indexedDefs[def.under]; | |
if ( ! ref ) { | |
throw "could not resolve under [" + def.under + "]"; | |
} | |
delete def.under; | |
var underClone = _.clone(ref); | |
_.merge(underClone, def); | |
current = underClone; | |
} | |
indexedDefs[index] = current; | |
}); | |
console.log(JSON.stringify(indexedDefs,null,' ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment