Home

Awesome

Elm Plugin for Protocol Buffers build status

This protoc plug-in generates Elm modules from .proto specification files. The generated modules make use of the elm-protocol-buffers library to handle the (de)serialization. They can be used to transmit bytes over HTTP(S) or via web-sockets.

Remote Procedure Call (RPC) generation is supported and possible to disable with --elm_opt=grpc=false. If used, you need to add a dependency on the elm-grpc library.

Take a look here for a general introduction on Protocol Buffers.

Installation

This package is a plug-in for protoc, make sure you have installed it and protoc is available on your path. After installing protoc-gen-elm globally from NPM, protoc will automatically find the binary when you add the --elm_out flag to your command.

npm install --global protoc-gen-elm

Alternatively, you can add protoc as a dev-dependency to your project. This should be the preferred way if you want to build your project in CI. If you wrap the call to protoc in some npm script, it should still work as expected.

npm install --save-dev protoc-gen-elm

You can now turn any .proto file into an Elm module. A similar approach can be used to generate code for C++, Dart, Go, Java, Python, Ruby, C#, Rust, JavaScript, PHP or another language to build a compliant back-end server!

protoc --elm_out=. api.proto

Overview

The following table gives an overview of how .proto types correspond to Elm types and what their default values are.

.proto typeElm typeDefault value**
packageThe name of the moduleProto
doubleFloat0
floatFloat0
int32Int0
int64Int64*0
uint32Int0
uint64Int64*0
sint32Int0
sint64Int64*0
fixed32Int0
fixed64Int64*0
boolBoolFalse
stringString""
bytesBytes.BytesEmpty bytes sequence
required aaNo default
optional aaDefault of a
repeated aList a[]
enumCustom typeFirst element
messageRecordAll fields take their default value
aMaybe RecordNothing
oneofCustom type with an associated dataNothing
map<k, v>Dict.Dict k vDict.empty
serviceGrpc.Rpc req res***No default
reservedN/A
extensionsN/A

*) 64-bit integers are defined in elm-protocol-buffers in Protobuf.Types.Int64.

**) Some default values can be overridden in proto2 specifications. This is currently not supported.

***) Rpc is implemented via the elm-grpc library.

Json Encoding

Protocol Buffers specify a canonical json encoding. When you pass the additional option --elm_opt=json to the protoc invocation, you will get JSON Encoders and Decoders generated. If you want to be more granular --elm_opt=json=encode or --elm_opt=json=decode will only generate one or the other.

The canonical json encoding has quite a few special cases. Below is a list which ones have been implemented so far.

Grpc Dev Tools

This plugin supports code generation for the grpc-dev-tools, which should primarily be used as debugging information for local development. Since it adds code bloat to your bundle and comes with a performance overhead, you should make sure it stays out of your production bundle.

To enable the extra code generation, enable the grpcDevTools flag by adding --elm_opt=grpcDevTools to your protoc invocation. This will generate additional JsonEncoders for your data types according to the canonical json encoding, a Proto/DevToolsWorker.elm file and a Proto/dev-tools.mjs file.

The dev-tools.mjs file internally imports the Elm file, so assuming you are using some sort of bundler with Elm support (like vite or webpack), all you need to do is import the file at the top of your html file or your main JS bundle, e.g.

<body>
  <script type="module" src="/generated/Proto/dev-tools.mjs"></script>
  ...
</body>

You can see a vite setup example in the /example directory, which also makes sure it does not get added in the production bundle.

Explanations about the generated code

In general, the generated code tries to be close to what the code looks like in other languages while still being ideomatic Elm code. Elm's concept of "Only one solution to solve" a problem has several consequences here.

General

Module Nesting

Protobufs have their own module system, which is different from Elms. Here are some interesting points about it:

Elm disallows circular imports as well, luckily protoc helps us out here on the package front. However, Elm does not have supported for nested modules, which is a problem.

For an illustration why, see the following example

// file: test.proto
package test;

enum Outer { A = 0 }

message Scope {
  enum Inner { B = 0 }
  message InnerMsg {
    Outer outer = 1;
    Inner inner = 2;
  }

  InnerMsg msg = 1;
}

Obviously there are two modules here: test and test.Scope. We generate two Elm files:

// file: Test.elm
import Test.Scope

type Outer = A

type alias Scope = {
  inner : Test.Scope.InnerMsg
}
// file: Test/Scope.elm
import Test

type Inner = B

type alias InnerMsg = {
  outer : Test.Outer,
  inner : Inner
}

This might look fine on first glance, but if we try to compile this we get a compile error. Why? Because the two modules are mutually recursive.

The only solution to this problem is making a large module for each package, so this is exactly what we do. But if we want to keep the nice, short names, we will get name conflicts. Protoc has no problems with identical names as long as they are in different scopes.

Therefore, we hide the large modules as .Internals_.elm modules, which you should not need to use and re-export from other modules with nicer names from there. The only downside: We lose the ability to pattern match on types, since we can not alias constructors. To not have to generate mapping functions (our solution from v3.x), we instead generate generic union types and apply them in the .Internals_.elm module.

Recursive Data Types

For ease of construction, protoc-gen-elm prefers to generate type aliases instead of nominal types. Type aliases have one downside though: they cannot be recursive. Otherwise, the Elm compiler would have to do infinite work to expand the type. So if you have a recursive type like this:

message Rec {
  repeated Rec rec = 1;
}

we generate

type alias Rec = { rec : List Rec_ }

type Rec_ = Rec_ Rec

and corresponding wrapRec and unwrapRec functions.

gRPC

If your .proto file includes a service declaration, an Elm module will be generated based on package and the services name.

This file:

package some_package

service SomeService {}

will generate a Proto/SomePackage/SomeService.elm module.

The code that needs to be generated inside is actually rather small. A gRPC call just needs

The rest of the work is done by the elm-grpc package. It provides functions to convert the generated Grpc.Rpc instances into Cmds and Tasks, as well as setting the usual Http Request fields (headers, timeout, tracker etc.)

Live Example

To run a minimal live example in your browser, follow the instructions in /example/grpc/README.md. For a more advanced/realistic example, look at /example/tonic_vite/README.md.

Well-known types

If you want to use protobufs well-known-types, you need to install the pre-built package elm-protoc-types or include the paths to the proto files in the compilation.

Example: If this is your proto file test.proto which uses the well-known type Timestamp,

import "google/protobuf/timestamp.proto";

message TestMessage {
  google.protobuf.Timestamp timestamp = 1;
}

the protoc invocation will need to include the path to the well-known types .proto file.

protoc --elm_out=. test.proto /usr/local/include/google/protobuf/timestamp.proto

Limitations

Development

Note: Currently, this project won't run on Windows (WSL works) because of shell scripts/executable js files.

Execute npm install, npm run build and npm test and you should be good to go. You will need protoc installed and on your PATH.

Run build.sh to build the elm code into index.min.js (which is imported by the entrypoint index.js).

To analyse the protoc requests, there are debug.js, DebugMain and build_debug.sh files. Run build_debug.sh, then use debug.js in place of index.js when running protoc. This should dump the deserialized request into debug.log. You can then put this into the Elm repl for example or use it as input for tests.