johanbrandhorst / protobuf

GopherJS Bindings for ProtobufJS and gRPC-Web
MIT License
81 stars 16 forks source link

Add js tags to protobuf-generated types #39

Open idcmp opened 5 years ago

idcmp commented 5 years ago

In my use case, I'd like to have a model that is isomorphic and handlers against this model mutate the model. In a normal world, I'd have the model once in JavaScript with mutators written in JavaScript, then I'd have the model again in Go with Go-based mutators.

In a Protobuf/GopherJS world, I'd like to define my model in a proto, generate the appropriate code for the model, then ship an initalState from Go code to the JS-client as a protobuf-on-the-wire which is deserialized and kept as a JS model (used in this case as a React model). I'm quite happy with JSX and various JavaScript UI-isms, so I don't want to use Go for React completely.

When a change to the model is needed, I want the server to create an event that mutates the model, then send that event to the browser. The handler for that event on the JS client would make the same mutation (because it would a GopherJS compiled version of the Go code).

It's my understanding that to turn the Go object to a JS object, I would need to include a *js.Object and the fields would need to be tagged:

   Name string `js:"name"`

..it seems the protoc in gopherjs mode doesn't do this; am I missing something? Am I expected to use pairs of Marshal methods in Go/JS to shovel the protobuf-serialized bytes between the languages? That feels like it'd be much slower, but I'm not really sure what heavy-lifting happens in GopherJS otherwise?

johanbrandhorst commented 5 years ago

Hi!

Yeah, so the objects don't automatically include the *js.Object type because it's only relevant on the border between JS and GopherJS, whereas this project was designed with a pure-Go use case in mind. I'm not entirely sure I understand your use case, but I think you might just want to duplicate the relevant types? You might be able to create wrapper types that just compose the generated protobuf types?

johanbrandhorst commented 5 years ago

I should note as well that if you want to use JS and React in the frontend, this project is probably not as relevant to you as https://github.com/improbable-eng/grpc-web or https://github.com/grpc/grpc-web. All my examples and tutorials pretty much assume a pure-Go consumer.