Hi, I'm creating protobuf for Nim (both protocompiler and runtime library)

I'm trying to do as little as possible in protoc of the code generation, and do 
most of it by using Nims metaprogramming power.

Unfortunately, I want to reuse parts of the AST but I don't know how. See below 
work in progress what protoc currently produces:
    
    
    # Generated by the protocol buffer compiler.  DO NOT EDIT!
    # source: Msg.proto
    
    import protobuf
    
    type
      ItemRow* = object
        id*: int32
        quantity*: int32
        cost*: int32
    
    proc WriteTo*(message: ItemRow, output: var CodedOutputStream) =
      PbWriteTo(message, output):
        id: PbInt32 @1
        quantity: PbInt32 @2
        cost: PbInt32 @4
    
    proc MergeFrom*(message: var ItemRow, input: CodedInputStream) =
      PbMergeFrom(message, input):
        id: PbInt32 @1
        quantity: PbInt32 @2
        cost: PbInt32 @4
    
    

As you can see both procs call a macro, PbWriteTo() or PbMergeFrom(), which 
generate the actual implementation to write or read binary streams. The 
codeblock passed to these macro's is the same, so I would like to do something 
like:
    
    
    PbMessage ItemRow:
      id: PbInt32 @1
      quantity: PbInt32 @2
      cost: PbInt32 @4
    
    proc WriteTo*(message: ItemRow, output: var CodedOutputStream) =
      PbWriteTo(message, output)
    
    proc MergeFrom*(message: var ItemRow, input: CodedInputStream) =
      PbMergeFrom(message, input)
    

I know how to generate the actual type in the PbMessage() macro. (the Nim in 
Action book a great resource!) But the actual type does not store the field 
numbers. How can I temporarily save the AST (or codeblock) that is given to 
PbMessage() macro and reuse it in the PbWriteTo() and PbMergeFrom() macros?

I thought about generating the procs inside the PbMessage() macro, but then the 
generated code will be very cryptic to end users of protobuf-nim, and they 
won't be able to see the function prototypes of the WriteTo() and MergeFrom() 
procs.

Reply via email to