-
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.
feat: add option
noDefaultsForOptionals
(#1051)
Adds a new option - `noDefaultsForOptionals` that allows for compatibility with [Square/Block's Wire Protobuf implementation](https://square.github.io/wire/) with respect to defaulting of optional fields. Note: As per the documentation added to the readme, this option provides non-standard protobuf behaviour so it would be understandable if this isn't something you'd like in the library. I'm hoping with the appropriate warning in the docs that you'll consider this as as option.
- Loading branch information
Showing
21 changed files
with
1,262 additions
and
13 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
48 changes: 48 additions & 0 deletions
48
integration/no-defaults-for-optionals-with-nulls/no-defaults-test.ts
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,48 @@ | ||
import { Proto2TestMessage } from './proto-2'; | ||
|
||
describe('proto2', () => { | ||
it('preserves null values on optional fields', () => { | ||
const message = Proto2TestMessage.fromJSON({ | ||
intValue: null, | ||
stringValue: null, | ||
boolValue: null, | ||
mapValue: {}, | ||
}); | ||
|
||
const data = Proto2TestMessage.encode(message).finish(); | ||
const result = Proto2TestMessage.decode(data); | ||
|
||
expect(result).toEqual(message); | ||
}); | ||
|
||
it('encodes/decodes non-null values on optional fields', () => { | ||
const message = Proto2TestMessage.fromJSON({ | ||
intValue: 100, | ||
stringValue: 'string', | ||
boolValue: true, | ||
mapValue: { | ||
v1:'1', | ||
v2:'2', | ||
} | ||
}); | ||
|
||
const data = Proto2TestMessage.encode(message).finish(); | ||
const result = Proto2TestMessage.decode(data); | ||
|
||
expect(result).toEqual(message); | ||
}); | ||
|
||
it('encodes/decodes non-null values set to standard protobuf defaults', () => { | ||
const message = Proto2TestMessage.fromJSON({ | ||
intValue: 0, | ||
stringValue: '', | ||
boolValue: false, | ||
mapValue: {}, | ||
}); | ||
|
||
const data = Proto2TestMessage.encode(message).finish(); | ||
const result = Proto2TestMessage.decode(data); | ||
|
||
expect(result).toEqual(message); | ||
}); | ||
}) |
1 change: 1 addition & 0 deletions
1
integration/no-defaults-for-optionals-with-nulls/parameters.txt
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 @@ | ||
noDefaultsForOptionals=true,useNullAsOptional=true |
Binary file not shown.
11 changes: 11 additions & 0 deletions
11
integration/no-defaults-for-optionals-with-nulls/proto-2.proto
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,11 @@ | ||
syntax = "proto2"; | ||
|
||
package omit; | ||
|
||
message Proto2TestMessage { | ||
optional bool boolValue = 1; | ||
optional int32 intValue = 2; | ||
optional string stringValue = 3; | ||
|
||
map<string, string> mapValue = 4; | ||
} |
238 changes: 238 additions & 0 deletions
238
integration/no-defaults-for-optionals-with-nulls/proto-2.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
15 changes: 15 additions & 0 deletions
15
integration/no-defaults-for-optionals-with-nulls/proto-3.proto
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,15 @@ | ||
syntax = "proto3"; | ||
|
||
package omit; | ||
|
||
message Proto3TestMessage { | ||
bool boolValue = 1; | ||
int32 intValue = 2; | ||
string stringValue = 3; | ||
|
||
optional bool optionalBoolValue = 4; | ||
optional int32 optionalIntValue = 5; | ||
optional string optionalStringValue = 6; | ||
|
||
map<string, string> mapValue = 7; | ||
} |
Oops, something went wrong.