Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add type annotations flag #786

Merged
merged 5 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.

- With `--ts_proto_opt=outputTypeRegistry=true`, the type registry will be generated that can be used to resolve message types by fully-qualified name. Also, each message will get extra `$type` field containing fully-qualified name.
- With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name.

- With `--ts_proto_opt=outputTypeRegistry=true`, the type registry will be generated that can be used to resolve message types by fully-qualified name. Also, each message will be given a `$type` field containing its fully-qualified name.

- With `--ts_proto_opt=outputServices=grpc-js`, ts-proto will output service definitions and server / client stubs in [grpc-js](https://github.com/grpc/grpc-node/tree/master/packages/grpc-js) format.

Expand Down
Binary file added integration/type-annotations/bar/bar.bin
Binary file not shown.
9 changes: 9 additions & 0 deletions integration/type-annotations/bar/bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package foo.bar;

import "foo.proto";

message Bar {
Foo foo = 1;
}
78 changes: 78 additions & 0 deletions integration/type-annotations/bar/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";
import { Foo } from "../foo";

export const protobufPackage = "foo.bar";

export interface Bar {
$type: "foo.bar.Bar";
foo: Foo | undefined;
}

function createBaseBar(): Bar {
return { $type: "foo.bar.Bar", foo: undefined };
}

export const Bar = {
$type: "foo.bar.Bar" as const,

encode(message: Bar, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.foo !== undefined) {
Foo.encode(message.foo, writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Bar {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseBar();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.foo = Foo.decode(reader, reader.uint32());
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): Bar {
return { $type: Bar.$type, foo: isSet(object.foo) ? Foo.fromJSON(object.foo) : undefined };
},

toJSON(message: Bar): unknown {
const obj: any = {};
message.foo !== undefined && (obj.foo = message.foo ? Foo.toJSON(message.foo) : undefined);
return obj;
},

create<I extends Exact<DeepPartial<Bar>, I>>(base?: I): Bar {
return Bar.fromPartial(base ?? {});
},

fromPartial<I extends Exact<DeepPartial<Bar>, I>>(object: I): Bar {
const message = createBaseBar();
message.foo = (object.foo !== undefined && object.foo !== null) ? Foo.fromPartial(object.foo) : undefined;
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in Exclude<keyof T, "$type">]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P> | "$type">]: never };

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Binary file added integration/type-annotations/foo.bin
Binary file not shown.
18 changes: 18 additions & 0 deletions integration/type-annotations/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package foo;

import "google/protobuf/timestamp.proto";
import "google/protobuf/struct.proto";

message Foo {
google.protobuf.Timestamp timestamp = 1;
}

message Foo2 {
google.protobuf.Timestamp timestamp = 1;
}

message WithStruct {
google.protobuf.Struct struct = 1;
}
221 changes: 221 additions & 0 deletions integration/type-annotations/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";
import { Struct } from "./google/protobuf/struct";
import { Timestamp } from "./google/protobuf/timestamp";

export const protobufPackage = "foo";

export interface Foo {
$type: "foo.Foo";
timestamp: Date | undefined;
}

export interface Foo2 {
$type: "foo.Foo2";
timestamp: Date | undefined;
}

export interface WithStruct {
$type: "foo.WithStruct";
struct: { [key: string]: any } | undefined;
}

function createBaseFoo(): Foo {
return { $type: "foo.Foo", timestamp: undefined };
}

export const Foo = {
$type: "foo.Foo" as const,

encode(message: Foo, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.timestamp !== undefined) {
Timestamp.encode(toTimestamp(message.timestamp), writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Foo {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseFoo();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.timestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): Foo {
return { $type: Foo.$type, timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined };
},

toJSON(message: Foo): unknown {
const obj: any = {};
message.timestamp !== undefined && (obj.timestamp = message.timestamp.toISOString());
return obj;
},

create<I extends Exact<DeepPartial<Foo>, I>>(base?: I): Foo {
return Foo.fromPartial(base ?? {});
},

fromPartial<I extends Exact<DeepPartial<Foo>, I>>(object: I): Foo {
const message = createBaseFoo();
message.timestamp = object.timestamp ?? undefined;
return message;
},
};

function createBaseFoo2(): Foo2 {
return { $type: "foo.Foo2", timestamp: undefined };
}

export const Foo2 = {
$type: "foo.Foo2" as const,

encode(message: Foo2, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.timestamp !== undefined) {
Timestamp.encode(toTimestamp(message.timestamp), writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Foo2 {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseFoo2();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.timestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): Foo2 {
return { $type: Foo2.$type, timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined };
},

toJSON(message: Foo2): unknown {
const obj: any = {};
message.timestamp !== undefined && (obj.timestamp = message.timestamp.toISOString());
return obj;
},

create<I extends Exact<DeepPartial<Foo2>, I>>(base?: I): Foo2 {
return Foo2.fromPartial(base ?? {});
},

fromPartial<I extends Exact<DeepPartial<Foo2>, I>>(object: I): Foo2 {
const message = createBaseFoo2();
message.timestamp = object.timestamp ?? undefined;
return message;
},
};

function createBaseWithStruct(): WithStruct {
return { $type: "foo.WithStruct", struct: undefined };
}

export const WithStruct = {
$type: "foo.WithStruct" as const,

encode(message: WithStruct, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.struct !== undefined) {
Struct.encode(Struct.wrap(message.struct), writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): WithStruct {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseWithStruct();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.struct = Struct.unwrap(Struct.decode(reader, reader.uint32()));
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): WithStruct {
return { $type: WithStruct.$type, struct: isObject(object.struct) ? object.struct : undefined };
},

toJSON(message: WithStruct): unknown {
const obj: any = {};
message.struct !== undefined && (obj.struct = message.struct);
return obj;
},

create<I extends Exact<DeepPartial<WithStruct>, I>>(base?: I): WithStruct {
return WithStruct.fromPartial(base ?? {});
},

fromPartial<I extends Exact<DeepPartial<WithStruct>, I>>(object: I): WithStruct {
const message = createBaseWithStruct();
message.struct = object.struct ?? undefined;
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in Exclude<keyof T, "$type">]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P> | "$type">]: never };

function toTimestamp(date: Date): Timestamp {
const seconds = date.getTime() / 1_000;
const nanos = (date.getTime() % 1_000) * 1_000_000;
return { $type: "google.protobuf.Timestamp", 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));
}
}

function isObject(value: any): boolean {
return typeof value === "object" && value !== null;
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Loading