Skip to content

Commit

Permalink
feat: Add useOptionals=all to enable non-field members to be optional. (
Browse files Browse the repository at this point in the history
#402)

* feat: Add useOptionals=all.

* Update codegen.

Co-authored-by: Stephen Haberman <stephen.haberman@gmail.com>
  • Loading branch information
sgielen and stephenh committed Dec 14, 2021
1 parent 5e30965 commit e7b70cb
Show file tree
Hide file tree
Showing 10 changed files with 866 additions and 33 deletions.
101 changes: 101 additions & 0 deletions integration/use-optionals-all/optionals-test.ts
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",
},
});
});
})
1 change: 1 addition & 0 deletions integration/use-optionals-all/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
useOptionals=all
Binary file added integration/use-optionals-all/test.bin
Binary file not shown.
39 changes: 39 additions & 0 deletions integration/use-optionals-all/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 {
}

0 comments on commit e7b70cb

Please sign in to comment.