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: Add useOptionals=all to enable non-field members to be optional. #402

Merged
merged 2 commits into from
Dec 14, 2021
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
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 {
}
Loading