-
Notifications
You must be signed in to change notification settings - Fork 194
Preprocessor Directives
You can make tweaks to how the code generator generates methods using directives in your source code comments. All directives take the form
//msgp:directive [arg1] [arg2] [arg3]...
much like the gc
compiler directives.
There are currently three supported directives.
//msgp:ignore Type1 Type2 Type3
Ignore tells the code generator not to generate methods for the list of types supplied.
//msgp:tuple TypeA
type TypeA struct {
Left float64
Right float64
}
The msgp:tuple
directive tells the generator to generate code for the struct so that it is serialized as an array instead of a map. In other words, TypeA{1.0, 2.0}
above would be encoded as
[1.0,2.0]
instead of
{"Left":1.0,"Right":2.0}
For smaller objects, tuple encoding can yield serious performance improvements.
//msgp:shim Enum as:string using:(Enum).String/parseString
type Enum byte
const(
A Enum = iota
B
C
D
invalid
)
func (e Enum) String() string {
switch e {
case A:
return "A"
case B:
return "B"
case C:
return "C"
case D:
return "D"
default:
return "<invalid>"
}
}
func parseString(s string) Enum {
switch s {
case "A":
return A
case "B":
return B
case "C":
return C
case "D":
return D
default:
return invalid
}
}
The shim
directive lets you inline a type-conversion function for a user-defined type in order to have it encode and decode differently than the default for its concrete type. In the example above, we're using the shim
directive to translate a const-iota
block into strings for encoding and decoding. Note that the as:
argument must take a "base" type (a built-in, []byte
, interface{}
, msgp.Extension
or a type already processed by the code generator.)