diff --git a/README.markdown b/README.markdown index 22a1ca3f9..70183baa6 100644 --- a/README.markdown +++ b/README.markdown @@ -26,6 +26,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 @@ -293,7 +294,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. @@ -410,6 +411,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/bar/bar.bin b/integration/type-registry/bar/bar.bin index 792e0d452..ad66e39c1 100644 Binary files a/integration/type-registry/bar/bar.bin and b/integration/type-registry/bar/bar.bin differ diff --git a/integration/type-registry/foo.bin b/integration/type-registry/foo.bin index ee29d4b22..f3f967f61 100644 Binary files a/integration/type-registry/foo.bin and b/integration/type-registry/foo.bin differ 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/type-registry/google/protobuf/timestamp.ts b/integration/type-registry/google/protobuf/timestamp.ts index 4b951a24d..ab0527dae 100644 --- a/integration/type-registry/google/protobuf/timestamp.ts +++ b/integration/type-registry/google/protobuf/timestamp.ts @@ -57,16 +57,7 @@ export const protobufPackage = 'google.protobuf'; * .setNanos((int) ((millis % 1000) * 1000000)).build(); * * - * Example 5: Compute Timestamp from Java `Instant.now()`. - * - * Instant now = Instant.now(); - * - * Timestamp timestamp = - * Timestamp.newBuilder().setSeconds(now.getEpochSecond()) - * .setNanos(now.getNano()).build(); - * - * - * Example 6: Compute Timestamp from current time in Python. + * Example 5: Compute Timestamp from current time in Python. * * timestamp = Timestamp() * timestamp.GetCurrentTime() 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 000000000..fade300b8 Binary files /dev/null and b/integration/use-date-string/use-date-string.bin differ diff --git a/integration/use-date-string/use-date-string.proto b/integration/use-date-string/use-date-string.proto new file mode 100644 index 000000000..d7cf85f20 --- /dev/null +++ b/integration/use-date-string/use-date-string.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-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 000000000..c83398b30 Binary files /dev/null and b/integration/use-date-true/use-date-true.bin differ 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 f683ebce6..6ae041188 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'; @@ -409,51 +409,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) => @@ -867,7 +884,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)!; @@ -895,7 +917,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); @@ -974,10 +1001,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]; @@ -986,8 +1015,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 { @@ -1049,6 +1082,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` @@ -1068,9 +1102,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)) { @@ -1084,7 +1120,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 441a0bf48..2f4709aab 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; @@ -47,7 +53,7 @@ export function defaultOptions(): Options { snakeToCamel: true, forceLong: LongOption.NUMBER, useOptionals: false, - useDate: true, + useDate: DateOption.DATE, oneof: OneofOption.PROPERTIES, esModuleInterop: false, lowerCaseServiceMethods: false, @@ -75,7 +81,7 @@ const nestJsOptions: Partial = { outputJsonMethods: false, outputPartialMethods: false, outputClientImpl: false, - useDate: false, + useDate: DateOption.TIMESTAMP, }; export function optionsFromParameter(parameter: string): Options { @@ -91,6 +97,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 511ade1c6..d582dc9e3 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -25,7 +25,7 @@ describe('options', () => { "snakeToCamel": true, "stringEnums": false, "unrecognizedEnum": true, - "useDate": false, + "useDate": "timestamp", "useOptionals": false, } `);