Goten Protobuf Compilers
Protobuf was developed by Google, and it has been implemented in many
languages, including Golang. Each supported language provides a protoc
compiler. For Golang, there exists protoc-gen-go, which takes protobuf
files and generates Golang files. This tool however is massively
insufficient compared to what we need in Goten: We have custom types,
extra functionality, and a full-blown framework generating almost all
the server code. We developed our protoc compilers which replace standard
protoc-gen-go. We have many protoc compilers, see the cmd
directory:
-
protoc-gen-goten-go
This is the main replacement of the standard
protoc-gen-go
. It generates all the base go files you can see throughout theresources/
andclient/
modules, typically. All the generated files ending with.pb.go
. -
protoc-gen-goten-client
It compiles some files in the
client
directory, except those ending with.pb.go
, which contain basic types. -
protoc-gen-goten-server
It generates those middleware and default core files under the
server
directory. -
protoc-gen-goten-controller
It generates controller packages, as described in the developer guide.
-
protoc-gen-goten-store
It generates files under the
store
directory. -
protoc-gen-goten-access
It compiles files in the
access
directory. -
protoc-gen-goten-resource
It focuses on protobuf objects annotated as resources but does not generate anything for “under” resources. It produces most of the files in the
resources
directory for each service. This includespb.access.go
,pb.collections.go
,pb.descriptor.go
,pb.filter.go
,pb.filterbuilder.go
,pb.name.go
,pb.namebuilder.go
,pb.pagination.go
,pb.query.go
,pb.view.go
,pb.change.go
. -
protoc-gen-goten-object
It provides additional optional types over protoc-gen-goten-go, those types are FieldPath, FieldMask, additional methods for merging, cloning, and diffing objects. You can see them in files ending with
pb.fieldmask.go
,pb.fieldpath.go
,pb.fieldpathbuider.go
,pb.object_ext.go
. This is done for resources or sub-objects used by resources. For example, in the goten repository, you can see files from this protoc compiler undertypes/meta/
directory. -
protoc-gen-goten-cli
It compiles files in the
cli
directory. -
protoc-gen-goten-validate
It generates
pb.validate.go
files you can typically find in theresources
directory, but it’s not necessarily limited there. -
protoc-gen-goten-versioning
It generates all versioning transformers under the
versioning
directory. -
protoc-gen-goten-doc
It generates markdown documentation files based on proto files (often docs directory).
-
protoc-gen-goten-jsonschema
It is a separate compiler for parsing
bootstrap.proto
into API skeleton JSON schema.
Depending on which files you want to be generated differently, or which you want to study, you need to start with relevant compiler.
Pretty much any compiler in the cmd
directory maps to some module
in the compiler
directory (there are exceptions like the ast
package!). For example:
- cmd/protoc-gen-goten-go maps to compiler/gengo.
- cmd/protoc-gen-goten-client maps to compiler/client.
Each of these compilers takes a set of protobuf files as the input. When you see some bash code like:
protoc \
-I "${PROTOINCLUDE}" \
"--goten-go_out=:${GOGENPATH}" \
"--goten-validate_out=${GOGENPATH}" \
"--goten-object_out=:${GOGENPATH}" \
"--goten-resource_out=:${GOGENPATH}" \
"--goten-access_out=:${GOGENPATH}" \
"--goten-cli_out=${GOGENPATH}" \
"--goten-versioning_out=:${GOGENPATH}" \
"--goten-store_out=datastore=firestore:${GOGENPATH}" \
"--goten-server_out=lang=:${GOGENPATH}" \
"--goten-client_out=:${GOGENPATH}" \
"--goten-doc_out=service=Meta:${SERVICEPATH}/docs/apis" \
"${SERVICEPATH}"/proto/v1/*.proto
It simply means we are calling many of those protoc utilities. In
the flag we pass proto include paths, so protos can be parsed correctly
and linked to others. In this shell, in the last line, we are passing
all files for which we want code to be generated. In this case, it is
all files we can find in the ${SERVICEPATH}"/proto/v1
directory.