Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(diregapic): support int64 conversion between the pf message and JSON object #1028

Merged
merged 7 commits into from
Jun 17, 2021

Conversation

summer-ji-eng
Copy link
Contributor

Similar to #1015, support int64 conversion from protobuf message to JSON object.

protobufjs

Message.toObject(message: Message [, options: ConversionOptions]): Object
converts a message instance to an arbitrary plain JavaScript object for interoperability with other libraries or storage. The resulting plain JavaScript object might still satisfy the requirements of a valid message depending on the actual conversion options specified, but most of the time it does not.

var object = AwesomeMessage.toObject(message, {
  enums: String,  // enums as string names
  longs: String,  // longs as strings (requires long.js)
  bytes: String,  // bytes as base64 encoded strings
  defaults: true, // includes default values
  arrays: true,   // populates empty arrays (repeated fields) even if defaults=false
  objects: true,  // populates empty objects (map fields) even if defaults=false
  oneofs: true    // includes virtual oneof fields set to the present field's name
});

@summer-ji-eng summer-ji-eng requested a review from a team as a code owner June 15, 2021 07:21
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jun 15, 2021
decodedRequest
decodedRequest,
{
longs: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add enums: String here too? This will make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hesitated yesterday. I will add here.
BTW, I didn't test this lines. No need to test this request conversion, it covered by Message.toObject; This expect to replace by use toJSON soon.

test/fixtures/library.json Outdated Show resolved Hide resolved
author: 'book-author',
title: 'book-title',
read: true,
bookId: '9007199254740992',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be my ignorance of this testing framework, but is there a place where you set the book to be an int64 and see it encoded as a string? (Asking because all the bookIds here look like strings...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I removed the quote.
Book Message with value is

Book {
  name: 'book-name',
  author: 'book-author',
  title: 'book-title',
  read: true,
  bookId: Long { low: 0, high: 2097152, unsigned: false }
}

Convert to JSON object

{
  name: 'book-name',
  author: 'book-author',
  title: 'book-title',
  read: true,
  bookId: '9007199254740992'
}

@summer-ji-eng summer-ji-eng changed the title fix: support uint64 conversion between the pf message and JSON object fix: support int64 conversion between the pf message and JSON object Jun 15, 2021
@alexander-fenster
Copy link
Contributor

@summer-ji-eng I added one commit that adds a call to .fromObject() for the request (making possible to pass enums and big integers as strings into the request) and renamed a few variables to increase readability of the code.

@summer-ji-eng summer-ji-eng merged commit b46f57d into master Jun 17, 2021
@summer-ji-eng summer-ji-eng deleted the uint64_support branch June 17, 2021 22:50
gcf-merge-on-green bot pushed a commit that referenced this pull request Jun 17, 2021
gcf-merge-on-green bot pushed a commit that referenced this pull request Jun 24, 2021
Add unit test for enum conversion #1028
@summer-ji-eng summer-ji-eng changed the title fix: support int64 conversion between the pf message and JSON object fix: [diregapic]support int64 conversion between the pf message and JSON object Sep 9, 2021
@summer-ji-eng summer-ji-eng changed the title fix: [diregapic]support int64 conversion between the pf message and JSON object fix(diregapic): support int64 conversion between the pf message and JSON object Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants