oskaritimperi / nimpb

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

enum error #19

Closed geohuz closed 3 years ago

geohuz commented 3 years ago

with the proto spec:

type
    TOPIC* {.pure.} = enum
        UNKNOWN = 0
        PARSER = 1
        CONNECTION = 2
        AUTH = 3
        EVENT = 4
        RECORD = 5
        RPC = 6
        PRESENCE = 7
        MONITORING = 8
        CLUSTER = 9
        LOCK = 10
        STATE_REGISTRY = 11
        ERROR = 100
    DPSMessage* = ref DPSMessageObj
    DPSMessageObj* = object of Message
        topic: TOPIC
        message: seq[byte]

The compiler generate the following errors:

/Users/geohuz/Coding/nimlearn/deepstream.io-protobuf/proto_files/general_pb.nim(120, 33) template/generic instantiation of `toJson` from here
/Users/geohuz/.nimble/pkgs/nimpb-0.2.0/nimpb/json.nim(42, 18) template/generic instantiation of `items` from here
/Users/geohuz/.choosenim/toolchains/nim-1.4.8/lib/system/iterators.nim(87, 19) template/generic instantiation of `..` from here
/Users/geohuz/.choosenim/toolchains/nim-1.4.8/lib/system/iterators_1.nim(85, 12) Error: type mismatch: got <TOPIC>
but expected one of: 
proc inc[T: Ordinal](x: var T; y = 1)
  first type mismatch at position: 1
  required type for x: var T: Ordinal
  but expression 'res' is of type: TOPIC

expression: inc(res)
geohuz commented 3 years ago

It can be caused by change in https://github.com/nim-lang/Nim/issues/14001, the following change as suggested:

proc toJson*[Enum: enum](value: Enum): JsonNode =
    for v in Enum.low.int..Enum.high.int:
        if ord(value) == v:
            return %($v)
    # The enum has a value that is not defined in the enum type
    result = %(cast[int](value))