-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add useOptionals=all to enable non-field members to be optional. (
#402) * feat: Add useOptionals=all. * Update codegen. Co-authored-by: Stephen Haberman <stephen.haberman@gmail.com>
- Loading branch information
Showing
10 changed files
with
866 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { OptionalsTest, StateEnum } from './test' | ||
|
||
describe('useOptionals=all', () => { | ||
it('has all optional members', () => { | ||
const test: OptionalsTest = {}; | ||
const data = OptionalsTest.encode(test).finish(); | ||
const test2 = OptionalsTest.decode(data); | ||
expect(test2).toEqual({ | ||
id: 0, | ||
child: undefined, | ||
state: StateEnum.UNKNOWN, | ||
long: 0, | ||
truth: false, | ||
description: "", | ||
data: new Uint8Array(0), | ||
|
||
repId: [], | ||
repChild: [], | ||
repState: [], | ||
repLong: [], | ||
repTruth: [], | ||
repDescription: [], | ||
repData: [], | ||
|
||
optChild: undefined, | ||
optId: undefined, | ||
optState: undefined, | ||
optLong: undefined, | ||
optTruth: undefined, | ||
optDescription: undefined, | ||
optData: undefined, | ||
|
||
translations: {}, | ||
}); | ||
}); | ||
|
||
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: [{}, {}], | ||
repState: [StateEnum.ON, StateEnum.OFF], | ||
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, | ||
optLong: 13, | ||
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: [{}, {}], | ||
repState: [StateEnum.ON, StateEnum.OFF], | ||
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, | ||
optLong: 13, | ||
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 |
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.