src-d / proteus

Generate .proto files from Go source code.
https://blog.sourced.tech/post/proteus/
MIT License
735 stars 70 forks source link

enum values are not generated correctly when the first value is discarded #86

Closed smola closed 7 years ago

smola commented 7 years ago

It seems that if you define an enum using _ = iota, the values in Go will start at 1, while the generated proto will start at 0.

I couldn't really verify this yet. This is where I'm seeing divergences: https://github.com/bblfsh/sdk/blob/master/uast/uast.go https://github.com/bblfsh/sdk/blob/master/uast/generated.proto (Roles enum)

erizocosmico commented 7 years ago

As a workaround, you can do Foo = 1 + iota instead of using _ = iota.

smola commented 7 years ago

@erizocosmico :+1: thanks!

erizocosmico commented 7 years ago

@smola on a second thought, we can't do anything to solve that on the proteus side. Protobuf enums must start at 0 (https://developers.google.com/protocol-buffers/docs/proto3#enum) If you want the enums to match you'll have to start at 0.

Talk to @Serabe tomorrow IRL.

erizocosmico commented 7 years ago

@smola one thing you could do is naming the invalid value, instead of using _:

type Role uint64

const (
  Invalid Role = iota

  Foo
  Bar
  ...
)

It's done extensively in the stdlib, so I don't think is a completely bad solution (example: https://golang.org/src/go/ast/scope.go?s=3516:3532#L127).

The drawback of this would be that INVALID would be generated in the proto as well.

If that is not an OK solution for you guys, we'd have to look into options for respecting the mapping, but we can't promise there will be a solution for this, since enum values must start with 0 no matter what.

erizocosmico commented 7 years ago

Ping @smola

Serabe commented 7 years ago

Ping @smola

abeaumont commented 7 years ago

looks good to me