Skip to content

Commit

Permalink
feat: enable prototype for defaults for ts-proto-descriptors (#487)
Browse files Browse the repository at this point in the history
chore: add option description to readme
  • Loading branch information
Vilsol authored Jan 21, 2022
1 parent d8635ee commit 2b5640f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 70 deletions.
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=unknownFields=true`, all unknown fields will be parsed and output as arrays of buffers.

- With `--ts_proto_opt=usePrototypeForDefaults=true`, the generated code will wrap new objects with `Object.create`.

### Only Types

If you're looking for `ts-proto` to generate only types for your Protobuf types then passing all three of `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` as `false` is probably what you want, i.e.:
Expand Down
2 changes: 1 addition & 1 deletion protos/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
protoc \
--plugin=./node_modules/ts-proto/protoc-gen-ts_proto \
--ts_proto_out=. \
--ts_proto_opt=exportCommonSymbols=false,unknownFields=true \
--ts_proto_opt=exportCommonSymbols=false,unknownFields=true,usePrototypeForDefaults=true \
./google/protobuf/descriptor.proto \
./google/protobuf/compiler/plugin.proto

Expand Down
16 changes: 8 additions & 8 deletions protos/google/protobuf/compiler/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export const Version = {
decode(input: Reader | Uint8Array, length?: number): Version {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseVersion();
const message = Object.create(createBaseVersion()) as Version;
(message as any)._unknownFields = {};
while (reader.pos < end) {
const tag = reader.uint32();
Expand Down Expand Up @@ -252,7 +252,7 @@ export const Version = {
},

fromPartial<I extends Exact<DeepPartial<Version>, I>>(object: I): Version {
const message = createBaseVersion();
const message = Object.create(createBaseVersion()) as Version;
message.major = object.major ?? 0;
message.minor = object.minor ?? 0;
message.patch = object.patch ?? 0;
Expand Down Expand Up @@ -298,7 +298,7 @@ export const CodeGeneratorRequest = {
decode(input: Reader | Uint8Array, length?: number): CodeGeneratorRequest {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseCodeGeneratorRequest();
const message = Object.create(createBaseCodeGeneratorRequest()) as CodeGeneratorRequest;
(message as any)._unknownFields = {};
while (reader.pos < end) {
const tag = reader.uint32();
Expand Down Expand Up @@ -358,7 +358,7 @@ export const CodeGeneratorRequest = {
},

fromPartial<I extends Exact<DeepPartial<CodeGeneratorRequest>, I>>(object: I): CodeGeneratorRequest {
const message = createBaseCodeGeneratorRequest();
const message = Object.create(createBaseCodeGeneratorRequest()) as CodeGeneratorRequest;
message.fileToGenerate = object.fileToGenerate?.map((e) => e) || [];
message.parameter = object.parameter ?? '';
message.protoFile = object.protoFile?.map((e) => FileDescriptorProto.fromPartial(e)) || [];
Expand Down Expand Up @@ -404,7 +404,7 @@ export const CodeGeneratorResponse = {
decode(input: Reader | Uint8Array, length?: number): CodeGeneratorResponse {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseCodeGeneratorResponse();
const message = Object.create(createBaseCodeGeneratorResponse()) as CodeGeneratorResponse;
(message as any)._unknownFields = {};
while (reader.pos < end) {
const tag = reader.uint32();
Expand Down Expand Up @@ -452,7 +452,7 @@ export const CodeGeneratorResponse = {
},

fromPartial<I extends Exact<DeepPartial<CodeGeneratorResponse>, I>>(object: I): CodeGeneratorResponse {
const message = createBaseCodeGeneratorResponse();
const message = Object.create(createBaseCodeGeneratorResponse()) as CodeGeneratorResponse;
message.error = object.error ?? '';
message.supportedFeatures = object.supportedFeatures ?? 0;
message.file = object.file?.map((e) => CodeGeneratorResponse_File.fromPartial(e)) || [];
Expand Down Expand Up @@ -497,7 +497,7 @@ export const CodeGeneratorResponse_File = {
decode(input: Reader | Uint8Array, length?: number): CodeGeneratorResponse_File {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseCodeGeneratorResponse_File();
const message = Object.create(createBaseCodeGeneratorResponse_File()) as CodeGeneratorResponse_File;
(message as any)._unknownFields = {};
while (reader.pos < end) {
const tag = reader.uint32();
Expand Down Expand Up @@ -551,7 +551,7 @@ export const CodeGeneratorResponse_File = {
},

fromPartial<I extends Exact<DeepPartial<CodeGeneratorResponse_File>, I>>(object: I): CodeGeneratorResponse_File {
const message = createBaseCodeGeneratorResponse_File();
const message = Object.create(createBaseCodeGeneratorResponse_File()) as CodeGeneratorResponse_File;
message.name = object.name ?? '';
message.insertionPoint = object.insertionPoint ?? '';
message.content = object.content ?? '';
Expand Down
Loading

0 comments on commit 2b5640f

Please sign in to comment.