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: support proto2 optional and default value fields #1007

Merged
merged 20 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=initializeFieldsAsUndefined=false`, all optional field initializers will be omited from the generated base instances.

- With `--ts_proto_opt=disableProto2Optionals=true`, all optional fields on proto2 files will not be set to be optional. Please note that this flag is primarily for preserving ts-proto's legacy handling of proto2 files, to avoid breaking changes, and as a result, it is not intended to be used moving forward.

- With `--ts_proto_opt=disableProto2DefaultValues=true`, all fields in proto2 files that specify a default value will not actually use that default value. Please note that this flag is primarily for preserving ts-proto's legacy handling of proto2 files, to avoid breaking changes, and as a result, it is not intended to be used moving forward.

- With `--ts_proto_opt=Mgoogle/protobuf/empty.proto=./google3/protobuf/empty`, ('M' means 'importMapping', similar to [protoc-gen-go](https://developers.google.com/protocol-buffers/docs/reference/go-generated#package)), the generated code import path for `./google/protobuf/empty.ts` will reflect the overridden value:

- `Mfoo/bar.proto=@myorg/some-lib` will map `foo/bar.proto` imports into `import ... from '@myorg/some-lib'`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function createBaseBoolValue(): BoolValue {

export const BoolValue = {
encode(message: BoolValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down Expand Up @@ -477,7 +477,7 @@ export const BoolValue = {

toJSON(message: BoolValue): unknown {
const obj: any = {};
if (message.value === true) {
if (message.value !== false) {
obj.value = message.value;
}
return obj;
Expand Down
4 changes: 2 additions & 2 deletions integration/bytes-node/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function createBaseBoolValue(): BoolValue {

export const BoolValue = {
encode(message: BoolValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down Expand Up @@ -477,7 +477,7 @@ export const BoolValue = {

toJSON(message: BoolValue): unknown {
const obj: any = {};
if (message.value === true) {
if (message.value !== false) {
obj.value = message.value;
}
return obj;
Expand Down
8 changes: 4 additions & 4 deletions integration/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generateFile, makeUtils } from "../src/main";
import { createTypeMap } from "../src/types";
import { generateIndexFiles } from "../src/utils";
import { getTsPoetOpts, optionsFromParameter } from "../src/options";
import { Context } from "../src/context";
import { BaseContext, createFileContext } from "../src/context";
import { generateTypeRegistry } from "../src/generate-type-registry";

/**
Expand Down Expand Up @@ -37,8 +37,8 @@ async function generate(binFile: string, baseDir: string, parameter: string) {
continue;
}
const utils = makeUtils(options);
const ctx: Context = { options, typeMap, utils };
const [path, code] = generateFile(ctx, file);
const ctx: BaseContext = { options, typeMap, utils };
const [path, code] = generateFile({ ...ctx, currentFile: createFileContext(file) }, file);
const filePath = `${baseDir}/${path}`;
const dirPath = parse(filePath).dir;
await promisify(mkdir)(dirPath, { recursive: true }).catch(() => {});
Expand All @@ -47,7 +47,7 @@ async function generate(binFile: string, baseDir: string, parameter: string) {

if (options.outputTypeRegistry) {
const utils = makeUtils(options);
const ctx: Context = { options, typeMap, utils };
const ctx: BaseContext = { options, typeMap, utils };

const path = "typeRegistry.ts";
const code = generateTypeRegistry(ctx);
Expand Down
2 changes: 1 addition & 1 deletion integration/emit-default-values-json/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const DefaultValuesTest = {
if (message.long !== 0) {
writer.uint32(32).int64(message.long);
}
if (message.truth === true) {
if (message.truth !== false) {
writer.uint32(40).bool(message.truth);
}
if (message.description !== "") {
Expand Down
4 changes: 2 additions & 2 deletions integration/extensions/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export const long: Extension<Long> = {
packed: false,
encode: (value: Long): Uint8Array[] => {
const encoded: Uint8Array[] = [];
if (value !== undefined && !value.isZero()) {
if (value !== undefined && !value.equals(Long.ZERO)) {
const writer = _m0.Writer.create();
writer.int64(value);
encoded.push(writer.finish());
Expand All @@ -542,7 +542,7 @@ export const fixed: Extension<Long> = {
packed: false,
encode: (value: Long): Uint8Array[] => {
const encoded: Uint8Array[] = [];
if (value !== undefined && !value.isZero()) {
if (value !== undefined && !value.equals(Long.UZERO)) {
const writer = _m0.Writer.create();
writer.fixed64(value);
encoded.push(writer.finish());
Expand Down
4 changes: 2 additions & 2 deletions integration/global-this/global-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function createBaseBoolean(): Boolean {

export const Boolean = {
encode(message: Boolean, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down Expand Up @@ -239,7 +239,7 @@ export const Boolean = {

toJSON(message: Boolean): unknown {
const obj: any = {};
if (message.value === true) {
if (message.value !== false) {
obj.value = message.value;
}
return obj;
Expand Down
4 changes: 2 additions & 2 deletions integration/grpc-js/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function createBaseBoolValue(): BoolValue {

export const BoolValue = {
encode(message: BoolValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down Expand Up @@ -477,7 +477,7 @@ export const BoolValue = {

toJSON(message: BoolValue): unknown {
const obj: any = {};
if (message.value === true) {
if (message.value !== false) {
obj.value = message.value;
}
return obj;
Expand Down
4 changes: 2 additions & 2 deletions integration/grpc-web/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function createBaseBoolValue(): BoolValue {

export const BoolValue = {
encode(message: BoolValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down Expand Up @@ -477,7 +477,7 @@ export const BoolValue = {

toJSON(message: BoolValue): unknown {
const obj: any = {};
if (message.value === true) {
if (message.value !== false) {
obj.value = message.value;
}
return obj;
Expand Down
8 changes: 4 additions & 4 deletions integration/map-long-optional/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ function createBaseMapBigInt_MapEntry(): MapBigInt_MapEntry {

export const MapBigInt_MapEntry = {
encode(message: MapBigInt_MapEntry, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (!message.key.isZero()) {
if (!message.key.equals(Long.UZERO)) {
writer.uint32(9).fixed64(message.key);
}
if (!message.value.isZero()) {
if (!message.value.equals(Long.ZERO)) {
writer.uint32(16).int64(message.value);
}
if (message._unknownFields !== undefined) {
Expand Down Expand Up @@ -204,10 +204,10 @@ export const MapBigInt_MapEntry = {

toJSON(message: MapBigInt_MapEntry): unknown {
const obj: any = {};
if (!message.key.isZero()) {
if (!message.key.equals(Long.UZERO)) {
obj.key = (message.key || Long.UZERO).toString();
}
if (!message.value.isZero()) {
if (!message.value.equals(Long.ZERO)) {
obj.value = (message.value || Long.ZERO).toString();
}
return obj;
Expand Down
2 changes: 1 addition & 1 deletion integration/meta-typings/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ function createBaseBoolValue(): BoolValue {

export const BoolValue = {
encode(message: BoolValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down
4 changes: 2 additions & 2 deletions integration/nice-grpc/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function createBaseBoolValue(): BoolValue {

export const BoolValue = {
encode(message: BoolValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value === true) {
if (message.value !== false) {
writer.uint32(8).bool(message.value);
}
return writer;
Expand Down Expand Up @@ -477,7 +477,7 @@ export const BoolValue = {

toJSON(message: BoolValue): unknown {
const obj: any = {};
if (message.value === true) {
if (message.value !== false) {
obj.value = message.value;
}
return obj;
Expand Down
4 changes: 2 additions & 2 deletions integration/omit-optionals/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function createBaseTestMessage(): TestMessage {

export const TestMessage = {
encode(message: TestMessage, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.field1 === true) {
if (message.field1 !== false) {
writer.uint32(8).bool(message.field1);
}
if (message.field2 !== undefined) {
Expand Down Expand Up @@ -62,7 +62,7 @@ export const TestMessage = {

toJSON(message: TestMessage): unknown {
const obj: any = {};
if (message.field1 === true) {
if (message.field1 !== false) {
obj.field1 = message.field1;
}
if (message.field2 !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions integration/optional-long/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function createBaseTimestamp(): Timestamp {

export const Timestamp = {
encode(message: Timestamp, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.seconds !== undefined && !message.seconds.isZero()) {
if (message.seconds !== undefined && !message.seconds.equals(Long.ZERO)) {
writer.uint32(8).int64(message.seconds);
}
if (message.nanos !== undefined && message.nanos !== 0) {
Expand Down Expand Up @@ -167,7 +167,7 @@ export const Timestamp = {

toJSON(message: Timestamp): unknown {
const obj: any = {};
if (message.seconds !== undefined && !message.seconds.isZero()) {
if (message.seconds !== undefined && !message.seconds.equals(Long.ZERO)) {
obj.seconds = (message.seconds || Long.ZERO).toString();
}
if (message.nanos !== undefined && message.nanos !== 0) {
Expand Down
Loading
Loading