feat!: proto3 JSON serializer and deserializer#2
Conversation
Release-As: v0.1.0
|
Warning: This pull request is touching the following templated files:
|
|
Here is the summary of possible violations 😱 DetailsThere is a possible violation for not having product prefix.
The end of the violation section. All the stuff below is FYI purposes only. Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
bcoe
left a comment
There was a problem hiding this comment.
If I'm understanding correctly, this will allow us to reduce the amount of nesting that users of GAPIC libraries need to use when interacting with complex APIs, like ai-platform.
Presumably this will eventually require a breaking change for libraries?
| "gts": "^3.1.0", | ||
| "jsdoc": "^3.6.7", | ||
| "jsdoc-fresh": "^1.1.0", | ||
| "jsdoc-region-tag": "^1.3.0", |
There was a problem hiding this comment.
Do we need jsdoc-region-tag for this library? It handles edge-cases in JSDoc parsing, e.g., snippet region tags.
There was a problem hiding this comment.
Not sure, I just copied it :)
There was a problem hiding this comment.
(but I do have one region tag in quickstart.js)
|
|
||
| function testGoogleProtobufStruct(root: protobuf.Root) { | ||
| const MessageWithStruct = root.lookupType('test.MessageWithStruct'); | ||
| const message = MessageWithStruct.fromObject({ |
There was a problem hiding this comment.
So, if I'm understanding, we're going to be able to represent these deeply nested structures much more elegantly for users, e.g., the parser turns:
structField: {
fields: {
stringField: {
stringValue: 'test',
},
numberField: {
numberValue: 42,
},
},
},
});Into:
const json = {
structField: {
stringField: 'test',
numberField: 42,
},
};
There was a problem hiding this comment.
That's right. We are not yet enabling this for libraries, for now we just need it for the transport layer in REGAPIC since the HTTP endpoint expects this short form rather than the long one, so gax should be able to read and write this short form JSON.
Enabling this for libraries is (1) breaking, (2) will require .d.ts updates to follow the short structure, so it's a separate story. But even now, aiplatform could start using this library instead of using their own conversion code.
🤖 I have created a release \*beep\* \*boop\* --- ## 0.1.0 (2021-08-03) ### ⚠ BREAKING CHANGES * proto3 JSON serializer and deserializer (#2) ### Features * proto3 JSON serializer and deserializer ([#2](https://www.github.com/googleapis/proto3-json-serializer-nodejs/issues/2)) ([96255a7](https://www.github.com/googleapis/proto3-json-serializer-nodejs/commit/96255a77c7714f33cae547db9160615d7f80a233)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
And this is the real code to be reviewed.
The implementation is in
typescript/src, tests are intypescript/test. The implementation of both the serializer and the deserializer is basically recursive, it's the regular traversing of the tree (the JSON tree for the deserializer, and the protobuf object for the serializer), and then I just have a separate pair of functions to serialize/deserialize any proto3 type that has a special JSON representation according to the spec linked below.README.mddescribes the idea and has all the samples :) I'll just copy the main part of it here.proto3 JSON serializer for TypeScript / JavaScript
This library implements proto3 JSON serialization and deserialization for protobuf.js protobuf objects according to the spec.
Note that the spec requires special representation of some
google.protobuf.*types (Value,Struct,Timestamp,Duration, etc.), so you cannot just use.toObject()since the result won't be understood by protobuf in other languages. Hence this module.JavaScript:
TypeScript:
Serialization: protobuf.js object to proto3 JSON
Serialization works with any object created by calling
.create(),.decode(), or.fromObject()for a loaded protobuf type. It relies on the$typefield so it will not work with a static object.Deserialization: proto3 JSON to protobuf.js object
To deserialize an object from proto3 JSON, we must know its type (as returned by
root.lookupType('...')). Pass this type as the first parameter to.fromProto3JSON:Complete example