Skip to content

Commit

Permalink
fix: initialize undefined optional fields upon use (#802)
Browse files Browse the repository at this point in the history
* fix: initialize undefined optional fields upon use

* chore: integration
  • Loading branch information
davidzeng0 committed Mar 19, 2023
1 parent e243bfc commit ee52e06
Show file tree
Hide file tree
Showing 7 changed files with 904 additions and 26 deletions.
16 changes: 8 additions & 8 deletions integration/groups/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const GroupsOptionalTest = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsOptionalTest {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsOptionalTest();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -180,7 +180,7 @@ export const GroupsOptionalTest_Group = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsOptionalTest_Group {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsOptionalTest_Group();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -281,7 +281,7 @@ export const GroupsRepeatedTest = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsRepeatedTest {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsRepeatedTest();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -416,7 +416,7 @@ export const GroupsRepeatedTest_Group = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsRepeatedTest_Group {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsRepeatedTest_Group();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -528,7 +528,7 @@ export const GroupsNestedTest = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsNestedTest {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsNestedTest();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -658,7 +658,7 @@ export const GroupsNestedTest_Group = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsNestedTest_Group {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsNestedTest_Group();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -744,7 +744,7 @@ export const GroupsNestedTest_Group_Nested = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsNestedTest_Group_Nested {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsNestedTest_Group_Nested();
(message as any)._unknownFields = {};
Expand Down Expand Up @@ -830,7 +830,7 @@ export const GroupsNestedTest_Group_Nested_Nested2 = {
},

decode(input: _m0.Reader | Uint8Array, length?: number): GroupsNestedTest_Group_Nested_Nested2 {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGroupsNestedTest_Group_Nested_Nested2();
(message as any)._unknownFields = {};
Expand Down
71 changes: 71 additions & 0 deletions integration/use-optionals-no-undefined/optionals-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { OptionalsTest, StateEnum } from './test'

describe('useOptionals=all initializeFieldsAsUndefined=false', () => {
it('has all optional members', () => {
const test: OptionalsTest = {};
const data = OptionalsTest.encode(test).finish();
const test2 = OptionalsTest.decode(data);
expect(test2).toEqual({});
});

it('allows setting all members, too', () => {
const test: OptionalsTest = {
id: 1,
child: {},
state: StateEnum.ON,
long: 10,
truth: true,
description: "hello world",
data: Buffer.alloc(2).fill(0x32),

repId: [1, 2],
repChild: [{}, {}],
repLong: [11, 12],
repTruth: [true, false],
repDescription: ["hello", "world"],
repData: [Buffer.alloc(3).fill(0x33), Buffer.alloc(4).fill(0x34), Buffer.alloc(5).fill(0x35)],

optChild: {},
optId: 2,
optState: StateEnum.OFF,
optTruth: true,
optDescription: "mumble",
optData: Buffer.alloc(6).fill(0x36),

translations: {
"hello": "hallo",
"world": "wereld",
},
};
const data = OptionalsTest.encode(test).finish();
const test2 = OptionalsTest.decode(data);
expect(test2).toEqual({
id: 1,
child: {},
state: StateEnum.ON,
long: 10,
truth: true,
description: "hello world",
data: Buffer.alloc(2).fill(0x32),

repId: [1, 2],
repChild: [{}, {}],
repLong: [11, 12],
repTruth: [true, false],
repDescription: ["hello", "world"],
repData: [Buffer.alloc(3).fill(0x33), Buffer.alloc(4).fill(0x34), Buffer.alloc(5).fill(0x35)],

optChild: {},
optId: 2,
optState: StateEnum.OFF,
optTruth: true,
optDescription: "mumble",
optData: Buffer.alloc(6).fill(0x36),

translations: {
"hello": "hallo",
"world": "wereld",
},
});
});
})
1 change: 1 addition & 0 deletions integration/use-optionals-no-undefined/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
useOptionals=all,initializeFieldsAsUndefined=false
Binary file added integration/use-optionals-no-undefined/test.bin
Binary file not shown.
39 changes: 39 additions & 0 deletions integration/use-optionals-no-undefined/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
syntax = "proto3";
package optionalstest;

message OptionalsTest {
int32 id = 1;
Child child = 2;
StateEnum state = 3;
int64 long = 4;
bool truth = 5;
string description = 6;
bytes data = 7;

repeated int32 rep_id = 11;
repeated Child rep_child = 12;
repeated StateEnum rep_state = 13;
repeated int64 rep_long = 14;
repeated bool rep_truth = 15;
repeated string rep_description = 16;
repeated bytes rep_data = 17;

optional int32 opt_id = 21;
optional Child opt_child = 22;
optional StateEnum opt_state = 23;
optional int64 opt_long = 24;
optional bool opt_truth = 25;
optional string opt_description = 26;
optional bytes opt_data = 27;

map<string, string> translations = 30;
}

enum StateEnum {
UNKNOWN = 0;
ON = 2;
OFF = 3;
}

message Child {
}
Loading

0 comments on commit ee52e06

Please sign in to comment.