Last active
July 29, 2020 15:33
-
-
Save james-turner/71028537fd106de907cf18206ca10075 to your computer and use it in GitHub Desktop.
Convert a json object example to cloudformation glue table column definition format [CAVEAT: data types are not perfect, you need a good value if you want the right type otherwise it defaults to string]
This file contains hidden or 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
| const fs = require('fs'); | |
| const cliArgs = process.argv.slice(2); | |
| const data = fs.readFileSync(cliArgs[0], 'UTF-8'); | |
| const tree = JSON.parse(data); | |
| const dfs = (todo, acc) => { | |
| if(todo.length === 0) return acc; | |
| const [name, node] = todo[0]; | |
| todo = todo.slice(1); | |
| if(node !== null && Array.isArray(node)){ | |
| return acc + [`${normalizeName(name)}array<${node.map(n => dfs([['',n]], ''))}>`, dfs(todo)].filter(_=>_).join(','); | |
| } else if(node !== null && typeof node === 'object'){ | |
| const keys = Object.keys(node); | |
| keys.forEach(k => { | |
| todo.unshift([k, node[k]]); | |
| }); | |
| return acc + `${normalizeName(name)}struct<${dfs(todo, '')}>`; | |
| } else { | |
| return [`${normalizeName(name)}${getType(node)}`, dfs(todo, '')].filter(_=>_).join(','); | |
| } | |
| }; | |
| const normalizeName = (name) => { | |
| if(name !== '') { | |
| return `${name.toLowerCase()}:` | |
| } | |
| return ''; | |
| }; | |
| const getType = (n) => { | |
| if(n === null) return 'string'; | |
| else { | |
| switch(typeof n){ | |
| case 'number': return 'int'; // could be a float so far to tell - maybe provide better parsing here. | |
| case 'boolean': return 'boolean'; | |
| default: return 'string'; // without any other info this might be the safest option for us. | |
| } | |
| } | |
| }; | |
| const keys = Object.keys(tree); | |
| console.log("Columns:") | |
| keys.forEach(k => { | |
| console.log(` - Name: ${k.toLowerCase()}`); | |
| console.log(` Type: ` + dfs([['',tree[k]]],'')); | |
| console.log(` Comment: FILL ME IN PLS`); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: