-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compiler flag oneof=unions generates type unions for oneof fields ins…
…tead of individual properties Addresses #74
- Loading branch information
Showing
15 changed files
with
1,121 additions
and
44 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,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 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,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; | ||
} | ||
|
||
} |
Oops, something went wrong.