elm-protobuf icon indicating copy to clipboard operation
elm-protobuf copied to clipboard

Add support for recursively defined message structures.

Open EdSchouten opened this issue 5 years ago • 3 comments

I'm trying to use this library in combination with a relatively complex protocol, namely the remote execution protocol of the Bazel build sytem:

https://github.com/bazelbuild/remote-apis/blob/master/build/bazel/remote/execution/v2/remote_execution.proto

Some of the dependencies of this protocol contain recursively defined messages. These currently cause the compiler to emit Elm code that isn't valid, due to recursive alias types.

Solve this by adding a "%sMessage" custom type for every message. This causes the recursion to be broken. Let all nested messages use this type and fix up the encode/decode functions to properly destruct/pack messages into the custom type.

EdSchouten avatar Apr 07 '19 21:04 EdSchouten

Here are the messages that contain recursive declarations:

https://github.com/googleapis/googleapis/blob/master/google/api/http.proto <- HttpRule https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto <- DescriptorProto

EdSchouten avatar Apr 09 '19 08:04 EdSchouten

(Friendly ping?) :-)

EdSchouten avatar Apr 26 '19 20:04 EdSchouten

Thanks for the PR! I was wondering, would it make sense to just avoid the type alias, and define a new (inlined) type for each message?

e.g.

current:

type alias Bar =
    { field : Bool -- 1
    }
type BarMessage = BarMessage Bar

proposed:

type Bar = Bar
    { field : Bool -- 1
    }

Then we only use the new type everywhere, even though it requires always explicitly referring to the constructor (both for creating new instances, and also for pattern matching).

WDYT?

tiziano88 avatar May 07 '19 13:05 tiziano88