Skip to content

Instantly share code, notes, and snippets.

@huangsam
Created November 3, 2024 09:33
Show Gist options
  • Save huangsam/154477e99f3d8e7b786bfe91e81766df to your computer and use it in GitHub Desktop.
Save huangsam/154477e99f3d8e7b786bfe91e81766df to your computer and use it in GitHub Desktop.
Protobuf madness with multiple languages
syntax = "proto3";
package hello;
option java_package = "com.tutorial.hello";
message Hello {
string message = 1;
}
import hello_pb2
import sys
def main():
hello = hello_pb2.Hello()
filename = sys.argv[1]
print(f"Parsing hello info from file with Python: {filename}")
with open(sys.argv[1], "rb") as f:
hello.ParseFromString(f.read())
print(f"Parsed hello info with following data:\n{hello}")
if __name__ == '__main__':
main()
package com.tutorial.hello;
import com.tutorial.hello.HelloOuterClass.Hello;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HelloWriter {
public static void main(String[] args) throws IOException {
Hello hello = Hello.newBuilder()
.setMessage("Hello world")
.build();
String filename = args[0];
System.out.println("Saving hello info to file with Java: " + filename);
try (FileOutputStream output = new FileOutputStream(filename)){
hello.writeTo(output);
}
System.out.println("Saved hello info with following data to disk:\n" + hello);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorial</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<sourceDirectory>.</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.28.3</version>
</dependency>
</dependencies>
</project>
protobuf==5.28.3
#!/bin/bash
protoc --java_out=. hello.proto
protoc --python_out=. hello.proto
mvn clean install
base="$HOME/.m2/repository"
my='1.0-SNAPSHOT'
my_jar="$base/com/tutorial/hello/$my/hello-$my.jar"
proto='4.28.3'
proto_jar="$base/com/google/protobuf/protobuf-java/$proto/protobuf-java-$proto.jar"
bin_file='hello.bin'
if [[ -f $bin_file ]]; then
echo "Remove old $bin_file output to enforce a deterministic outcome"
rm -f "$bin_file"
fi
java -cp "$my_jar:$proto_jar" com.tutorial.hello.HelloWriter "$bin_file"
python hello_read.py "$bin_file"
@huangsam
Copy link
Author

huangsam commented Nov 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment