Skip to content

Commit

Permalink
use PartialDeep for message encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tatethurston committed Jul 23, 2023
1 parent 4813142 commit 9885c4f
Show file tree
Hide file tree
Showing 56 changed files with 4,191 additions and 11,117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
coverage
node_modules
todo.txt
well-known-types/**/*.pb.ts
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
coverage
pnpm-lock.yaml
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## v0.0.15

- `encode` methods now accept partials for nested messages as well (`PartialDeep` instead of `Partial`). Previously, the types required that full messages were provided for any nested messages.

## v0.0.15

This release includes a number of bug fixes

- Fix treeshaking for nested messages. Previously, there were cases where protobuf did not tree shake out of JSON only client usage. Thanks @noahseger!
Expand Down
4 changes: 4 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ As part of installation, husky pre-commit hooks are installed to run linters aga
### Publishing

There are CI and publishing GitHub workflows in `./github/workflows`. These are named `ci.yml` and `publish.yml`.

### Note

`protoscript` package self referencing is used by the wellknowntypes. right now works in a surprising manner: the package is built into dist, but the source package.json has the same name as the package.json that will be generated into dist. This results in self referencing using the source directory when everything is built.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ console.log(json);
const userFromJSON = UserJSON.decode(json);
console.log(userFromJSON);

