tinylib / msgp

A Go code generator for MessagePack / msgpack.org[Go]
MIT License
1.79k stars 191 forks source link

Ability to specify which methods are generated on a per struct basis #34

Closed myitcv closed 9 years ago

myitcv commented 9 years ago

e.g. in pseudo code:

//msgp:shim string as:[]byte using:[]byte/string

//msgp:tuple MyTuple generate:DecodeMsg

type MyTuple struct {
    A, B string
}

would cause only the DecodeMsg method to be generated (but not any of the marshall-related methods or the EncodeMsg)

The use case here is again MSGPACK RPC. I want to be able to generate a decoder for one struct (the args to the method) and an encoder for another (the return values). Then combine those two structs into a wrapper struct along the following lines:

//msgp:tuple MethodArgs generate:DecodeMsg

type MethodArgs struct {
  I int
}

//msgp:tuple MethodRetVals generate:EncodeMsg

type MethodRetVals struct {
  S string
}

type MethodWrapper struct {
  *MethodArgs
  *MethodRetVals
}

I can then unambiguously call DecodeMsg on an instance of the MethodWrapper

Thoughts?

philhofer commented 9 years ago

Well, here's a hack-y solution:

//go:generate msgp
//msgp:tuple MethodRetvals
//go:generate gorename -from "path/to/pkg".MethodRetVals.DecodeMsg -to deleteme
//go:generate gorename -from "path/to/pkg".MethodArgs.EncodeMsg - to deleteme

... and then delete methods named "deleteme" using the gross regex/tool of your choice. (I couldn't figure out a quick way to delete a method using gofmt or gorename, but I'm sure it's possible.)

myitcv commented 9 years ago

@philhofer - that certainly works as a solution (hadn't considered it to be honest!)