Created
June 6, 2018 07:58
-
-
Save simon04/5e85a2974f3aafb3f831c33d45b7f0e6 to your computer and use it in GitHub Desktop.
Convert Plot.ly schema to Java POJOs
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 schema = JSON.parse(require('fs').readFileSync('dist/plot-schema.json')); | |
generate("layout", schema.layout.layoutAttributes); | |
generate("scatter", schema.traces.scatter.attributes); | |
generate("annotations", schema.layout.layoutAttributes.annotations); | |
generate("shapes", schema.layout.layoutAttributes.shapes); | |
function generate(key, value, indent = 0) { | |
if (key[0] === '_') { | |
return; | |
} else if (value.valType === 'enumerated') { | |
if (value.description) | |
console.log(' '.repeat(indent) + `/** ${value.description} */`); | |
console.log(' '.repeat(indent) + `${capitalize(key)} ${key};`); | |
console.log(' '.repeat(indent) + `enum ${capitalize(key)} {`); | |
const values = value.values.filter(v => typeof v !== 'number').map(v => [ | |
' '.repeat(indent + 2) + `@com.google.gson.annotations.SerializedName("${v}")`, | |
' '.repeat(indent + 2) + new String(v).replace(/[ -]/g, '_').replace(/^$/, '_').toUpperCase(), | |
].join('\n') | |
).join(',\n'); | |
console.log(values); | |
console.log(' '.repeat(indent) + `}`); | |
} else if (value.valType) { | |
const types = { | |
angle: 'Number', | |
any: 'Object', | |
color: 'java.awt.Color', | |
colorlist: 'java.awt.Color[]', | |
data_array: 'Object[]', | |
flaglist: 'Object', | |
info_array: 'Object[]', | |
}; | |
const valType = types[value.valType] || capitalize(value.valType); | |
if (value.description) | |
console.log(' '.repeat(indent) + `/** ${value.description} */`); | |
console.log(' '.repeat(indent) + `${valType} ${key};`); | |
} else if (typeof value === 'object') { | |
if (value.description) | |
console.log(' '.repeat(indent) + `/** ${value.description} */`); | |
console.log(' '.repeat(indent) + `${capitalize(key)} ${key};`); | |
console.log(' '.repeat(indent) + `static class ${capitalize(key)} {`); | |
Object.keys(value).forEach(k => generate(k, value[k], indent + 2)); | |
console.log(' '.repeat(indent) + `}`); | |
} | |
} | |
function capitalize(s) { | |
return s.replace(/(?:^|\s)\S/g, a => a.toUpperCase()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment