planetscale / vtprotobuf

A Protocol Buffers compiler that generates optimized marshaling & unmarshaling Go code for ProtoBuf APIv2
BSD 3-Clause "New" or "Revised" License
865 stars 73 forks source link

Twirp Integration #13

Closed T-J-L closed 3 years ago

T-J-L commented 3 years ago

In response to "I actually have no idea of how to switch encoders in Twirp. Maybe it's not even possible.", this can't be done out of the box with the code generation, but can be achieved (server-side) with a simple find and replace.

The changes are below:

proto.Marshal(respContent) >> respContent.MarshalVT()
proto.Unmarshal(buf, reqContent) >> reqContent.UnmarshalVT(buf)

I'm using make to control all code gen, so I added the below as a final step:

for twirp in $${dir}/*.twirp.go; \
do \
  echo 'Updating' $${twirp}; \
  sed -i '' -e 's/respBytes, err := proto.Marshal(respContent)/respBytes, err := respContent.MarshalVT()/g' $${twirp}; \
  sed -i '' -e 's/if err = proto.Unmarshal(buf, reqContent); err != nil {/if err = reqContent.UnmarshalVT(buf); err != nil {/g' $${twirp}; \
done; \
T-J-L commented 3 years ago

See https://github.com/twitchtv/twirp/issues/327 for further discussion.

vmg commented 3 years ago

Thank you! I've updated the README with your example!