Skip to content

Commit

Permalink
Compiler flag oneof=unions generates type unions for oneof fields ins…
Browse files Browse the repository at this point in the history
…tead of individual properties (#95)

Addresses #74
  • Loading branch information
philikon authored Jul 11, 2020
1 parent d8e4230 commit ce3ca37
Show file tree
Hide file tree
Showing 15 changed files with 1,121 additions and 44 deletions.
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

0 comments on commit ce3ca37

Please sign in to comment.