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

Compiler flag oneof=unions generates type unions for oneof fields instead of individual properties #95

Merged
merged 1 commit into from
Jul 11, 2020
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
157 changes: 157 additions & 0 deletions integration/oneof-properties/oneof-properties-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { oneof as pbjs } from './pbjs';
import { PleaseChoose } from './oneof'

describe('oneof=properties (default)', () => {
it('generates types correctly', () => {
const alice: PleaseChoose = {
name: 'Alice',
age: 42,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: undefined,
bunchaBytes: undefined,
either: undefined,
or: undefined,
thirdOption: undefined,
};
const bob: PleaseChoose = {
name: 'Bob',
age: 42,
aNumber: 132,
aString: undefined,
aMessage: undefined,
aBool: undefined,
bunchaBytes: undefined,
either: undefined,
or: undefined,
thirdOption: undefined,
};
});

it('decode', () => {
let encoded = pbjs.PleaseChoose.encode(new pbjs.PleaseChoose({
name: 'Debbie',
aBool: true,
age: 37,
or: 'perhaps not',
})).finish();
let decoded = PleaseChoose.decode(encoded);
expect(decoded).toEqual({
name: 'Debbie',
age: 37,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: true,
bunchaBytes: undefined,
either: undefined,
or: 'perhaps not',
thirdOption: undefined,
});
});

it('encode', () => {
let encoded = PleaseChoose.encode({
name: 'Debbie',
age: 37,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: true,
bunchaBytes: undefined,
either: undefined,
or: 'perhaps not',
thirdOption: undefined,
}).finish();
let decoded = pbjs.PleaseChoose.decode(encoded);
expect(decoded).toEqual({
name: 'Debbie',
aBool: true,
age: 37,
or: 'perhaps not',
});
});

it('fromPartial', () => {
let empty = PleaseChoose.fromPartial({})
expect(empty).toEqual({
name: '',
age: 0,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: undefined,
either: undefined,
or: undefined,
thirdOption: undefined,
});

let partial = PleaseChoose.fromPartial({
name: 'Debbie',
aBool: true,
age: 37,
or: 'perhaps not',
});
expect(partial).toEqual({
name: 'Debbie',
age: 37,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: true,
either: undefined,
or: 'perhaps not',
thirdOption: undefined,
});
});

it('toJSON', () => {
let debbie: PleaseChoose = {
name: 'Debbie',
age: 37,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: true,
bunchaBytes: undefined,
either: undefined,
or: 'perhaps not',
thirdOption: undefined,
};
let pbjsJson = pbjs.PleaseChoose.decode(PleaseChoose.encode(debbie).finish()).toJSON();
let json = PleaseChoose.toJSON(debbie);
expect(json).toEqual(pbjsJson);
});

it('fromJSON', () => {
let empty = PleaseChoose.fromJSON({})
expect(empty).toEqual({
name: '',
age: 0,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: undefined,
either: undefined,
or: undefined,
thirdOption: undefined,
})

let debbie: PleaseChoose = {
name: 'Debbie',
age: 37,
aNumber: undefined,
aString: undefined,
aMessage: undefined,
aBool: true,
bunchaBytes: undefined,
either: undefined,
or: 'perhaps not',
thirdOption: undefined,
};
let pbjsJson = pbjs.PleaseChoose.decode(PleaseChoose.encode(debbie).finish()).toJSON();
let fromJson = PleaseChoose.fromJSON(pbjsJson)
expect(fromJson).toEqual(debbie)
});
});
Binary file added integration/oneof-properties/oneof.bin
Binary file not shown.
41 changes: 41 additions & 0 deletions integration/oneof-properties/oneof.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
syntax = "proto3";
package oneof;

message PleaseChoose {

string name = 1;

message Submessage {
string name = 1;
}

// Please to be choosing one of the fields within this oneof clause.
// This text exists to ensure we transpose comments correctly.
oneof choice {

// Use this if you want a number. Numbers are great. Who doesn't
// like them?
double a_number = 2;

// Use this if you want a string. Strings are also nice. Not as
// nice as numbers, but what are you going to do...
string a_string = 3;

Submessage a_message = 4;

// We also added a bool option! This was added after the 'age'
// field, so it has a higher number.
bool a_bool = 6;

bytes buncha_bytes = 10;
}

uint32 age = 5;

oneof either_or {
string either = 7;
string or = 8;
string third_option = 9;
}

}
Loading