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

[JS] Add fromObject method to take an AsObject type and return a T. #11

Open
alexandros-megas opened this issue Mar 8, 2022 · 4 comments
Assignees
Labels
bug Something isn't working javascript triaged Issue has been triaged

Comments

@alexandros-megas
Copy link

What language does this apply to?
JS/TS

Describe the problem you are trying to solve.
In JS, many different libraries expect to work with dictionaries, basically Record<A, B> or lists of these, to do things like state management in single page apps, list operations like find, slice, zip, etc. and more. Having the ability to freely move between a Message and a JSON object would make it MUCH easier to interoperate with different tools.

Currently, my message types get compiled to something like the following:

export class Polygon extends jspb.Message {
  // getters and setters here

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): Polygon.AsObject;
  static toObject(includeInstance: boolean, msg: Polygon): Polygon.AsObject;
  static serializeBinaryToWriter(message: Polygon, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): Polygon;
  static deserializeBinaryFromReader(message: Polygon, reader: jspb.BinaryReader): Polygon;
}

Describe the solution you'd like

  static fromObject(from: Polygon.AsObject): Polygon;

Describe alternatives you've considered
Writing a generic function to parse the field descriptors, but I don't know enough to make it work correctly, PLUS, it seems like a much better thing to be provided out of the box.

Additional context

@alexandros-megas
Copy link
Author

alexandros-megas commented Mar 10, 2022

Maybe the proposed solution would be something like

  static fromObject(from: Partial<Polygon.AsObject>): Polygon;

This way, you can construct a partial message.

@elharo elharo added javascript enhancement New feature or request labels Mar 10, 2022
@acozzette acozzette transferred this issue from protocolbuffers/protobuf May 16, 2022
@dibenede dibenede added bug Something isn't working triaged Issue has been triaged and removed enhancement New feature or request labels Aug 12, 2022
@dibenede
Copy link
Contributor

We should fix this. #6 should help with this.

@infodusha
Copy link

infodusha commented Jan 23, 2023

For now I have built https://github.com/infodusha/grpc-web-from-object
Just in case you may want this in runtime

@bomsbro
Copy link

bomsbro commented Apr 27, 2023

how is it going on? i have same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working javascript triaged Issue has been triaged
Projects
None yet
Development

No branches or pull requests

6 participants