Skip to content

Instantly share code, notes, and snippets.

@valexandersaulys
Last active July 10, 2024 14:33
Show Gist options
  • Select an option

  • Save valexandersaulys/0537982950c98c8fcb15f6bbebb3d40e to your computer and use it in GitHub Desktop.

Select an option

Save valexandersaulys/0537982950c98c8fcb15f6bbebb3d40e to your computer and use it in GitHub Desktop.
Protocol Buffers

Scattering of thoughts for my eyes (and maybe others?)

To install

eget protocolbuffers/protobuf
# dumps it out in the local directory

General Call Pattern

./protoc -I=$(pwd) --go_out=$(pwd) --js_out=myJsApp,import_style=commonjs,binary:. awesome.proto

File Layout

Needs to be setup with the following at top:

syntax = "proto3";
package awesomepackage;

Go Specifics

Requires an option `` to signifiy where to put it.

option go_package = "example/protobuf";

Install the following so it can parse Go, can't be installed globally (sad):

go get google.golang.org/protobuf/proto@latest

Then you can import and use:

package main

import (
	pb "example/protobuf"
	"fmt"
	"google.golang.org/protobuf/proto"
	"io/ioutil"
)

func main() {

	msg := &pb.AwesomeMessage{}
	msg.AwesomeField = "Hello World from Go!"
	fmt.Println(msg.AwesomeField)

	// dump this to disk
	// https://protobuf.dev/getting-started/gotutorial/#writing-a-message
	var fname string = "../output_protobuf.obj"
	out, err := proto.Marshal(msg)
	if err != nil {
		fmt.Println("Failed to encode msg:", err)
	}
	if err := ioutil.WriteFile(fname, out, 0644); err != nil {
		fmt.Println("Failed to write msg:", err)
	}
	
	// Read out the Javascript entry from file (see below)
	var readFilename string = "../output_protobuf_js.obj"
	in, err := ioutil.ReadFile(readFilename)
	if err != nil {
		fmt.Println("Error reading file:", err)
	}
	msgTwo := &pb.AwesomeMessage{}
	if err := proto.Unmarshal(in, msgTwo); err != nil {
		fmt.Println("Failed to parse address book:", err)
	}

	fmt.Println(msgTwo.AwesomeField)	
}

Javascript Specifics

Use npm to install everything, not bun. There's some issue with dependency finding that can probably be solved.

npm install -g protoc-gen-js

Note that even if this dumps out into subfolder myJsApp, you'll need to also npm init -y and npm install google-protobuf wthin that directory.

const { AwesomeMessage } = require("./awesome_pb.js");
let myObj = new AwesomeMessage();

myObj.getNameOfFieldInCamelCase();
// > "" 

myObj.setNameOfFieldInCamelCase("Named");
myObj.getNameOfFieldInCamelCase();
// > "Named"

Read serialized data off disk

const { AwesomeMessage } = require("./awesome_pb.js")
const binaryData = fs.readFileSync('../output_protobuf.obj');
let myObj = AwesomeMessage.deserializeBinary(binaryData)
console.log(myObj.getAwesomeString())
// > "Hello World from Go!"

Write serialized data to disk

let myObj = new AwesomeMessage()
myObj.setAwesomeField("Hello World from Node!")
fs.writeFileSync("../output_protobuf_js.obj", myObj.serializeBinary())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment