From 97c1d623304e4d584db1469d19c190ca2ae61ff5 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Thu, 5 Nov 2020 09:14:00 -0600 Subject: [PATCH] feat: gen snake_case filenames for golang Signed-off-by: Grant Timmerman --- tools/quicktype-wrapper/src/index.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tools/quicktype-wrapper/src/index.ts b/tools/quicktype-wrapper/src/index.ts index 94d49000..2c15bfc3 100644 --- a/tools/quicktype-wrapper/src/index.ts +++ b/tools/quicktype-wrapper/src/index.ts @@ -60,6 +60,22 @@ async function getJSONSchemasPaths(directory: string) { return paths; } +/** + * Gets a filename from a type (proto message *Data name) + * @param {string} typeName The type, like DocumentEventData + * @param {string} lang The language, like golang + * @returns {string} The filename, like document_event_data + */ +function getFilename(typeName: string, lang: string) { + if (lang === 'GOLANG') { + // Snake case + return typeName.split(/(?=[A-Z])/).join('_').toLowerCase(); + } else { + // Pascal case (default) + return typeName; + } +} + /** * Gets a list of tuples of all JSON schemas and code generated from them * @param directory The path to the directory with schemas. @@ -162,7 +178,7 @@ if (!module.parent) { await mkdirp(absFilePathDir); // Write file - const typeFilename = `${typeName}.${LANGUAGE_EXT[L]}`; + const typeFilename = `${getFilename(typeName, L)}.${LANGUAGE_EXT[L]}`; const absFilePath = `${absFilePathDir}/${typeFilename}`; writeFileSync(absFilePath, fileContentsMaybeWithLicenseHeader); bufferedOutput.push(