-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initialize undefined optional fields upon use (#802)
* fix: initialize undefined optional fields upon use * chore: integration
- Loading branch information
1 parent
e243bfc
commit ee52e06
Showing
7 changed files
with
904 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
useOptionals=all,initializeFieldsAsUndefined=false |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
Oops, something went wrong.