1.1.1 • Published 2 years ago

protoc-gen-elm-fork v1.1.1

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
2 years ago

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. However, this plug-in itself does not implement or generate any Remote Procedure Call (RPC) logic.

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

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#, Objective C, 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 moduleThe .proto filename, e.g. proto/api.proto becomes module Proto.Api
doubleFloat0
floatFloat0
int32Int0
int64Int*0
uint32Int0
uint64Int*0
sint32Int0
sint64Int*0
fixed32Int0
fixed64Int*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
serviceN/A
reservedN/A
extensionsN/A

*) 64-bit integers are processed as 32-bit integers, see elm-protocol-buffers

**) Some default values can be overridden in proto2 specifications

Live Example

To run a live example in your browser, first start the example back-end server:

npm install
node example/server.js

The server implements a (basic) back-end for example/greeter.proto. You can now generate an Elm module from the same specification. The example code will use the generated example/src/Proto.Greeter.elm to communicate with the server. Start the reactor and give it a try on http://localhost:8000/src/Main.elm:

cd example
protoc --elm_out=src greeter.proto
elm reactor

Limitations

  • All limitations of elm-protocol-buffers apply;
  • This is still a beta release. Please report any issues you have generating your Elm modules;

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.

  • The plugin logic is written in Elm itself. To be executable via node, there is a index.js wrapper. It converts the incoming bytes to base64, because there currently is no way to directly send the type Bytes through a port.
  • Main.elm essentially wires up the binding to JS: A request is received through a port, gets decoded, processed and then sent through another port.
  • For decoding the protoc request, it uses "itself", meaning that upgrading protoc versions should be done by running the plugin against the new include files from protoc to generate the new encoders/decoders.
  • A Mapper converts the request into a convenient internal structure
  • A Generator then uses this internal structure to build an Elm AST which is then pretty-printed to a file.