// ProtoScript generates and consumes plain JavaScript objects (POJOs) over classes. If you want to generate
// a full message with default fields, you can use the #initialize method on the generated message class:
// ProtoScript generates and consumes plain JavaScript objects (POJOs) over classes. If you want to generate a full message
// with default fields, you can use the #initialize method on the generated message class:
const user = User.initialize();
console.log(user);
```
Expand Down
10 changes: 7 additions & 3 deletions dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
"sideEffects": false,
"types": "./index.d.ts",
"dependencies": {
"google-protobuf": "^3.21.0",
"prettier": "^2.7.1"
"google-protobuf": "^3.21.2",
"prettier": "^3.0.0"
},
"keywords": ["protobuf", "protocol buffers", "typescript"],
"keywords": [
"protobuf",
"protocol buffers",
"typescript"
],
"exports": {
"./package.json": "./package.json",
".": {
Expand Down
54 changes: 27 additions & 27 deletions e2e/conformance/proto/conformance/conformance.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export const FailureSet = {
decode: function (bytes: ByteSource): FailureSet {
return FailureSet._readMessage(
FailureSet.initialize(),
new BinaryReader(bytes)
new BinaryReader(bytes),
);
},

Expand All @@ -326,7 +326,7 @@ export const FailureSet = {
*/
_writeMessage: function (
msg: Partial<FailureSet>,
writer: BinaryWriter
writer: BinaryWriter,
): BinaryWriter {
if (msg.failure?.length) {
writer.writeRepeatedString(1, msg.failure);
Expand Down Expand Up @@ -362,7 +362,7 @@ export const ConformanceRequest = {
encode: function (msg: Partial<ConformanceRequest>): Uint8Array {
return ConformanceRequest._writeMessage(
msg,
new BinaryWriter()
new BinaryWriter(),
).getResultBuffer();
},

Expand All @@ -372,7 +372,7 @@ export const ConformanceRequest = {
decode: function (bytes: ByteSource): ConformanceRequest {
return ConformanceRequest._readMessage(
ConformanceRequest.initialize(),
new BinaryReader(bytes)
new BinaryReader(bytes),
);
},

Expand All @@ -398,7 +398,7 @@ export const ConformanceRequest = {
*/
_writeMessage: function (
msg: Partial<ConformanceRequest>,
writer: BinaryWriter
writer: BinaryWriter,
): BinaryWriter {
if (msg.protobufPayload?.length) {
writer.writeBytes(1, msg.protobufPayload);
Expand Down Expand Up @@ -428,7 +428,7 @@ export const ConformanceRequest = {
writer.writeMessage(
6,
msg.jspbEncodingOptions,
JspbEncodingConfig._writeMessage
JspbEncodingConfig._writeMessage,
);
}
if (msg.printUnknownFields) {
Expand All @@ -442,7 +442,7 @@ export const ConformanceRequest = {
*/
_readMessage: function (
msg: ConformanceRequest,
reader: BinaryReader
reader: BinaryReader,
): ConformanceRequest {
while (reader.nextField()) {
const field = reader.getFieldNumber();
Expand Down Expand Up @@ -478,7 +478,7 @@ export const ConformanceRequest = {
case 6: {
reader.readMessage(
msg.jspbEncodingOptions,
JspbEncodingConfig._readMessage
JspbEncodingConfig._readMessage,
);
break;
}
Expand All @@ -503,7 +503,7 @@ export const ConformanceResponse = {
encode: function (msg: Partial<ConformanceResponse>): Uint8Array {
return ConformanceResponse._writeMessage(
msg,
new BinaryWriter()
new BinaryWriter(),
).getResultBuffer();
},

Expand All @@ -513,7 +513,7 @@ export const ConformanceResponse = {
decode: function (bytes: ByteSource): ConformanceResponse {
return ConformanceResponse._readMessage(
ConformanceResponse.initialize(),
new BinaryReader(bytes)
new BinaryReader(bytes),
);
},

Expand All @@ -539,7 +539,7 @@ export const ConformanceResponse = {
*/
_writeMessage: function (
msg: Partial<ConformanceResponse>,
writer: BinaryWriter
writer: BinaryWriter,
): BinaryWriter {
if (msg.parseError != undefined) {
writer.writeString(1, msg.parseError);
Expand Down Expand Up @@ -576,7 +576,7 @@ export const ConformanceResponse = {
*/
_readMessage: function (
msg: ConformanceResponse,
reader: BinaryReader
reader: BinaryReader,
): ConformanceResponse {
while (reader.nextField()) {
const field = reader.getFieldNumber();
Expand Down Expand Up @@ -634,7 +634,7 @@ export const JspbEncodingConfig = {
encode: function (msg: Partial<JspbEncodingConfig>): Uint8Array {
return JspbEncodingConfig._writeMessage(
msg,
new BinaryWriter()
new BinaryWriter(),
).getResultBuffer();
},

Expand All @@ -644,7 +644,7 @@ export const JspbEncodingConfig = {
decode: function (bytes: ByteSource): JspbEncodingConfig {
return JspbEncodingConfig._readMessage(
JspbEncodingConfig.initialize(),
new BinaryReader(bytes)
new BinaryReader(bytes),
);
},

Expand All @@ -662,7 +662,7 @@ export const JspbEncodingConfig = {
*/
_writeMessage: function (
msg: Partial<JspbEncodingConfig>,
writer: BinaryWriter
writer: BinaryWriter,
): BinaryWriter {
if (msg.useJspbArrayAnyFormat) {
writer.writeBool(1, msg.useJspbArrayAnyFormat);
Expand All @@ -675,7 +675,7 @@ export const JspbEncodingConfig = {
*/
_readMessage: function (
msg: JspbEncodingConfig,
reader: BinaryReader
reader: BinaryReader,
): JspbEncodingConfig {
while (reader.nextField()) {
const field = reader.getFieldNumber();
Expand Down Expand Up @@ -854,7 +854,7 @@ export const FailureSetJSON = {
decode: function (json: string): FailureSet {
return FailureSetJSON._readMessage(
FailureSetJSON.initialize(),
JSON.parse(json)
JSON.parse(json),
);
},

Expand Down Expand Up @@ -904,7 +904,7 @@ export const ConformanceRequestJSON = {
decode: function (json: string): ConformanceRequest {
return ConformanceRequestJSON._readMessage(
ConformanceRequestJSON.initialize(),
JSON.parse(json)
JSON.parse(json),
);
},

Expand All @@ -929,7 +929,7 @@ export const ConformanceRequestJSON = {
* @private
*/
_writeMessage: function (
msg: Partial<ConformanceRequest>
msg: Partial<ConformanceRequest>,
): Record<string, unknown> {
const json: Record<string, unknown> = {};
if (msg.protobufPayload?.length) {
Expand Down Expand Up @@ -958,7 +958,7 @@ export const ConformanceRequestJSON = {
}
if (msg.jspbEncodingOptions) {
const _jspbEncodingOptions_ = JspbEncodingConfigJSON._writeMessage(
msg.jspbEncodingOptions
msg.jspbEncodingOptions,
);
if (Object.keys(_jspbEncodingOptions_).length > 0) {
json["jspbEncodingOptions"] = _jspbEncodingOptions_;
Expand All @@ -975,7 +975,7 @@ export const ConformanceRequestJSON = {
*/
_readMessage: function (
msg: ConformanceRequest,
json: any
json: any,
): ConformanceRequest {
const _protobufPayload_ =
json["protobufPayload"] ?? json["protobuf_payload"];
Expand Down Expand Up @@ -1037,7 +1037,7 @@ export const ConformanceResponseJSON = {
decode: function (json: string): ConformanceResponse {
return ConformanceResponseJSON._readMessage(
ConformanceResponseJSON.initialize(),
JSON.parse(json)
JSON.parse(json),
);
},

Expand All @@ -1062,7 +1062,7 @@ export const ConformanceResponseJSON = {
* @private
*/
_writeMessage: function (
msg: Partial<ConformanceResponse>
msg: Partial<ConformanceResponse>,
): Record<string, unknown> {
const json: Record<string, unknown> = {};
if (msg.parseError != undefined) {
Expand Down Expand Up @@ -1100,7 +1100,7 @@ export const ConformanceResponseJSON = {
*/
_readMessage: function (
msg: ConformanceResponse,
json: any
json: any,
): ConformanceResponse {
const _parseError_ = json["parseError"] ?? json["parse_error"];
if (_parseError_) {
Expand Down Expand Up @@ -1157,7 +1157,7 @@ export const JspbEncodingConfigJSON = {
decode: function (json: string): JspbEncodingConfig {
return JspbEncodingConfigJSON._readMessage(
JspbEncodingConfigJSON.initialize(),
JSON.parse(json)
JSON.parse(json),
);
},

Expand All @@ -1174,7 +1174,7 @@ export const JspbEncodingConfigJSON = {
* @private
*/
_writeMessage: function (
msg: Partial<JspbEncodingConfig>
msg: Partial<JspbEncodingConfig>,
): Record<string, unknown> {
const json: Record<string, unknown> = {};
if (msg.useJspbArrayAnyFormat) {
Expand All @@ -1188,7 +1188,7 @@ export const JspbEncodingConfigJSON = {
*/
_readMessage: function (
msg: JspbEncodingConfig,
json: any
json: any,
): JspbEncodingConfig {
const _useJspbArrayAnyFormat_ =
json["useJspbArrayAnyFormat"] ?? json["use_jspb_array_any_format"];
Expand Down
Loading

0 comments on commit 9885c4f

Please sign in to comment.