From d967a9afd6cf63fc7b156d506b8683b2f8fd6569 Mon Sep 17 00:00:00 2001 From: Ruben Maher Date: Sat, 3 Apr 2021 03:17:05 +0900 Subject: [PATCH] feat: Add support for useDate=string (#221) * Add support for useDate=string Co-authored-by: @actuosus Co-authored-by: @jessebutterfield * fix: remove redundant check for undefined * fix: run codegen again for changes in master * fix: update-bins and run codegen again for changes in master This is libprotoc 3.13.0, I guess it's the expected version otherwise there would be more changes than this. Co-authored-by: @actuosus Co-authored-by: @jessebutterfield --- README.markdown | 11 +- .../simple-optionals/import_dir/thing.ts | 3 +- integration/simple-optionals/simple.ts | 3 +- integration/simple-optionals/thing.ts | 3 +- integration/simple-snake/import_dir/thing.ts | 3 +- integration/simple-snake/simple.ts | 3 +- .../import_dir/thing.ts | 3 +- .../simple-unrecognized-enum/simple.ts | 3 +- integration/simple/import_dir/thing.ts | 3 +- integration/simple/simple.ts | 8 +- integration/type-registry/foo.ts | 3 +- integration/use-date-false/metadata.ts | 3 +- .../google/protobuf/timestamp.ts | 212 ++++++++++++++ integration/use-date-string/parameters.txt | 1 + .../use-date-string/use-date-string-test.ts | 48 +++ .../use-date-string/use-date-string.bin | Bin 0 -> 6990 bytes .../use-date-string/use-date-string.proto | 10 + .../use-date-string/use-date-string.ts | 265 +++++++++++++++++ .../google/protobuf/timestamp.ts | 212 ++++++++++++++ integration/use-date-true/parameters.txt | 1 + .../use-date-true/use-date-true-test.ts | 48 +++ integration/use-date-true/use-date-true.bin | Bin 0 -> 6986 bytes integration/use-date-true/use-date-true.proto | 10 + integration/use-date-true/use-date-true.ts | 274 ++++++++++++++++++ src/main.ts | 135 ++++++--- src/options.ts | 20 +- src/types.ts | 12 +- tests/options-test.ts | 2 +- 28 files changed, 1218 insertions(+), 81 deletions(-) create mode 100644 integration/use-date-string/google/protobuf/timestamp.ts create mode 100644 integration/use-date-string/parameters.txt create mode 100644 integration/use-date-string/use-date-string-test.ts create mode 100644 integration/use-date-string/use-date-string.bin create mode 100644 integration/use-date-string/use-date-string.proto create mode 100644 integration/use-date-string/use-date-string.ts create mode 100644 integration/use-date-true/google/protobuf/timestamp.ts create mode 100644 integration/use-date-true/parameters.txt create mode 100644 integration/use-date-true/use-date-true-test.ts create mode 100644 integration/use-date-true/use-date-true.bin create mode 100644 integration/use-date-true/use-date-true.proto create mode 100644 integration/use-date-true/use-date-true.ts diff --git a/README.markdown b/README.markdown index 908c0ac86..5672d0a5c 100644 --- a/README.markdown +++ b/README.markdown @@ -25,6 +25,7 @@ - [Primitive Types](#primitive-types) - [Wrapper Types](#wrapper-types) - [Number Types](#number-types) +- [Timestamps](#timestamps) - [Current Status of Optional Values](#current-status-of-optional-values) # Overview @@ -292,7 +293,7 @@ protoc --plugin=node_modules/ts-proto/protoc-gen-ts_proto ./batching.proto -I. Note that `addGrpcMetadata`, `addNestjsRestParameter` and `returnObservable` will still be false. -- With `--ts_proto_opt=useDate=false`, fields of type `google.protobuf.Timestamp` will not be mapped to type `Date` in the generated types. +- With `--ts_proto_opt=useDate=false`, fields of type `google.protobuf.Timestamp` will not be mapped to type `Date` in the generated types. See [Timestamps](#timestamps) for more details. - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators. @@ -411,6 +412,14 @@ The protobuf number types map to JavaScript types based on the `forceLong` confi Where (\*) indicates they might throw an error at runtime. +# Timestamps + +The representation of `google.protobuf.Timestamp` is configurable by the `useDate` flag. + +| Protobuf well-known type | Default/`useDate=true` | `useDate=false` | `useDate=string` | +| --------------------------- | ---------------------- | ------------------------------------ | ---------------- | +| `google.protobuf.Timestamp` | `Date` | `{ seconds: number, nanos: number }` | `string` | + # Current Status of Optional Values - Required primitives: use as-is, i.e. `string name = 1`. diff --git a/integration/simple-optionals/import_dir/thing.ts b/integration/simple-optionals/import_dir/thing.ts index 0ce1ae0d3..07f00fa09 100644 --- a/integration/simple-optionals/import_dir/thing.ts +++ b/integration/simple-optionals/import_dir/thing.ts @@ -49,8 +49,7 @@ export const ImportedThing = { toJSON(message: ImportedThing): unknown { const obj: any = {}; - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); return obj; }, diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index 5a68e49c5..626277f6e 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -402,8 +402,7 @@ export const Simple = { const obj: any = {}; message.name !== undefined && (obj.name = message.name); message.age !== undefined && (obj.age = message.age); - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); message.state !== undefined && (obj.state = stateEnumToJSON(message.state)); if (message.grandChildren) { diff --git a/integration/simple-optionals/thing.ts b/integration/simple-optionals/thing.ts index 62c4ec4c5..6c8e27ef4 100644 --- a/integration/simple-optionals/thing.ts +++ b/integration/simple-optionals/thing.ts @@ -49,8 +49,7 @@ export const ImportedThing = { toJSON(message: ImportedThing): unknown { const obj: any = {}; - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); return obj; }, diff --git a/integration/simple-snake/import_dir/thing.ts b/integration/simple-snake/import_dir/thing.ts index 894613e3d..a1206a96d 100644 --- a/integration/simple-snake/import_dir/thing.ts +++ b/integration/simple-snake/import_dir/thing.ts @@ -49,8 +49,7 @@ export const ImportedThing = { toJSON(message: ImportedThing): unknown { const obj: any = {}; - message.created_at !== undefined && - (obj.created_at = message.created_at !== undefined ? message.created_at.toISOString() : null); + message.created_at !== undefined && (obj.created_at = message.created_at.toISOString()); return obj; }, diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index 160dbd7c4..4598cb0dd 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -402,8 +402,7 @@ export const Simple = { const obj: any = {}; message.name !== undefined && (obj.name = message.name); message.age !== undefined && (obj.age = message.age); - message.created_at !== undefined && - (obj.created_at = message.created_at !== undefined ? message.created_at.toISOString() : null); + message.created_at !== undefined && (obj.created_at = message.created_at.toISOString()); message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); message.state !== undefined && (obj.state = stateEnumToJSON(message.state)); if (message.grand_children) { diff --git a/integration/simple-unrecognized-enum/import_dir/thing.ts b/integration/simple-unrecognized-enum/import_dir/thing.ts index 874358a1e..1c0f0c21c 100644 --- a/integration/simple-unrecognized-enum/import_dir/thing.ts +++ b/integration/simple-unrecognized-enum/import_dir/thing.ts @@ -49,8 +49,7 @@ export const ImportedThing = { toJSON(message: ImportedThing): unknown { const obj: any = {}; - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); return obj; }, diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index 5602c8307..64083f42d 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -393,8 +393,7 @@ export const Simple = { const obj: any = {}; message.name !== undefined && (obj.name = message.name); message.age !== undefined && (obj.age = message.age); - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); message.state !== undefined && (obj.state = stateEnumToJSON(message.state)); if (message.grandChildren) { diff --git a/integration/simple/import_dir/thing.ts b/integration/simple/import_dir/thing.ts index 874358a1e..1c0f0c21c 100644 --- a/integration/simple/import_dir/thing.ts +++ b/integration/simple/import_dir/thing.ts @@ -49,8 +49,7 @@ export const ImportedThing = { toJSON(message: ImportedThing): unknown { const obj: any = {}; - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); return obj; }, diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index 9d1d2f5cf..389992000 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -480,8 +480,7 @@ export const Simple = { const obj: any = {}; message.name !== undefined && (obj.name = message.name); message.age !== undefined && (obj.age = message.age); - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); message.state !== undefined && (obj.state = stateEnumToJSON(message.state)); if (message.grandChildren) { @@ -1620,7 +1619,7 @@ export const SimpleWithMap_MapOfTimestampsEntry = { toJSON(message: SimpleWithMap_MapOfTimestampsEntry): unknown { const obj: any = {}; message.key !== undefined && (obj.key = message.key); - message.value !== undefined && (obj.value = message.value !== undefined ? message.value.toISOString() : null); + message.value !== undefined && (obj.value = message.value.toISOString()); return obj; }, @@ -2466,8 +2465,7 @@ export const SimpleButOptional = { const obj: any = {}; message.name !== undefined && (obj.name = message.name); message.age !== undefined && (obj.age = message.age); - message.createdAt !== undefined && - (obj.createdAt = message.createdAt !== undefined ? message.createdAt.toISOString() : null); + message.createdAt !== undefined && (obj.createdAt = message.createdAt.toISOString()); message.child !== undefined && (obj.child = message.child ? Child.toJSON(message.child) : undefined); message.state !== undefined && (obj.state = message.state !== undefined ? stateEnumToJSON(message.state) : undefined); diff --git a/integration/type-registry/foo.ts b/integration/type-registry/foo.ts index 2ae6639ab..ce6b3e250 100644 --- a/integration/type-registry/foo.ts +++ b/integration/type-registry/foo.ts @@ -53,8 +53,7 @@ export const Foo = { toJSON(message: Foo): unknown { const obj: any = {}; - message.timestamp !== undefined && - (obj.timestamp = message.timestamp !== undefined ? message.timestamp.toISOString() : null); + message.timestamp !== undefined && (obj.timestamp = message.timestamp.toISOString()); return obj; }, diff --git a/integration/use-date-false/metadata.ts b/integration/use-date-false/metadata.ts index b91a322ba..18d137010 100644 --- a/integration/use-date-false/metadata.ts +++ b/integration/use-date-false/metadata.ts @@ -49,8 +49,7 @@ export const Metadata = { toJSON(message: Metadata): unknown { const obj: any = {}; - message.lastEdited !== undefined && - (obj.lastEdited = message.lastEdited !== undefined ? fromTimestamp(message.lastEdited).toISOString() : null); + message.lastEdited !== undefined && (obj.lastEdited = fromTimestamp(message.lastEdited).toISOString()); return obj; }, diff --git a/integration/use-date-string/google/protobuf/timestamp.ts b/integration/use-date-string/google/protobuf/timestamp.ts new file mode 100644 index 000000000..11a824a90 --- /dev/null +++ b/integration/use-date-string/google/protobuf/timestamp.ts @@ -0,0 +1,212 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; + +export const protobufPackage = 'google.protobuf'; + +/** + * A Timestamp represents a point in time independent of any time zone or local + * calendar, encoded as a count of seconds and fractions of seconds at + * nanosecond resolution. The count is relative to an epoch at UTC midnight on + * January 1, 1970, in the proleptic Gregorian calendar which extends the + * Gregorian calendar backwards to year one. + * + * All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap + * second table is needed for interpretation, using a [24-hour linear + * smear](https://developers.google.com/time/smear). + * + * The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By + * restricting to that range, we ensure that we can convert to and from [RFC + * 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. + * + * # Examples + * + * Example 1: Compute Timestamp from POSIX `time()`. + * + * Timestamp timestamp; + * timestamp.set_seconds(time(NULL)); + * timestamp.set_nanos(0); + * + * Example 2: Compute Timestamp from POSIX `gettimeofday()`. + * + * struct timeval tv; + * gettimeofday(&tv, NULL); + * + * Timestamp timestamp; + * timestamp.set_seconds(tv.tv_sec); + * timestamp.set_nanos(tv.tv_usec * 1000); + * + * Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. + * + * FILETIME ft; + * GetSystemTimeAsFileTime(&ft); + * UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + * + * // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z + * // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. + * Timestamp timestamp; + * timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); + * timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); + * + * Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. + * + * long millis = System.currentTimeMillis(); + * + * Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) + * .setNanos((int) ((millis % 1000) * 1000000)).build(); + * + * + * Example 5: Compute Timestamp from current time in Python. + * + * timestamp = Timestamp() + * timestamp.GetCurrentTime() + * + * # JSON Mapping + * + * In JSON format, the Timestamp type is encoded as a string in the + * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the + * format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" + * where {year} is always expressed using four digits while {month}, {day}, + * {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional + * seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), + * are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone + * is required. A proto3 JSON serializer should always use UTC (as indicated by + * "Z") when printing the Timestamp type and a proto3 JSON parser should be + * able to accept both UTC and other timezones (as indicated by an offset). + * + * For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past + * 01:30 UTC on January 15, 2017. + * + * In JavaScript, one can convert a Date object to this format using the + * standard + * [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) + * method. In Python, a standard `datetime.datetime` object can be converted + * to this format using + * [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with + * the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use + * the Joda Time's [`ISODateTimeFormat.dateTime()`]( + * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D + * ) to obtain a formatter capable of generating timestamps in this format. + */ +export interface Timestamp { + /** + * Represents seconds of UTC time since Unix epoch + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59Z inclusive. + */ + seconds: number; + /** + * Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values + * that count forward in time. Must be from 0 to 999,999,999 + * inclusive. + */ + nanos: number; +} + +const baseTimestamp: object = { seconds: 0, nanos: 0 }; + +export const Timestamp = { + encode(message: Timestamp, writer: Writer = Writer.create()): Writer { + if (message.seconds !== 0) { + writer.uint32(8).int64(message.seconds); + } + if (message.nanos !== 0) { + writer.uint32(16).int32(message.nanos); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Timestamp { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseTimestamp } as Timestamp; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.seconds = longToNumber(reader.int64() as Long); + break; + case 2: + message.nanos = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Timestamp { + const message = { ...baseTimestamp } as Timestamp; + if (object.seconds !== undefined && object.seconds !== null) { + message.seconds = Number(object.seconds); + } else { + message.seconds = 0; + } + if (object.nanos !== undefined && object.nanos !== null) { + message.nanos = Number(object.nanos); + } else { + message.nanos = 0; + } + return message; + }, + + toJSON(message: Timestamp): unknown { + const obj: any = {}; + message.seconds !== undefined && (obj.seconds = message.seconds); + message.nanos !== undefined && (obj.nanos = message.nanos); + return obj; + }, + + fromPartial(object: DeepPartial): Timestamp { + const message = { ...baseTimestamp } as Timestamp; + if (object.seconds !== undefined && object.seconds !== null) { + message.seconds = object.seconds; + } else { + message.seconds = 0; + } + if (object.nanos !== undefined && object.nanos !== null) { + message.nanos = object.nanos; + } else { + message.nanos = 0; + } + return message; + }, +}; + +declare var self: any | undefined; +declare var window: any | undefined; +var globalThis: any = (() => { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof self !== 'undefined') return self; + if (typeof window !== 'undefined') return window; + if (typeof global !== 'undefined') return global; + throw 'Unable to locate global object'; +})(); + +type Builtin = Date | Function | Uint8Array | string | number | undefined; +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +function longToNumber(long: Long): number { + if (long.gt(Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); + } + return long.toNumber(); +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} diff --git a/integration/use-date-string/parameters.txt b/integration/use-date-string/parameters.txt new file mode 100644 index 000000000..9259e124f --- /dev/null +++ b/integration/use-date-string/parameters.txt @@ -0,0 +1 @@ +useDate=string diff --git a/integration/use-date-string/use-date-string-test.ts b/integration/use-date-string/use-date-string-test.ts new file mode 100644 index 000000000..905c37d0d --- /dev/null +++ b/integration/use-date-string/use-date-string-test.ts @@ -0,0 +1,48 @@ +import { Todo } from './use-date-string'; + +const jan1 = new Date('1970-01-01T00:00:00.000Z'); +const feb1 = new Date('1970-02-01T00:00:00.000Z'); + +describe('useDate=string', () => { + it('generates types that compile and encode', () => { + const output = Todo.encode({ + id: '6883ed6e-bd0d-4817-ba58-c2a53c73edc2', + timestamp: feb1.toISOString(), + repeatedTimestamp: [jan1.toISOString(), feb1.toISOString()], + mapOfTimestamps: { + jan1: jan1.toISOString(), + feb1: feb1.toISOString(), + }, + }).finish(); + + expect(Todo.decode(output)).toMatchInlineSnapshot(` + Object { + "id": "6883ed6e-bd0d-4817-ba58-c2a53c73edc2", + "mapOfTimestamps": Object { + "feb1": "1970-02-01T00:00:00.000Z", + "jan1": "1970-01-01T00:00:00.000Z", + }, + "repeatedTimestamp": Array [ + "1970-01-01T00:00:00.000Z", + "1970-02-01T00:00:00.000Z", + ], + "timestamp": "1970-02-01T00:00:00.000Z", + } + `); + + expect(Todo.toJSON(Todo.decode(output))).toMatchInlineSnapshot(` + Object { + "id": "6883ed6e-bd0d-4817-ba58-c2a53c73edc2", + "mapOfTimestamps": Object { + "feb1": "1970-02-01T00:00:00.000Z", + "jan1": "1970-01-01T00:00:00.000Z", + }, + "repeatedTimestamp": Array [ + "1970-01-01T00:00:00.000Z", + "1970-02-01T00:00:00.000Z", + ], + "timestamp": "1970-02-01T00:00:00.000Z", + } + `); + }); +}); diff --git a/integration/use-date-string/use-date-string.bin b/integration/use-date-string/use-date-string.bin new file mode 100644 index 0000000000000000000000000000000000000000..fade300b85ff59eff53f787afdb2518ba713c0d3 GIT binary patch literal 6990 zcmbtZOLN=S73M>-W!Q;rCYkX#o#a%BVlpK`O7bI?({?BdlHgLLN`O{mPileWrGyv+ zSOSzRyUwDkbkl#(O_%u>-FMecXS#2GK{s9YJNE)4{qBQ9N%wW$=bm%!k@mqj6s4XM ziBcGiT>nrXj)Ew-y0DPF^v;#kV(Q6X%G$?=L2&4avfS#7`{l?Th%j;nLv=cLQ5{e- z99X=gowJiOxhvY)P;>*o7hXwII-hMN)7<;oyzlrycqN1N`Iefjf2+OM4F>uPIM?5u zVBBREchmQOa_B}!p{KF}W!{~G z6Q1`AKREGihPnIql6E#X|5569>9qFe3tIX@F8fg`xALb8Bq6$iN9*H$UyMR3k-~K? zBrt}JaWJ(V|6;gu>i(cV;cEE_8D+&w=xepvhM=0b(<{NSJ_(y)jfZ;X|7;@_+H6u^kv|FX)a3 z!jGhi*(7D?Kfun2;3;C{I$k(cwKNbmHJY{UM5u;<0RhQ~eP`A@5 zQOFYc%1r`z@4z{wj$luL5(5%`534{lVkQPb1P_opiQwJf4$et^Y-qp>YlJ~RI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?Ks0B zVlfs>U#+O8EWg5L!yvz~*(sk(=PIDanN*Wu`M-+FiWFybBsv%GRGDGbDid3L%GvZ; z>FEG3nIrr~jC*E3<&^ep?I|)Ve&c35d!Rq%B7)=4v#;+jYRKCHw?siRBF8&%PQmQ)iMoT=fZj1(kE$~W^9F$zjUrzaoFNM zlos!!YCxL+p#g!2ANMFb*oy22vOdc%qAXf0+<9&lFIFz2em!k?``>L9~|jRG40k3sa9RHA>`l~|FeEoVp#9lS>; zWI8CdWTnG-zuQHHMjgDKB~6A^FX=?G@FhOD7!3M-+)G)+Y@$jMD&rSvaiy|+i(}^U z4ZE_uwpv+RzM)r^p+4o^2)#T{%tb>dL^Uc;O8RKJOsdHnMRaW(^6;BD$)nR z69hd+);z*5wXHJR4qxrIihQH7c+ogN|&QIkB85_u*SZ zc|q`KIuyPbIqIqbQf7L-ma{Gf59LpGE`Ql4 z_QjEkpLl%BSs22j0v)qy>7mJo)JwGo?QowDuJER}g7&J%!hNvZ^>7uA`StXUHqY$M zOa9I#(=<;PE+*3~Pjl~SKPX|a`yC$LSO1o#M&OrxaR{y8ahL>Q*&BmDt4Afjt~hDG z{KRa;fJw+E)X-x@Q`x4QB9k4>Fv1n2BL`&sS(*}c49KnpYUaVo-Uv)5dNy|PGr?D1!n5$^>(zX5r6{>@4nJ?1o zA87AAOaz#+diNh0?M!Yil{%N^o)7+$kxJ#b=aWt4vgs6xKH2h_%kqIF5@Z)HXy@ZV zke$n1(k>*|4zq=1hLACL?tC)KV)pLE7z|tUna|^`^ckLgn9O+ZM@z{JVDl(F6*Y9m z&Sbufw=#1)(~=pGoH_q#G6Rw`OSh64kepe&6C=Uo>;y>`NX|}>WP#-D1W6W1&Q6eI j8E%3EU<;X_$N1*}wvfpuGk`4=){+^(7JmA( map_of_timestamps = 5; +} diff --git a/integration/use-date-string/use-date-string.ts b/integration/use-date-string/use-date-string.ts new file mode 100644 index 000000000..bafe66933 --- /dev/null +++ b/integration/use-date-string/use-date-string.ts @@ -0,0 +1,265 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; +import { Timestamp } from './google/protobuf/timestamp'; + +export const protobufPackage = ''; + +export interface Todo { + id: string; + timestamp: string | undefined; + repeatedTimestamp: string[]; + optionalTimestamp?: string | undefined; + mapOfTimestamps: { [key: string]: string }; +} + +export interface Todo_MapOfTimestampsEntry { + key: string; + value: string | undefined; +} + +const baseTodo: object = { id: '' }; + +export const Todo = { + encode(message: Todo, writer: Writer = Writer.create()): Writer { + if (message.id !== '') { + writer.uint32(10).string(message.id); + } + if (message.timestamp !== undefined) { + Timestamp.encode(toTimestamp(message.timestamp), writer.uint32(18).fork()).ldelim(); + } + for (const v of message.repeatedTimestamp) { + Timestamp.encode(toTimestamp(v!), writer.uint32(26).fork()).ldelim(); + } + if (message.optionalTimestamp !== undefined) { + Timestamp.encode(toTimestamp(message.optionalTimestamp), writer.uint32(34).fork()).ldelim(); + } + Object.entries(message.mapOfTimestamps).forEach(([key, value]) => { + Todo_MapOfTimestampsEntry.encode({ key: key as any, value }, writer.uint32(42).fork()).ldelim(); + }); + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Todo { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseTodo } as Todo; + message.repeatedTimestamp = []; + message.mapOfTimestamps = {}; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.id = reader.string(); + break; + case 2: + message.timestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + case 3: + message.repeatedTimestamp.push(fromTimestamp(Timestamp.decode(reader, reader.uint32()))); + break; + case 4: + message.optionalTimestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + case 5: + const entry5 = Todo_MapOfTimestampsEntry.decode(reader, reader.uint32()); + if (entry5.value !== undefined) { + message.mapOfTimestamps[entry5.key] = entry5.value; + } + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Todo { + const message = { ...baseTodo } as Todo; + message.repeatedTimestamp = []; + message.mapOfTimestamps = {}; + if (object.id !== undefined && object.id !== null) { + message.id = String(object.id); + } else { + message.id = ''; + } + if (object.timestamp !== undefined && object.timestamp !== null) { + message.timestamp = String(object.timestamp); + } else { + message.timestamp = undefined; + } + if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { + for (const e of object.repeatedTimestamp) { + message.repeatedTimestamp.push(String(e)); + } + } + if (object.optionalTimestamp !== undefined && object.optionalTimestamp !== null) { + message.optionalTimestamp = String(object.optionalTimestamp); + } else { + message.optionalTimestamp = undefined; + } + if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { + Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + message.mapOfTimestamps[key] = String(value); + }); + } + return message; + }, + + toJSON(message: Todo): unknown { + const obj: any = {}; + message.id !== undefined && (obj.id = message.id); + message.timestamp !== undefined && (obj.timestamp = message.timestamp); + if (message.repeatedTimestamp) { + obj.repeatedTimestamp = message.repeatedTimestamp.map((e) => e); + } else { + obj.repeatedTimestamp = []; + } + message.optionalTimestamp !== undefined && (obj.optionalTimestamp = message.optionalTimestamp); + obj.mapOfTimestamps = {}; + if (message.mapOfTimestamps) { + Object.entries(message.mapOfTimestamps).forEach(([k, v]) => { + obj.mapOfTimestamps[k] = v; + }); + } + return obj; + }, + + fromPartial(object: DeepPartial): Todo { + const message = { ...baseTodo } as Todo; + message.repeatedTimestamp = []; + message.mapOfTimestamps = {}; + if (object.id !== undefined && object.id !== null) { + message.id = object.id; + } else { + message.id = ''; + } + if (object.timestamp !== undefined && object.timestamp !== null) { + message.timestamp = object.timestamp; + } else { + message.timestamp = undefined; + } + if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { + for (const e of object.repeatedTimestamp) { + message.repeatedTimestamp.push(e); + } + } + if (object.optionalTimestamp !== undefined && object.optionalTimestamp !== null) { + message.optionalTimestamp = object.optionalTimestamp; + } else { + message.optionalTimestamp = undefined; + } + if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { + Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + if (value !== undefined) { + message.mapOfTimestamps[key] = value; + } + }); + } + return message; + }, +}; + +const baseTodo_MapOfTimestampsEntry: object = { key: '' }; + +export const Todo_MapOfTimestampsEntry = { + encode(message: Todo_MapOfTimestampsEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== '') { + writer.uint32(10).string(message.key); + } + if (message.value !== undefined) { + Timestamp.encode(toTimestamp(message.value), writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Todo_MapOfTimestampsEntry { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Todo_MapOfTimestampsEntry { + const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; + if (object.key !== undefined && object.key !== null) { + message.key = String(object.key); + } else { + message.key = ''; + } + if (object.value !== undefined && object.value !== null) { + message.value = String(object.value); + } else { + message.value = undefined; + } + return message; + }, + + toJSON(message: Todo_MapOfTimestampsEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = message.key); + message.value !== undefined && (obj.value = message.value); + return obj; + }, + + fromPartial(object: DeepPartial): Todo_MapOfTimestampsEntry { + const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; + if (object.key !== undefined && object.key !== null) { + message.key = object.key; + } else { + message.key = ''; + } + if (object.value !== undefined && object.value !== null) { + message.value = object.value; + } else { + message.value = undefined; + } + return message; + }, +}; + +type Builtin = Date | Function | Uint8Array | string | number | undefined; +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +function toTimestamp(dateStr: string): Timestamp { + const date = new Date(dateStr); + const seconds = date.getTime() / 1_000; + const nanos = (date.getTime() % 1_000) * 1_000_000; + return { seconds, nanos }; +} + +function fromTimestamp(t: Timestamp): string { + let millis = t.seconds * 1_000; + millis += t.nanos / 1_000_000; + return new Date(millis).toISOString(); +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} diff --git a/integration/use-date-true/google/protobuf/timestamp.ts b/integration/use-date-true/google/protobuf/timestamp.ts new file mode 100644 index 000000000..11a824a90 --- /dev/null +++ b/integration/use-date-true/google/protobuf/timestamp.ts @@ -0,0 +1,212 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; + +export const protobufPackage = 'google.protobuf'; + +/** + * A Timestamp represents a point in time independent of any time zone or local + * calendar, encoded as a count of seconds and fractions of seconds at + * nanosecond resolution. The count is relative to an epoch at UTC midnight on + * January 1, 1970, in the proleptic Gregorian calendar which extends the + * Gregorian calendar backwards to year one. + * + * All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap + * second table is needed for interpretation, using a [24-hour linear + * smear](https://developers.google.com/time/smear). + * + * The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By + * restricting to that range, we ensure that we can convert to and from [RFC + * 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. + * + * # Examples + * + * Example 1: Compute Timestamp from POSIX `time()`. + * + * Timestamp timestamp; + * timestamp.set_seconds(time(NULL)); + * timestamp.set_nanos(0); + * + * Example 2: Compute Timestamp from POSIX `gettimeofday()`. + * + * struct timeval tv; + * gettimeofday(&tv, NULL); + * + * Timestamp timestamp; + * timestamp.set_seconds(tv.tv_sec); + * timestamp.set_nanos(tv.tv_usec * 1000); + * + * Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. + * + * FILETIME ft; + * GetSystemTimeAsFileTime(&ft); + * UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + * + * // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z + * // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. + * Timestamp timestamp; + * timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); + * timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); + * + * Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. + * + * long millis = System.currentTimeMillis(); + * + * Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) + * .setNanos((int) ((millis % 1000) * 1000000)).build(); + * + * + * Example 5: Compute Timestamp from current time in Python. + * + * timestamp = Timestamp() + * timestamp.GetCurrentTime() + * + * # JSON Mapping + * + * In JSON format, the Timestamp type is encoded as a string in the + * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the + * format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" + * where {year} is always expressed using four digits while {month}, {day}, + * {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional + * seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), + * are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone + * is required. A proto3 JSON serializer should always use UTC (as indicated by + * "Z") when printing the Timestamp type and a proto3 JSON parser should be + * able to accept both UTC and other timezones (as indicated by an offset). + * + * For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past + * 01:30 UTC on January 15, 2017. + * + * In JavaScript, one can convert a Date object to this format using the + * standard + * [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) + * method. In Python, a standard `datetime.datetime` object can be converted + * to this format using + * [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with + * the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use + * the Joda Time's [`ISODateTimeFormat.dateTime()`]( + * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D + * ) to obtain a formatter capable of generating timestamps in this format. + */ +export interface Timestamp { + /** + * Represents seconds of UTC time since Unix epoch + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59Z inclusive. + */ + seconds: number; + /** + * Non-negative fractions of a second at nanosecond resolution. Negative + * second values with fractions must still have non-negative nanos values + * that count forward in time. Must be from 0 to 999,999,999 + * inclusive. + */ + nanos: number; +} + +const baseTimestamp: object = { seconds: 0, nanos: 0 }; + +export const Timestamp = { + encode(message: Timestamp, writer: Writer = Writer.create()): Writer { + if (message.seconds !== 0) { + writer.uint32(8).int64(message.seconds); + } + if (message.nanos !== 0) { + writer.uint32(16).int32(message.nanos); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Timestamp { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseTimestamp } as Timestamp; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.seconds = longToNumber(reader.int64() as Long); + break; + case 2: + message.nanos = reader.int32(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Timestamp { + const message = { ...baseTimestamp } as Timestamp; + if (object.seconds !== undefined && object.seconds !== null) { + message.seconds = Number(object.seconds); + } else { + message.seconds = 0; + } + if (object.nanos !== undefined && object.nanos !== null) { + message.nanos = Number(object.nanos); + } else { + message.nanos = 0; + } + return message; + }, + + toJSON(message: Timestamp): unknown { + const obj: any = {}; + message.seconds !== undefined && (obj.seconds = message.seconds); + message.nanos !== undefined && (obj.nanos = message.nanos); + return obj; + }, + + fromPartial(object: DeepPartial): Timestamp { + const message = { ...baseTimestamp } as Timestamp; + if (object.seconds !== undefined && object.seconds !== null) { + message.seconds = object.seconds; + } else { + message.seconds = 0; + } + if (object.nanos !== undefined && object.nanos !== null) { + message.nanos = object.nanos; + } else { + message.nanos = 0; + } + return message; + }, +}; + +declare var self: any | undefined; +declare var window: any | undefined; +var globalThis: any = (() => { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof self !== 'undefined') return self; + if (typeof window !== 'undefined') return window; + if (typeof global !== 'undefined') return global; + throw 'Unable to locate global object'; +})(); + +type Builtin = Date | Function | Uint8Array | string | number | undefined; +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +function longToNumber(long: Long): number { + if (long.gt(Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); + } + return long.toNumber(); +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} diff --git a/integration/use-date-true/parameters.txt b/integration/use-date-true/parameters.txt new file mode 100644 index 000000000..c00c195f2 --- /dev/null +++ b/integration/use-date-true/parameters.txt @@ -0,0 +1 @@ +useDate=true diff --git a/integration/use-date-true/use-date-true-test.ts b/integration/use-date-true/use-date-true-test.ts new file mode 100644 index 000000000..5d51fc157 --- /dev/null +++ b/integration/use-date-true/use-date-true-test.ts @@ -0,0 +1,48 @@ +import { Todo } from './use-date-true'; + +const jan1 = new Date('1970-01-01T00:00:00.000Z'); +const feb1 = new Date('1970-02-01T00:00:00.000Z'); + +describe('useDate=true', () => { + it('generates types that compile and encode', () => { + const output = Todo.encode({ + id: '6883ed6e-bd0d-4817-ba58-c2a53c73edc2', + timestamp: feb1, + repeatedTimestamp: [jan1, feb1], + mapOfTimestamps: { + jan1, + feb1, + }, + }).finish(); + + expect(Todo.decode(output)).toMatchInlineSnapshot(` + Object { + "id": "6883ed6e-bd0d-4817-ba58-c2a53c73edc2", + "mapOfTimestamps": Object { + "feb1": 1970-02-01T00:00:00.000Z, + "jan1": 1970-01-01T00:00:00.000Z, + }, + "repeatedTimestamp": Array [ + 1970-01-01T00:00:00.000Z, + 1970-02-01T00:00:00.000Z, + ], + "timestamp": 1970-02-01T00:00:00.000Z, + } + `); + + expect(Todo.toJSON(Todo.decode(output))).toMatchInlineSnapshot(` + Object { + "id": "6883ed6e-bd0d-4817-ba58-c2a53c73edc2", + "mapOfTimestamps": Object { + "feb1": "1970-02-01T00:00:00.000Z", + "jan1": "1970-01-01T00:00:00.000Z", + }, + "repeatedTimestamp": Array [ + "1970-01-01T00:00:00.000Z", + "1970-02-01T00:00:00.000Z", + ], + "timestamp": "1970-02-01T00:00:00.000Z", + } + `); + }); +}); diff --git a/integration/use-date-true/use-date-true.bin b/integration/use-date-true/use-date-true.bin new file mode 100644 index 0000000000000000000000000000000000000000..c83398b30547058b94094eccb93a47c0ca2df1ee GIT binary patch literal 6986 zcmbtZOLN=S73M>dW!Q;rI-T)2o#a%BVlpK`Qt~5~lXfTylHgLLN`O{mPileWrGx|o zSOAnPyUwDkbkl#(O_%u>-FMZ_bf){}7j)BQzjFbQv@P3xa46}%&imYR&OK7DjssEZ zTcIe0{#d9Z-wVC#i;LOI?_5hQrJnw^tbBOrd54ZD%dPHsP!8>(2tsQ(icaS)MF*l8 z4lLbM&YQ`Z+%@G~AbOtL53Z#tozJ$DY3@B`-nCpWxR$~Cd^?(~f2&;Wc|-Ljoa^sS zFzzyoyXl8NJ+#B4aaYCZ@}cKg?%|ZhQFuBM!IaZ~rPF`RWVd$Lzt4QICB;!C8gQ%b z3CH<`>z%kJ!`%OSNjaCB{~-0dbXxiIMJ0VPm;E4>Tlv#Pk`O)5q4n`#ApC$z6ydrN z5Hx6!?S{hd9f1=;1J55?p+a?Ubn4rOMs zZP)ToC0{|2PM|yTeEH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J;eCD!zAc+In#f!>? zI1FUxGMAN$$)&?=F_|G?%$>iG%(9rhdnpFP)_msEcq_fYv-gu3@BMf=nE`Abg=a(! zeX)hi=kZo%j%P|T1CoUcA15;)Sy;Z4%z$KJ?Ou!olXDX!Ss*z#L6QZMa}y+4AUQWd kl4ZCF5`ZmcejekW1K46FpUeQZSXfJD09*XxXUV1dH+bPEX#fBK literal 0 HcmV?d00001 diff --git a/integration/use-date-true/use-date-true.proto b/integration/use-date-true/use-date-true.proto new file mode 100644 index 000000000..d7cf85f20 --- /dev/null +++ b/integration/use-date-true/use-date-true.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; +import "google/protobuf/timestamp.proto"; + +message Todo { + string id = 1; + google.protobuf.Timestamp timestamp = 2; + repeated google.protobuf.Timestamp repeated_timestamp = 3; + optional google.protobuf.Timestamp optional_timestamp = 4; + map map_of_timestamps = 5; +} diff --git a/integration/use-date-true/use-date-true.ts b/integration/use-date-true/use-date-true.ts new file mode 100644 index 000000000..604ca1d3c --- /dev/null +++ b/integration/use-date-true/use-date-true.ts @@ -0,0 +1,274 @@ +/* eslint-disable */ +import { util, configure, Writer, Reader } from 'protobufjs/minimal'; +import * as Long from 'long'; +import { Timestamp } from './google/protobuf/timestamp'; + +export const protobufPackage = ''; + +export interface Todo { + id: string; + timestamp: Date | undefined; + repeatedTimestamp: Date[]; + optionalTimestamp?: Date | undefined; + mapOfTimestamps: { [key: string]: Date }; +} + +export interface Todo_MapOfTimestampsEntry { + key: string; + value: Date | undefined; +} + +const baseTodo: object = { id: '' }; + +export const Todo = { + encode(message: Todo, writer: Writer = Writer.create()): Writer { + if (message.id !== '') { + writer.uint32(10).string(message.id); + } + if (message.timestamp !== undefined) { + Timestamp.encode(toTimestamp(message.timestamp), writer.uint32(18).fork()).ldelim(); + } + for (const v of message.repeatedTimestamp) { + Timestamp.encode(toTimestamp(v!), writer.uint32(26).fork()).ldelim(); + } + if (message.optionalTimestamp !== undefined) { + Timestamp.encode(toTimestamp(message.optionalTimestamp), writer.uint32(34).fork()).ldelim(); + } + Object.entries(message.mapOfTimestamps).forEach(([key, value]) => { + Todo_MapOfTimestampsEntry.encode({ key: key as any, value }, writer.uint32(42).fork()).ldelim(); + }); + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Todo { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseTodo } as Todo; + message.repeatedTimestamp = []; + message.mapOfTimestamps = {}; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.id = reader.string(); + break; + case 2: + message.timestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + case 3: + message.repeatedTimestamp.push(fromTimestamp(Timestamp.decode(reader, reader.uint32()))); + break; + case 4: + message.optionalTimestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + case 5: + const entry5 = Todo_MapOfTimestampsEntry.decode(reader, reader.uint32()); + if (entry5.value !== undefined) { + message.mapOfTimestamps[entry5.key] = entry5.value; + } + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Todo { + const message = { ...baseTodo } as Todo; + message.repeatedTimestamp = []; + message.mapOfTimestamps = {}; + if (object.id !== undefined && object.id !== null) { + message.id = String(object.id); + } else { + message.id = ''; + } + if (object.timestamp !== undefined && object.timestamp !== null) { + message.timestamp = fromJsonTimestamp(object.timestamp); + } else { + message.timestamp = undefined; + } + if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { + for (const e of object.repeatedTimestamp) { + message.repeatedTimestamp.push(fromJsonTimestamp(e)); + } + } + if (object.optionalTimestamp !== undefined && object.optionalTimestamp !== null) { + message.optionalTimestamp = fromJsonTimestamp(object.optionalTimestamp); + } else { + message.optionalTimestamp = undefined; + } + if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { + Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + message.mapOfTimestamps[key] = fromJsonTimestamp(value); + }); + } + return message; + }, + + toJSON(message: Todo): unknown { + const obj: any = {}; + message.id !== undefined && (obj.id = message.id); + message.timestamp !== undefined && (obj.timestamp = message.timestamp.toISOString()); + if (message.repeatedTimestamp) { + obj.repeatedTimestamp = message.repeatedTimestamp.map((e) => e.toISOString()); + } else { + obj.repeatedTimestamp = []; + } + message.optionalTimestamp !== undefined && (obj.optionalTimestamp = message.optionalTimestamp.toISOString()); + obj.mapOfTimestamps = {}; + if (message.mapOfTimestamps) { + Object.entries(message.mapOfTimestamps).forEach(([k, v]) => { + obj.mapOfTimestamps[k] = v.toISOString(); + }); + } + return obj; + }, + + fromPartial(object: DeepPartial): Todo { + const message = { ...baseTodo } as Todo; + message.repeatedTimestamp = []; + message.mapOfTimestamps = {}; + if (object.id !== undefined && object.id !== null) { + message.id = object.id; + } else { + message.id = ''; + } + if (object.timestamp !== undefined && object.timestamp !== null) { + message.timestamp = object.timestamp; + } else { + message.timestamp = undefined; + } + if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { + for (const e of object.repeatedTimestamp) { + message.repeatedTimestamp.push(e); + } + } + if (object.optionalTimestamp !== undefined && object.optionalTimestamp !== null) { + message.optionalTimestamp = object.optionalTimestamp; + } else { + message.optionalTimestamp = undefined; + } + if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { + Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + if (value !== undefined) { + message.mapOfTimestamps[key] = value; + } + }); + } + return message; + }, +}; + +const baseTodo_MapOfTimestampsEntry: object = { key: '' }; + +export const Todo_MapOfTimestampsEntry = { + encode(message: Todo_MapOfTimestampsEntry, writer: Writer = Writer.create()): Writer { + if (message.key !== '') { + writer.uint32(10).string(message.key); + } + if (message.value !== undefined) { + Timestamp.encode(toTimestamp(message.value), writer.uint32(18).fork()).ldelim(); + } + return writer; + }, + + decode(input: Reader | Uint8Array, length?: number): Todo_MapOfTimestampsEntry { + const reader = input instanceof Uint8Array ? new Reader(input) : input; + let end = length === undefined ? reader.len : reader.pos + length; + const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = fromTimestamp(Timestamp.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }, + + fromJSON(object: any): Todo_MapOfTimestampsEntry { + const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; + if (object.key !== undefined && object.key !== null) { + message.key = String(object.key); + } else { + message.key = ''; + } + if (object.value !== undefined && object.value !== null) { + message.value = fromJsonTimestamp(object.value); + } else { + message.value = undefined; + } + return message; + }, + + toJSON(message: Todo_MapOfTimestampsEntry): unknown { + const obj: any = {}; + message.key !== undefined && (obj.key = message.key); + message.value !== undefined && (obj.value = message.value.toISOString()); + return obj; + }, + + fromPartial(object: DeepPartial): Todo_MapOfTimestampsEntry { + const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; + if (object.key !== undefined && object.key !== null) { + message.key = object.key; + } else { + message.key = ''; + } + if (object.value !== undefined && object.value !== null) { + message.value = object.value; + } else { + message.value = undefined; + } + return message; + }, +}; + +type Builtin = Date | Function | Uint8Array | string | number | undefined; +export type DeepPartial = T extends Builtin + ? T + : T extends Array + ? Array> + : T extends ReadonlyArray + ? ReadonlyArray> + : T extends {} + ? { [K in keyof T]?: DeepPartial } + : Partial; + +function toTimestamp(date: Date): Timestamp { + const seconds = date.getTime() / 1_000; + const nanos = (date.getTime() % 1_000) * 1_000_000; + return { seconds, nanos }; +} + +function fromTimestamp(t: Timestamp): Date { + let millis = t.seconds * 1_000; + millis += t.nanos / 1_000_000; + return new Date(millis); +} + +function fromJsonTimestamp(o: any): Date { + if (o instanceof Date) { + return o; + } else if (typeof o === 'string') { + return new Date(o); + } else { + return fromTimestamp(Timestamp.fromJSON(o)); + } +} + +// If you get a compile-error about 'Constructor and ... have no overlap', +// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. +if (util.Long !== Long) { + util.Long = Long as any; + configure(); +} diff --git a/src/main.ts b/src/main.ts index 5315baebe..1003eb5dc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,7 +55,7 @@ import { } from './generate-grpc-web'; import { generateEnum } from './enums'; import { visit, visitServices } from './visit'; -import { EnvOption, LongOption, OneofOption, Options } from './options'; +import { EnvOption, LongOption, OneofOption, Options, DateOption } from './options'; import { Context } from './context'; import { generateSchema } from './schema'; import { ConditionalOutput } from 'ts-poet/build/ConditionalOutput'; @@ -416,51 +416,68 @@ function makeTimestampMethods(options: Options, longs: ReturnType>> 0; writeSnippet = (place) => code`writer.uint32(${tag}).${toReaderCall(field)}(${place})`; - } else if (isTimestamp(field) && options.useDate) { + } else if (isTimestamp(field) && (options.useDate === DateOption.DATE || options.useDate === DateOption.STRING)) { const tag = ((field.number << 3) | 2) >>> 0; const type = basicTypeName(ctx, field, { keepValueType: true }); writeSnippet = (place) => @@ -874,7 +891,12 @@ function generateFromJson(ctx: Context, fullName: string, messageDesc: Descripto const cstr = capitalize(basicTypeName(ctx, field, { keepValueType: true }).toCodeString()); return code`${cstr}(${from})`; } - } else if (isTimestamp(field)) { + } else if (isTimestamp(field) && options.useDate === DateOption.STRING) { + return code`String(${from})`; + } else if ( + isTimestamp(field) && + (options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP) + ) { return code`${utils.fromJsonTimestamp}(${from})`; } else if (isValueType(ctx, field)) { const valueType = valueTypeName(ctx, field.typeName)!; @@ -902,7 +924,12 @@ function generateFromJson(ctx: Context, fullName: string, messageDesc: Descripto const cstr = capitalize(basicTypeName(ctx, valueType).toCodeString()); return code`${cstr}(${from})`; } - } else if (isTimestamp(valueType)) { + } else if (isTimestamp(valueType) && options.useDate === DateOption.STRING) { + return code`String(${from})`; + } else if ( + isTimestamp(valueType) && + (options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP) + ) { return code`${utils.fromJsonTimestamp}(${from})`; } else { const type = basicTypeName(ctx, valueType); @@ -981,10 +1008,12 @@ function generateToJson(ctx: Context, fullName: string, messageDesc: DescriptorP return isWithinOneOf(field) ? code`${from} !== undefined ? ${toJson}(${from}) : undefined` : code`${toJson}(${from})`; - } else if (isTimestamp(field)) { - return options.useDate - ? code`${from} !== undefined ? ${from}.toISOString() : null` - : code`${from} !== undefined ? ${utils.fromTimestamp}(${from}).toISOString() : null`; + } else if (isTimestamp(field) && options.useDate === DateOption.DATE) { + return code`${from}.toISOString()`; + } else if (isTimestamp(field) && options.useDate === DateOption.STRING) { + return code`${from}`; + } else if (isTimestamp(field) && options.useDate === DateOption.TIMESTAMP) { + return code`${utils.fromTimestamp}(${from}).toISOString()`; } else if (isMapType(ctx, messageDesc, field)) { // For map types, drill-in and then admittedly re-hard-code our per-value-type logic const valueType = (typeMap.get(field.typeName)![2] as DescriptorProto).field[1]; @@ -993,8 +1022,12 @@ function generateToJson(ctx: Context, fullName: string, messageDesc: DescriptorP return code`${toJson}(${from})`; } else if (isBytes(valueType)) { return code`${utils.base64FromBytes}(${from})`; - } else if (isTimestamp(valueType)) { - return options.useDate ? code`${from}.toISOString()` : code`${utils.fromTimestamp}(${from}).toISOString()`; + } else if (isTimestamp(valueType) && options.useDate === DateOption.DATE) { + return code`${from}.toISOString()`; + } else if (isTimestamp(valueType) && options.useDate === DateOption.STRING) { + return code`${from}`; + } else if (isTimestamp(valueType) && options.useDate === DateOption.TIMESTAMP) { + return code`${utils.fromTimestamp}(${from}).toISOString()`; } else if (isScalar(valueType)) { return code`${from}`; } else { @@ -1056,6 +1089,7 @@ function generateToJson(ctx: Context, fullName: string, messageDesc: DescriptorP function generateFromPartial(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code { const { options, utils, typeMap } = ctx; const chunks: Code[] = []; + const Timestamp = imp('Timestamp@./google/protobuf/timestamp'); // create the basic function declaration chunks.push(code` @@ -1075,9 +1109,11 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri const fieldName = maybeSnakeToCamel(field.name, options); const readSnippet = (from: string): Code => { - if (isPrimitive(field) || isValueType(ctx, field)) { - return code`${from}`; - } else if (isTimestamp(field) && options.useDate) { + if ( + isPrimitive(field) || + (isTimestamp(field) && (options.useDate === DateOption.DATE || options.useDate === DateOption.STRING)) || + isValueType(ctx, field) + ) { return code`${from}`; } else if (isMessage(field)) { if (isRepeated(field) && isMapType(ctx, messageDesc, field)) { @@ -1091,7 +1127,10 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri const cstr = capitalize(basicTypeName(ctx, valueType).toCodeString()); return code`${cstr}(${from})`; } - } else if (isTimestamp(valueType) && options.useDate) { + } else if ( + isTimestamp(valueType) && + (options.useDate === DateOption.DATE || options.useDate === DateOption.STRING) + ) { return code`${from}`; } else { const type = basicTypeName(ctx, valueType); diff --git a/src/options.ts b/src/options.ts index a478cbb4b..ee2e5b8f0 100644 --- a/src/options.ts +++ b/src/options.ts @@ -4,6 +4,12 @@ export enum LongOption { STRING = 'string', } +export enum DateOption { + DATE = 'date', + STRING = 'string', + TIMESTAMP = 'timestamp', +} + export enum EnvOption { NODE = 'node', BROWSER = 'browser', @@ -20,7 +26,7 @@ export type Options = { snakeToCamel: boolean; forceLong: LongOption; useOptionals: boolean; - useDate: boolean; + useDate: DateOption; oneof: OneofOption; esModuleInterop: boolean; outputEncodeMethods: boolean; @@ -50,7 +56,7 @@ export function defaultOptions(): Options { snakeToCamel: true, forceLong: LongOption.NUMBER, useOptionals: false, - useDate: true, + useDate: DateOption.DATE, oneof: OneofOption.PROPERTIES, esModuleInterop: false, lowerCaseServiceMethods: false, @@ -80,7 +86,7 @@ const nestJsOptions: Partial = { outputJsonMethods: false, outputPartialMethods: false, outputClientImpl: false, - useDate: false, + useDate: DateOption.TIMESTAMP, }; export function optionsFromParameter(parameter: string): Options { @@ -100,6 +106,14 @@ export function optionsFromParameter(parameter: string): Options { if ((options.forceLong as any) === true) { options.forceLong = LongOption.LONG; } + + if ((options.useDate as any) === true) { + // Treat useDate=true as DATE + options.useDate = DateOption.DATE; + } else if ((options.useDate as any) === false) { + // Treat useDate=false as TIMESTAMP + options.useDate = DateOption.TIMESTAMP; + } return options; } diff --git a/src/types.ts b/src/types.ts index 1ca1ecf2c..abd07ce08 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,7 +10,7 @@ import { } from 'ts-proto-descriptors/google/protobuf/descriptor'; import { CodeGeneratorRequest } from 'ts-proto-descriptors/google/protobuf/compiler/plugin'; import { code, Code, imp, Import } from 'ts-poet'; -import { EnvOption, LongOption, OneofOption, Options } from './options'; +import { DateOption, EnvOption, LongOption, OneofOption, Options } from './options'; import { visit } from './visit'; import { fail } from './utils'; import SourceInfo from './sourceInfo'; @@ -436,8 +436,14 @@ export function messageToTypeName( return code`${valueType} | undefined`; } // Look for other special prototypes like Timestamp that aren't technically wrapper types - if (!typeOptions.keepValueType && protoType === '.google.protobuf.Timestamp' && options.useDate) { - return code`Date`; + if (!typeOptions.keepValueType && protoType === '.google.protobuf.Timestamp') { + if (options.useDate == DateOption.DATE) { + return code`Date`; + } + + if (options.useDate == DateOption.STRING) { + return code`string`; + } } const [module, type] = toModuleAndType(typeMap, protoType); return code`${imp(`${type}@./${module}`)}`; diff --git a/tests/options-test.ts b/tests/options-test.ts index eb1282ea8..c4430caf4 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -27,7 +27,7 @@ describe('options', () => { "snakeToCamel": true, "stringEnums": false, "unrecognizedEnum": true, - "useDate": false, + "useDate": "timestamp", "useOptionals": false, } `);