Skip to content

Commit

Permalink
add a header
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Mar 30, 2024
1 parent 2a9f226 commit 31c70f7
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 23 deletions.
5 changes: 5 additions & 0 deletions __fixtures__/output/parser/enums.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export enum OverridingKind {
OVERRIDING_NOT_SET = 0,
OVERRIDING_USER_VALUE = 1,
Expand Down
5 changes: 5 additions & 0 deletions __fixtures__/output/parser/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type OverridingKind = "OVERRIDING_NOT_SET" | "OVERRIDING_USER_VALUE" | "OVERRIDING_SYSTEM_VALUE";
export type QuerySource = "QSRC_ORIGINAL" | "QSRC_PARSER" | "QSRC_INSTEAD_RULE" | "QSRC_QUAL_INSTEAD_RULE" | "QSRC_NON_INSTEAD_RULE";
export type SortByDir = "SORTBY_DEFAULT" | "SORTBY_ASC" | "SORTBY_DESC" | "SORTBY_USING";
Expand Down
5 changes: 5 additions & 0 deletions __fixtures__/output/parser/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type EnumType = "OverridingKind" | "QuerySource" | "SortByDir" | "SortByNulls" | "SetQuantifier" | "A_Expr_Kind" | "RoleSpecType" | "TableLikeOption" | "DefElemAction" | "PartitionStrategy" | "PartitionRangeDatumKind" | "RTEKind" | "WCOKind" | "GroupingSetKind" | "CTEMaterialize" | "SetOperation" | "ObjectType" | "DropBehavior" | "AlterTableType" | "GrantTargetType" | "VariableSetKind" | "ConstrType" | "ImportForeignSchemaType" | "RoleStmtType" | "FetchDirection" | "FunctionParameterMode" | "TransactionStmtKind" | "ViewCheckOption" | "DiscardMode" | "ReindexObjectType" | "AlterTSConfigType" | "PublicationObjSpecType" | "AlterPublicationAction" | "AlterSubscriptionType" | "OnCommitAction" | "ParamKind" | "CoercionContext" | "CoercionForm" | "BoolExprType" | "SubLinkType" | "RowCompareType" | "MinMaxOp" | "SQLValueFunctionOp" | "XmlExprOp" | "XmlOptionType" | "JsonEncoding" | "JsonFormatType" | "JsonConstructorType" | "JsonValueType" | "NullTestType" | "BoolTestType" | "CmdType" | "JoinType" | "AggStrategy" | "AggSplit" | "SetOpCmd" | "SetOpStrategy" | "OnConflictAction" | "LimitOption" | "LockClauseStrength" | "LockWaitPolicy" | "LockTupleMode" | "KeywordKind" | "Token";
export const getEnumValue = (enumType: EnumType, key: string | number) => {
switch (enumType) {
Expand Down
5 changes: 5 additions & 0 deletions __fixtures__/output/undef/enums.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export enum OverridingKind {
OVERRIDING_KIND_UNDEFINED = 0,
OVERRIDING_NOT_SET = 1,
Expand Down
5 changes: 5 additions & 0 deletions __fixtures__/output/undef/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type OverridingKind = "OVERRIDING_KIND_UNDEFINED" | "OVERRIDING_NOT_SET" | "OVERRIDING_USER_VALUE" | "OVERRIDING_SYSTEM_VALUE";
export type QuerySource = "QUERY_SOURCE_UNDEFINED" | "QSRC_ORIGINAL" | "QSRC_PARSER" | "QSRC_INSTEAD_RULE" | "QSRC_QUAL_INSTEAD_RULE" | "QSRC_NON_INSTEAD_RULE";
export type SortByDir = "SORT_BY_DIR_UNDEFINED" | "SORTBY_DEFAULT" | "SORTBY_ASC" | "SORTBY_DESC" | "SORTBY_USING";
Expand Down
5 changes: 5 additions & 0 deletions __fixtures__/output/undef/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type EnumType = "OverridingKind" | "QuerySource" | "SortByDir" | "SortByNulls" | "SetQuantifier" | "A_Expr_Kind" | "RoleSpecType" | "TableLikeOption" | "DefElemAction" | "PartitionStrategy" | "PartitionRangeDatumKind" | "RTEKind" | "WCOKind" | "GroupingSetKind" | "CTEMaterialize" | "SetOperation" | "ObjectType" | "DropBehavior" | "AlterTableType" | "GrantTargetType" | "VariableSetKind" | "ConstrType" | "ImportForeignSchemaType" | "RoleStmtType" | "FetchDirection" | "FunctionParameterMode" | "TransactionStmtKind" | "ViewCheckOption" | "DiscardMode" | "ReindexObjectType" | "AlterTSConfigType" | "PublicationObjSpecType" | "AlterPublicationAction" | "AlterSubscriptionType" | "OnCommitAction" | "ParamKind" | "CoercionContext" | "CoercionForm" | "BoolExprType" | "SubLinkType" | "RowCompareType" | "MinMaxOp" | "SQLValueFunctionOp" | "XmlExprOp" | "XmlOptionType" | "JsonEncoding" | "JsonFormatType" | "JsonConstructorType" | "JsonValueType" | "NullTestType" | "BoolTestType" | "CmdType" | "JoinType" | "AggStrategy" | "AggSplit" | "SetOpCmd" | "SetOpStrategy" | "OnConflictAction" | "LimitOption" | "LockClauseStrength" | "LockWaitPolicy" | "LockTupleMode" | "KeywordKind" | "Token";
export const getEnumValue = (enumType: EnumType, key: string | number) => {
switch (enumType) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default async (argv) => {
includeUtils: argv.hasOwnProperty('includeUtils') ? argv.includeUtils : true,
includeEnumTypeUnion: argv.hasOwnProperty('includeEnumTypeUnion') ? argv.includeEnumTypeUnion : true,
includeEnums: argv.hasOwnProperty('includeEnums') ? argv.includeEnums : true,
includeHeader: argv.hasOwnProperty('includeHeader') ? argv.includeHeader : true,
optionalFields: argv.hasOwnProperty('optionalFields') ? argv.optionalFields : true,
removeUndefinedAt0: argv.hasOwnProperty('removeUndefinedAt0') ? argv.removeUndefinedAt0 : true,
parser: {
Expand Down
1 change: 0 additions & 1 deletion packages/parser/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ __tests__
.travis.yml
.vscode
CHANGELOG.md
package.json
42 changes: 36 additions & 6 deletions packages/parser/__tests__/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
exports[`keep undefined 1`] = `
[
{
"code": "export enum OverridingKind {
"code": "/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export enum OverridingKind {
OVERRIDING_KIND_UNDEFINED = 0,
OVERRIDING_NOT_SET = 1,
OVERRIDING_USER_VALUE = 2,
Expand Down Expand Up @@ -3415,7 +3420,12 @@ export enum Token {
"file": "enums2str.json",
},
{
"code": "export type OverridingKind = "OVERRIDING_KIND_UNDEFINED" | "OVERRIDING_NOT_SET" | "OVERRIDING_USER_VALUE" | "OVERRIDING_SYSTEM_VALUE";
"code": "/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type OverridingKind = "OVERRIDING_KIND_UNDEFINED" | "OVERRIDING_NOT_SET" | "OVERRIDING_USER_VALUE" | "OVERRIDING_SYSTEM_VALUE";
export type QuerySource = "QUERY_SOURCE_UNDEFINED" | "QSRC_ORIGINAL" | "QSRC_PARSER" | "QSRC_INSTEAD_RULE" | "QSRC_QUAL_INSTEAD_RULE" | "QSRC_NON_INSTEAD_RULE";
export type SortByDir = "SORT_BY_DIR_UNDEFINED" | "SORTBY_DEFAULT" | "SORTBY_ASC" | "SORTBY_DESC" | "SORTBY_USING";
export type SortByNulls = "SORT_BY_NULLS_UNDEFINED" | "SORTBY_NULLS_DEFAULT" | "SORTBY_NULLS_FIRST" | "SORTBY_NULLS_LAST";
Expand Down Expand Up @@ -5545,7 +5555,12 @@ export interface ScanToken {
"file": "types.ts",
},
{
"code": "export type EnumType = "OverridingKind" | "QuerySource" | "SortByDir" | "SortByNulls" | "SetQuantifier" | "A_Expr_Kind" | "RoleSpecType" | "TableLikeOption" | "DefElemAction" | "PartitionStrategy" | "PartitionRangeDatumKind" | "RTEKind" | "WCOKind" | "GroupingSetKind" | "CTEMaterialize" | "SetOperation" | "ObjectType" | "DropBehavior" | "AlterTableType" | "GrantTargetType" | "VariableSetKind" | "ConstrType" | "ImportForeignSchemaType" | "RoleStmtType" | "FetchDirection" | "FunctionParameterMode" | "TransactionStmtKind" | "ViewCheckOption" | "DiscardMode" | "ReindexObjectType" | "AlterTSConfigType" | "PublicationObjSpecType" | "AlterPublicationAction" | "AlterSubscriptionType" | "OnCommitAction" | "ParamKind" | "CoercionContext" | "CoercionForm" | "BoolExprType" | "SubLinkType" | "RowCompareType" | "MinMaxOp" | "SQLValueFunctionOp" | "XmlExprOp" | "XmlOptionType" | "JsonEncoding" | "JsonFormatType" | "JsonConstructorType" | "JsonValueType" | "NullTestType" | "BoolTestType" | "CmdType" | "JoinType" | "AggStrategy" | "AggSplit" | "SetOpCmd" | "SetOpStrategy" | "OnConflictAction" | "LimitOption" | "LockClauseStrength" | "LockWaitPolicy" | "LockTupleMode" | "KeywordKind" | "Token";
"code": "/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type EnumType = "OverridingKind" | "QuerySource" | "SortByDir" | "SortByNulls" | "SetQuantifier" | "A_Expr_Kind" | "RoleSpecType" | "TableLikeOption" | "DefElemAction" | "PartitionStrategy" | "PartitionRangeDatumKind" | "RTEKind" | "WCOKind" | "GroupingSetKind" | "CTEMaterialize" | "SetOperation" | "ObjectType" | "DropBehavior" | "AlterTableType" | "GrantTargetType" | "VariableSetKind" | "ConstrType" | "ImportForeignSchemaType" | "RoleStmtType" | "FetchDirection" | "FunctionParameterMode" | "TransactionStmtKind" | "ViewCheckOption" | "DiscardMode" | "ReindexObjectType" | "AlterTSConfigType" | "PublicationObjSpecType" | "AlterPublicationAction" | "AlterSubscriptionType" | "OnCommitAction" | "ParamKind" | "CoercionContext" | "CoercionForm" | "BoolExprType" | "SubLinkType" | "RowCompareType" | "MinMaxOp" | "SQLValueFunctionOp" | "XmlExprOp" | "XmlOptionType" | "JsonEncoding" | "JsonFormatType" | "JsonConstructorType" | "JsonValueType" | "NullTestType" | "BoolTestType" | "CmdType" | "JoinType" | "AggStrategy" | "AggSplit" | "SetOpCmd" | "SetOpStrategy" | "OnConflictAction" | "LimitOption" | "LockClauseStrength" | "LockWaitPolicy" | "LockTupleMode" | "KeywordKind" | "Token";
export const getEnumValue = (enumType: EnumType, key: string | number) => {
switch (enumType) {
case "OverridingKind":
Expand Down Expand Up @@ -10028,7 +10043,12 @@ export const getEnumValue = (enumType: EnumType, key: string | number) => {
exports[`parser 1`] = `
[
{
"code": "export enum OverridingKind {
"code": "/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export enum OverridingKind {
OVERRIDING_NOT_SET = 0,
OVERRIDING_USER_VALUE = 1,
OVERRIDING_SYSTEM_VALUE = 2,
Expand Down Expand Up @@ -13254,7 +13274,12 @@ export enum Token {
"file": "enums2str.json",
},
{
"code": "export type OverridingKind = "OVERRIDING_NOT_SET" | "OVERRIDING_USER_VALUE" | "OVERRIDING_SYSTEM_VALUE";
"code": "/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type OverridingKind = "OVERRIDING_NOT_SET" | "OVERRIDING_USER_VALUE" | "OVERRIDING_SYSTEM_VALUE";
export type QuerySource = "QSRC_ORIGINAL" | "QSRC_PARSER" | "QSRC_INSTEAD_RULE" | "QSRC_QUAL_INSTEAD_RULE" | "QSRC_NON_INSTEAD_RULE";
export type SortByDir = "SORTBY_DEFAULT" | "SORTBY_ASC" | "SORTBY_DESC" | "SORTBY_USING";
export type SortByNulls = "SORTBY_NULLS_DEFAULT" | "SORTBY_NULLS_FIRST" | "SORTBY_NULLS_LAST";
Expand Down Expand Up @@ -15384,7 +15409,12 @@ export interface ScanToken {
"file": "types.ts",
},
{
"code": "export type EnumType = "OverridingKind" | "QuerySource" | "SortByDir" | "SortByNulls" | "SetQuantifier" | "A_Expr_Kind" | "RoleSpecType" | "TableLikeOption" | "DefElemAction" | "PartitionStrategy" | "PartitionRangeDatumKind" | "RTEKind" | "WCOKind" | "GroupingSetKind" | "CTEMaterialize" | "SetOperation" | "ObjectType" | "DropBehavior" | "AlterTableType" | "GrantTargetType" | "VariableSetKind" | "ConstrType" | "ImportForeignSchemaType" | "RoleStmtType" | "FetchDirection" | "FunctionParameterMode" | "TransactionStmtKind" | "ViewCheckOption" | "DiscardMode" | "ReindexObjectType" | "AlterTSConfigType" | "PublicationObjSpecType" | "AlterPublicationAction" | "AlterSubscriptionType" | "OnCommitAction" | "ParamKind" | "CoercionContext" | "CoercionForm" | "BoolExprType" | "SubLinkType" | "RowCompareType" | "MinMaxOp" | "SQLValueFunctionOp" | "XmlExprOp" | "XmlOptionType" | "JsonEncoding" | "JsonFormatType" | "JsonConstructorType" | "JsonValueType" | "NullTestType" | "BoolTestType" | "CmdType" | "JoinType" | "AggStrategy" | "AggSplit" | "SetOpCmd" | "SetOpStrategy" | "OnConflictAction" | "LimitOption" | "LockClauseStrength" | "LockWaitPolicy" | "LockTupleMode" | "KeywordKind" | "Token";
"code": "/**
* This file was automatically generated by [email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
export type EnumType = "OverridingKind" | "QuerySource" | "SortByDir" | "SortByNulls" | "SetQuantifier" | "A_Expr_Kind" | "RoleSpecType" | "TableLikeOption" | "DefElemAction" | "PartitionStrategy" | "PartitionRangeDatumKind" | "RTEKind" | "WCOKind" | "GroupingSetKind" | "CTEMaterialize" | "SetOperation" | "ObjectType" | "DropBehavior" | "AlterTableType" | "GrantTargetType" | "VariableSetKind" | "ConstrType" | "ImportForeignSchemaType" | "RoleStmtType" | "FetchDirection" | "FunctionParameterMode" | "TransactionStmtKind" | "ViewCheckOption" | "DiscardMode" | "ReindexObjectType" | "AlterTSConfigType" | "PublicationObjSpecType" | "AlterPublicationAction" | "AlterSubscriptionType" | "OnCommitAction" | "ParamKind" | "CoercionContext" | "CoercionForm" | "BoolExprType" | "SubLinkType" | "RowCompareType" | "MinMaxOp" | "SQLValueFunctionOp" | "XmlExprOp" | "XmlOptionType" | "JsonEncoding" | "JsonFormatType" | "JsonConstructorType" | "JsonValueType" | "NullTestType" | "BoolTestType" | "CmdType" | "JoinType" | "AggStrategy" | "AggSplit" | "SetOpCmd" | "SetOpStrategy" | "OnConflictAction" | "LimitOption" | "LockClauseStrength" | "LockWaitPolicy" | "LockTupleMode" | "KeywordKind" | "Token";
export const getEnumValue = (enumType: EnumType, key: string | number) => {
switch (enumType) {
case "OverridingKind":
Expand Down
24 changes: 9 additions & 15 deletions packages/parser/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@ import { Service, Type, Field, Enum, Namespace, ReflectionObject } from '@launch
import { generateTSEnums, generateTSInterfaces, generateTSEnumFunction, generateTSEnumsTypeUnionAST } from './ast';
import { generateEnum2IntJSON, generateEnum2StrJSON } from './json';
import { sync as mkdirp } from 'mkdirp';
import { writeFileSync } from 'fs';
import { defaultPgProtoParserOptions, PgProtoStoreOptions } from './types';
import { getUndefinedKey, hasUndefinedInitialValue } from './utils';

const cloneAndNameNode = (node: ReflectionObject, name: string) => {
const clone = JSON.parse(JSON.stringify(node));
return {
name,
...clone
}
}
import { cloneAndNameNode, getUndefinedKey, hasUndefinedInitialValue, writeFileToDisk } from './utils';

interface IProtoStore {
options: PgProtoStoreOptions;
Expand Down Expand Up @@ -108,8 +99,8 @@ export class ProtoStore implements IProtoStore {
const enums2str = generateEnum2StrJSON(this.enums);

// Write the files
writeFileSync(`${this.options.outDir}/enums2int.json`, JSON.stringify(enums2int, null, 2));
writeFileSync(`${this.options.outDir}/enums2str.json`, JSON.stringify(enums2str, null, 2));
this.writeFile(`${this.options.outDir}/enums2int.json`, JSON.stringify(enums2int, null, 2));
this.writeFile(`${this.options.outDir}/enums2str.json`, JSON.stringify(enums2str, null, 2));
}

if (this.options.includeTypes) {
Expand All @@ -123,22 +114,25 @@ export class ProtoStore implements IProtoStore {
}

// Write the files
writeFileSync(`${this.options.outDir}/types.ts`, `${enumsTS}\n${typesTS}`);
this.writeFile(`${this.options.outDir}/types.ts`, `${enumsTS}\n${typesTS}`);
}

if (this.options.includeEnums) {
const enumsTS = generateTSEnums(this.enums);
// Write the files
writeFileSync(`${this.options.outDir}/enums.ts`, enumsTS);
this.writeFile(`${this.options.outDir}/enums.ts`, enumsTS);
}

if (this.options.includeUtils) {
const utilsTS = generateTSEnumFunction(this.enums);

// Write the files
writeFileSync(`${this.options.outDir}/utils.ts`, utilsTS);
this.writeFile(`${this.options.outDir}/utils.ts`, utilsTS);
}
}
writeFile (filename: string, content: string) {
writeFileToDisk(filename, content, this.options);
}

}

2 changes: 2 additions & 0 deletions packages/parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PgProtoStoreOptions {
includeEnums?: boolean;
optionalFields?: boolean;
removeUndefinedAt0?: boolean;
includeHeader?: boolean;
outDir?: string;
}

Expand All @@ -26,6 +27,7 @@ export const defaultPgProtoParserOptions: PgProtoParserOptions = {
includeUtils: true,
includeEnumTypeUnion: true,
includeEnums: true,
includeHeader: true,
optionalFields: true,
removeUndefinedAt0: true,
outDir: `${process.cwd()}/out`,
Expand Down
26 changes: 25 additions & 1 deletion packages/parser/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Enum, Field } from '@launchql/protobufjs';
import { Enum, Field, ReflectionObject } from '@launchql/protobufjs';
import pkg from '../package.json';
import { PgProtoParserOptions } from './types';
import { writeFileSync } from 'fs';
import { extname } from 'path';

export const getUndefinedKey = (enumName) => {
// Split the name into parts where a lowercase letter is followed by an uppercase letter
Expand Down Expand Up @@ -27,6 +31,26 @@ export const hasUndefinedInitialValue = (enumData: Enum) => {
return firstEntry[0] === undefinedKey && firstEntry[1] === 0;
}

export const cloneAndNameNode = (node: ReflectionObject, name: string) => {
const clone = JSON.parse(JSON.stringify(node));
return {
name,
...clone
}
}

export const getFieldName = (field: Field, fallbackName: string) => {
return field.options?.json_name ? field.options.json_name : fallbackName;
}

export const getHeader = () => (`/**
* This file was automatically generated by pg-proto-parser@${pkg.version}.
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
* and run the pg-proto-parser generate command to regenerate this file.
*/
`);

export const writeFileToDisk = (path: string, contents: string, options: PgProtoParserOptions) => {
const c = (options.includeHeader && extname(path) === '.ts') ? `${getHeader()}${contents}` : contents;
writeFileSync(path, c);
}
1 change: 1 addition & 0 deletions packages/parser/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface PgProtoStoreOptions {
includeEnums?: boolean;
optionalFields?: boolean;
removeUndefinedAt0?: boolean;
includeHeader?: boolean;
outDir?: string;
}
export interface ParseProtoOptions {
Expand Down

0 comments on commit 31c70f7

Please sign in to comment.