oskaritimperi / nimpb

Protocol Buffers for Nim
MIT License
38 stars 6 forks source link

Generate pure enums #1

Closed andreaferretti closed 6 years ago

andreaferretti commented 6 years ago

The enums generated by the generator are not pure. This creates a problem if a value of the enum is named the same as the name of the enum, except for casing. The simplest example is

syntax = "proto3";

package example;

enum Foo {
  FOO = 0;
  NOT_FOO = 1;
}

If you run the generator on this, you get the following nim file

# Generated by protoc_gen_nim. Do not edit!

import intsets

import protobuf/gen
import protobuf/stream
import protobuf/types

const
    FooDesc = EnumDesc(
        name: "Foo",
        values: @[
            EnumValueDesc(name: "FOO", number: 0),
            EnumValueDesc(name: "NOT_FOO", number: 1),
        ]
    )
generateEnumType(FooDesc)
generateEnumProcs(FooDesc)

This file will not compile because the name of the enum (Foo) conflicts with one of the values of the enum itself (FOO). It would be nice to have the possibility to generate pure enums, so that the value FOO would need to be scoped as Foo.FOO and no conflict would arise

oskaritimperi commented 6 years ago

Yeah I was thinking that it might be good idea to generate pure enums from the start because the language supports that. I was lazy so didn't do it yet but I'll definitely get it done at some point.

Thanks for reminding! :-)

andreaferretti commented 6 years ago

I wanted to try this, but the latest version of the generator does not compile for me, either on devel or on 0.18:

descriptor_pb.nim(344, 21) template/generic instantiation from here
gen.nim(431, 44) Error: type mismatch: got <Tag>
but expected one of: 
proc wiretype(field: NimNode): WireType
proc wiretype(ft: FieldType): WireType
oskaritimperi commented 6 years ago

It seems that I forgot to push some changes that I made yesterday. I'll push them later today.

oskaritimperi commented 6 years ago

Should work now.

andreaferretti commented 6 years ago

Confirmed, thank you!