Skip to content

Commit

Permalink
feat: add meta data for enum mapping to help code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenRover committed Jan 28, 2024
1 parent 64378e3 commit f2b9c09
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/google-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ export class GoogleTypes {
'HALF',
'YEAR',
],
'x-enum-mapping': {
CALENDAR_PERIOD_UNSPECIFIED: 0,
DAY: 1,
WEEK: 2,
FORTNIGHT: 3,
MONTH: 4,
QUARTER: 5,
HALF: 6,
YEAR: 7,
},
description:
'A `CalendarPeriod` represents the abstract concept of a time period that has a canonical start. Grammatically, "the start of the current `CalendarPeriod`." All calendar times begin at midnight UTC.',
},
Expand Down Expand Up @@ -498,6 +508,21 @@ export class GoogleTypes {
'NOVEMBER',
'DECEMBER',
],
'x-enum-mapping': {
MONTH_UNSPECIFIED: 0,
JANUARY: 1,
FEBRUARY: 2,
MARCH: 3,
APRIL: 4,
MAY: 5,
JUNE: 6,
JULY: 7,
AUGUST: 8,
SEPTEMBER: 9,
OCTOBER: 10,
NOVEMBER: 11,
DECEMBER: 12,
},
description: 'Represents a month in the Gregorian calendar.',
},
'google.type.PostalAddress': {
Expand Down
6 changes: 6 additions & 0 deletions src/protoj2jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,16 @@ class Proto2JsonSchema {
private compileEnum(
enumType: InternalEnum
): SpecTypesV2.AsyncAPISchemaDefinition {
const enumMapping: {[key: string]: number} = {};
for (const enumKey of Object.keys(enumType.values)) {
enumMapping[enumKey] = enumType.values[enumKey].value;
}

return {
title: enumType.name,
type: 'string',
enum: Object.keys(enumType.values),
'x-enum-mapping': enumMapping
};
}

Expand Down
70 changes: 70 additions & 0 deletions test/documents/realworld.train_run.proto.result.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@
"AFDZ_MANUELL_ERGAENZT",
"AFDZ_OK"
],
"x-enum-mapping": {
"AFDZ_UNSPECIFIED": 0,
"AFDZ_KEINE_TRAKTION": 1,
"AFDZ_ANHAENGELAST_FEHLT": 2,
"AFDZ_MANUELL_ERGAENZT": 3,
"AFDZ_OK": 4
},

},
"anzahl_loks": {
Expand Down Expand Up @@ -621,6 +628,13 @@
"FS_NORMAL",
"FS_FAHRZEIT_VERTEILEN"
],
"x-enum-mapping": {
"FS_UNSPECIFIED": 0,
"FS_SCHNELL": 1,
"FS_HALTEN": 2,
"FS_NORMAL": 3,
"FS_FAHRZEIT_VERTEILEN": 4
},

}
},
Expand Down Expand Up @@ -680,6 +694,13 @@
"TST_D",
"TST_N"
],
"x-enum-mapping": {
"TST_UNSPECIFIED": 0,
"TST_R": 1,
"TST_A": 2,
"TST_D": 3,
"TST_N": 4
},

}
},
Expand Down Expand Up @@ -714,6 +735,13 @@
"AOSS_OBSERVE_EARLIEST_TIMES",
"AOSS_ONLY_WHEN_ROUTE_IS_SET"
],
"x-enum-mapping": {
"AOSS_UNSPECIFIED": 0,
"AOSS_ALLOWED": 1,
"AOSS_PROHIBITED": 2,
"AOSS_OBSERVE_EARLIEST_TIMES": 4,
"AOSS_ONLY_WHEN_ROUTE_IS_SET": 5
},

}
},
Expand Down Expand Up @@ -791,6 +819,13 @@
"AFDZ_MANUELL_ERGAENZT",
"AFDZ_OK"
],
"x-enum-mapping": {
"AFDZ_UNSPECIFIED": 0,
"AFDZ_KEINE_TRAKTION": 1,
"AFDZ_ANHAENGELAST_FEHLT": 2,
"AFDZ_MANUELL_ERGAENZT": 3,
"AFDZ_OK": 4
},

},
"anzahl_loks": {
Expand Down Expand Up @@ -1043,6 +1078,13 @@
"AFDZ_MANUELL_ERGAENZT",
"AFDZ_OK"
],
"x-enum-mapping": {
"AFDZ_UNSPECIFIED": 0,
"AFDZ_KEINE_TRAKTION": 1,
"AFDZ_ANHAENGELAST_FEHLT": 2,
"AFDZ_MANUELL_ERGAENZT": 3,
"AFDZ_OK": 4
},

},
"anzahl_loks": {
Expand Down Expand Up @@ -1448,6 +1490,13 @@
"FS_NORMAL",
"FS_FAHRZEIT_VERTEILEN"
],
"x-enum-mapping": {
"FS_UNSPECIFIED": 0,
"FS_SCHNELL": 1,
"FS_HALTEN": 2,
"FS_NORMAL": 3,
"FS_FAHRZEIT_VERTEILEN": 4
},

}
},
Expand Down Expand Up @@ -1507,6 +1556,13 @@
"TST_D",
"TST_N"
],
"x-enum-mapping": {
"TST_UNSPECIFIED": 0,
"TST_R": 1,
"TST_A": 2,
"TST_D": 3,
"TST_N": 4
},

}
},
Expand Down Expand Up @@ -1541,6 +1597,13 @@
"AOSS_OBSERVE_EARLIEST_TIMES",
"AOSS_ONLY_WHEN_ROUTE_IS_SET"
],
"x-enum-mapping": {
"AOSS_UNSPECIFIED": 0,
"AOSS_ALLOWED": 1,
"AOSS_PROHIBITED": 2,
"AOSS_OBSERVE_EARLIEST_TIMES": 4,
"AOSS_ONLY_WHEN_ROUTE_IS_SET": 5
},

}
},
Expand Down Expand Up @@ -1618,6 +1681,13 @@
"AFDZ_MANUELL_ERGAENZT",
"AFDZ_OK"
],
"x-enum-mapping": {
"AFDZ_UNSPECIFIED": 0,
"AFDZ_KEINE_TRAKTION": 1,
"AFDZ_ANHAENGELAST_FEHLT": 2,
"AFDZ_MANUELL_ERGAENZT": 3,
"AFDZ_OK": 4
},

},
"anzahl_loks": {
Expand Down

0 comments on commit f2b9c09

Please sign in to comment.