Skip to content

Commit

Permalink
feat: gen snake_case filenames for golang (#106)
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
  • Loading branch information
grant authored Nov 5, 2020
1 parent cae30bf commit 2712cd5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tools/quicktype-wrapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 2712cd5

Please sign in to comment.