Documentation ¶
Overview ¶
Package gen is the tool msgp uses to generate Go code for the types in your program that you want to serialize to and from the MessagePack format. This package is designed to be usable by both the main.go file at the root of this repository (installed as a command line tool that is called by the `go generate` command) and by external programs that import the package.
Documentation on how to use this tool either by the command line or from a Go program is at the wiki at https://github.com/dchenk/msgp/wiki.
To use this package from a Go program, call Run on a file or directory with the settings you want. Example:
import "github.com/dchenk/msgp/gen" err := gen.Run("path/to/my_file.go", gen.Size|gen.Marshal|gen.Unmarshal|gen.Test, false)
Index ¶
- Constants
- func Run(srcPath string, outputPath string, mode Method, unexported bool) error
- func RunData(srcPath string, mode Method, unexported bool) (mainBuf *bytes.Buffer, testsBuf *bytes.Buffer, err error)
- type Array
- type BaseElem
- func (s *BaseElem) Alias(typ string)
- func (s *BaseElem) BaseName() string
- func (s *BaseElem) BaseType() string
- func (s *BaseElem) Complexity() int
- func (s *BaseElem) Copy() Elem
- func (s *BaseElem) FromBase() string
- func (s *BaseElem) Needsref(b bool)
- func (s *BaseElem) Printable() bool
- func (s *BaseElem) Resolved() bool
- func (s *BaseElem) SetVarname(a string)
- func (s *BaseElem) ToBase() string
- func (s *BaseElem) TypeName() string
- func (c *BaseElem) Varname() string
- type Elem
- type Map
- type Method
- type Ptr
- type ShimMode
- type Slice
- type Struct
- type TransformPass
Constants ¶
const ( Invalid primitive = iota Bytes String Float32 Float64 Complex64 Complex128 Uint Uint8 Uint16 Uint32 Uint64 Byte Int Int8 Int16 Int32 Int64 Bool Intf // interface{} Time // time.Time Ext // extension IDENT // IDENT means an unrecognized identifier )
This list of primitive types is effectively the list of types currently having ReadXxxx and WriteXxxx methods.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run writes your desired methods and test files. You must set the source code path. The output file path can be left blank to have a file created at old_name_gen.go (_gen appended to the old name; the test file, if you opt to create one, will be at old_name_gen_test.go). The mode is the set of Method types and tests you would like. Set unexported to true if you want code to be generated for unexported as well as for exported types.
func RunData ¶
func RunData(srcPath string, mode Method, unexported bool) (mainBuf *bytes.Buffer, testsBuf *bytes.Buffer, err error)
RunData works just like Run except that, instead of writing out a file, it outputs the generated file's contents, the corresponding generated test file (nil if mode does not include gen.Test), and a possibly nil error.
Types ¶
type Array ¶
type Array struct { Index string // index variable name Size string // array size Els Elem // child // contains filtered or unexported fields }
Array represents an array.
func (*Array) Complexity ¶
Complexity returns a measure of the complexity of the element.
func (*Array) SetVarname ¶
SetVarname sets the name of the array and its index variable.
type BaseElem ¶
type BaseElem struct { ShimMode ShimMode // Method used to shim ShimToBase string // shim to base type, or empty ShimFromBase string // shim from base type, or empty Value primitive // Type of element Convert bool // should we do an explicit conversion? // contains filtered or unexported fields }
A BaseElem is an element that can be represented by a primitive MessagePack type.
func (*BaseElem) BaseName ¶
BaseName returns the string form of the base type (e.g. Float64, Ident, etc)
func (*BaseElem) Complexity ¶
Complexity returns a measure of the complexity of the element.
func (*BaseElem) Resolved ¶
Resolved says whether or not the type of the element is a primitive or a builtin provided by the package.
func (*BaseElem) SetVarname ¶
SetVarname sets the name of the variable.
type Elem ¶
type Elem interface { // SetVarname sets the node's variable name and recursively the names of // all its children. In general, this should only be called on the parent // of the tree. SetVarname(s string) // Varname returns the variable name of the element. Varname() string // TypeName is the canonical Go type name of the node, such as "string", // "int", "map[string]float64" OR the alias name, if it has been set. TypeName() string // Alias sets a type (alias) name. Alias(typ string) // Copy returns a deep copy of the object. Copy() Elem // Complexity returns a measure of the complexity of the element (greater // than or equal to 1). Complexity() int }
An Elem is a Go type capable of being serialized into MessagePack. It is implemented by *Ptr, *Struct, *Array, *Slice, *Map, and *BaseElem.
type Map ¶
type Map struct { KeyIndx string // key variable name ValIndx string // value variable name Value Elem // value element // contains filtered or unexported fields }
Map is a map[string]Elem.
func (*Map) Complexity ¶
Complexity returns a measure of the complexity of the element.
func (*Map) SetVarname ¶
SetVarname sets the names of the map and the index variables.
type Method ¶
type Method uint8
A Method is a bitfield representing something that the generator knows how to print.
const ( Decode Method = 1 << iota // Decode using msgp.Decoder Encode // Encode using msgp.Encoder Marshal // Marshal using msgp.Marshaler Unmarshal // Unmarshal using msgp.Unmarshaler Size // Size using msgp.Sizer Test // Test functions should be generated )
The following methods indicate for each pass what interfaces types should implement.
type Ptr ¶
type Ptr struct { Value Elem // contains filtered or unexported fields }
Ptr represents a pointer.
func (*Ptr) Complexity ¶
Complexity returns a measure of the complexity of the element.
func (*Ptr) NeedsInit ¶
NeedsInit says if the pointer needs to be checked if it should be newly allocated for use.
func (*Ptr) SetVarname ¶
SetVarname sets the name of the pointer variable.
type Slice ¶
type Slice struct { Index string Els Elem // The type of each element // contains filtered or unexported fields }
Slice represents a slice.
func (*Slice) Complexity ¶
Complexity returns a measure of the complexity of the element.
func (*Slice) SetVarname ¶
SetVarname sets the name of the slice and its index variable.
type Struct ¶
type Struct struct { Fields []structField // field list AsTuple bool // write as an array instead of a map // contains filtered or unexported fields }
Struct represents a struct.
func (*Struct) Complexity ¶
Complexity returns a measure of the complexity of the element.
func (*Struct) SetVarname ¶
SetVarname sets the name of the struct variable.
type TransformPass ¶
A TransformPass is a pass that transforms individual elements. If the returned is different from the argument, it should not point to the same objects.
func IgnoreTypename ¶
func IgnoreTypename(pattern string) TransformPass
IgnoreTypename is a pass that just ignores types of a given name.