In this post I will show how to use Bazel with Protocol buffers to share types between Typescript, Java and C++ in the same application.
Bazel is a language agnostic build system. This means Bazel can build your entire tech stack, even if it’s made up of very different programming languages.
Most mainstream programming languages are already supported by Bazel.
In my demo application I currently have code written in Typescript (Angular + NodeJS), Java and C++.
One typical challenge when working in different languages is that it can be difficult to synchronize types across language boundaries.
A typical example is frontend written in Typescript and a backend written in Java. If the shape of the objects returned from Java fall out of sync with the client code, the application will fail at runtime.
I have seen this happen numerous times. If you catch it too late, you have to scramble to fix bugs in production as a result.
Wouldn’t it be nice if we could catch this at compile time instead?
It turns out we can use Protocol Buffers to address this issue.
You can think of Protocol Buffers as a language neutral standard for defining schemas. Even though the syntax is different, a protocol buffer is similar to defining schemas in json or xml.
Below is an example of a Protocol buffer.
The idea is that you define “messages” that make up the schema for the object you are describing. In this sample I have created a person object with two properties; name and address. The number on the right is just a running sequential number used during serialization.
Notice the singular Person message vs the plural Persons message. Person defines individual objects with a name and address property. Persons defines an object with an array or Person objects. The array is returned by the persons property.
Now that we have the schema, how can we use it to synchronize types across multiple languages.
The answer is code generation. We can’t use a Protocal buffer directly in our code, but we can include Bazel rules that will generate code for supported languages.
In the following code listing I will show Bazel rules for Typescript, Java and C++:
The first rule, proto_library, is the base rule that points to the file (person.proto) where the schema is defined.
Next we have language specific rules that depend on proto_library. The result of running those rules is generated language specific code. In this case C++, Java and Typescript objects are created.
Since the objects are generated from the same schema, we can trust that the definitions are kept in sync.
The language specific code is actually a bit more than just plain old objects. The generated code comes with a builder api that can be used to create objects.
Below I have included examples in Java and C++:
In my demo application the Java and C++ code return objects that are consumed by the Typescript frontend.
The Typescript proto rule generates IPerson and IPersons interfaces that can be used to make sure the client and backend are in sync.
If the objects fall out of sync, you will get a compile time error instead of a runtime error.
See example below:
If you are interested in trying this yourself, just download my project from Github.
In a future article I will discuss how to combine this with gRPC