gen

package module
v0.0.0-...-5c34657 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 19 Imported by: 0

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

View Source
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

func Run(srcPath string, outputPath string, mode Method, unexported bool) error

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) Alias

func (c *Array) Alias(typ string)

func (*Array) Complexity

func (a *Array) Complexity() int

Complexity returns a measure of the complexity of the element.

func (*Array) Copy

func (a *Array) Copy() Elem

Copy returns a deep copy of the object.

func (*Array) SetVarname

func (a *Array) SetVarname(s string)

SetVarname sets the name of the array and its index variable.

func (*Array) TypeName

func (a *Array) TypeName() string

TypeName returns the canonical Go type name.

func (*Array) Varname

func (c *Array) Varname() string

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 Ident

func Ident(id string) *BaseElem

Ident returns the *BaseElem that corresponds to the provided identity.

func (*BaseElem) Alias

func (s *BaseElem) Alias(typ string)

Alias sets an alias.

func (*BaseElem) BaseName

func (s *BaseElem) BaseName() string

BaseName returns the string form of the base type (e.g. Float64, Ident, etc)

func (*BaseElem) BaseType

func (s *BaseElem) BaseType() string

BaseType gives the name of the base type.

func (*BaseElem) Complexity

func (s *BaseElem) Complexity() int

Complexity returns a measure of the complexity of the element.

func (*BaseElem) Copy

func (s *BaseElem) Copy() Elem

Copy returns a deep copy of the object.

func (*BaseElem) FromBase

func (s *BaseElem) FromBase() string

FromBase is used as {{Varname}} = {{FromBase}}(tmp) if Convert==true.

func (*BaseElem) Needsref

func (s *BaseElem) Needsref(b bool)

Needsref indicates whether the base type is a pointer.

func (*BaseElem) Printable

func (s *BaseElem) Printable() bool

Printable says if the element is printable.

func (*BaseElem) Resolved

func (s *BaseElem) Resolved() bool

Resolved says whether or not the type of the element is a primitive or a builtin provided by the package.

func (*BaseElem) SetVarname

func (s *BaseElem) SetVarname(a string)

SetVarname sets the name of the variable.

func (*BaseElem) ToBase

func (s *BaseElem) ToBase() string

ToBase is used as tmp = {{ToBase}}({{Varname}}) if Convert==true.

func (*BaseElem) TypeName

func (s *BaseElem) TypeName() string

TypeName returns the canonical Go type name for the base element.

func (*BaseElem) Varname

func (c *BaseElem) Varname() string

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) Alias

func (c *Map) Alias(typ string)

func (*Map) Complexity

func (m *Map) Complexity() int

Complexity returns a measure of the complexity of the element.

func (*Map) Copy

func (m *Map) Copy() Elem

Copy returns a deep copy of the object.

func (*Map) SetVarname

func (m *Map) SetVarname(s string)

SetVarname sets the names of the map and the index variables.

func (*Map) TypeName

func (m *Map) TypeName() string

TypeName returns the canonical Go type name.

func (*Map) Varname

func (c *Map) Varname() string

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.

func (Method) String

func (m Method) String() string

String implements fmt.Stringer

type Ptr

type Ptr struct {
	Value Elem
	// contains filtered or unexported fields
}

Ptr represents a pointer.

func (*Ptr) Alias

func (c *Ptr) Alias(typ string)

func (*Ptr) Complexity

func (s *Ptr) Complexity() int

Complexity returns a measure of the complexity of the element.

func (*Ptr) Copy

func (s *Ptr) Copy() Elem

Copy returns a deep copy of the object.

func (*Ptr) NeedsInit

func (s *Ptr) NeedsInit() bool

NeedsInit says if the pointer needs to be checked if it should be newly allocated for use.

func (*Ptr) SetVarname

func (s *Ptr) SetVarname(n string)

SetVarname sets the name of the pointer variable.

func (*Ptr) TypeName

func (s *Ptr) TypeName() string

TypeName returns the canonical Go type name.

func (*Ptr) Varname

func (c *Ptr) Varname() string

type ShimMode

type ShimMode uint8

ShimMode determines whether the shim is a cast or a convert.

const (
	// Cast says to use the casting mode.
	Cast ShimMode = 0

	// Convert says to use the conversion mode.
	Convert ShimMode = 1
)

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) Alias

func (c *Slice) Alias(typ string)

func (*Slice) Complexity

func (s *Slice) Complexity() int

Complexity returns a measure of the complexity of the element.

func (*Slice) Copy

func (s *Slice) Copy() Elem

Copy returns a deep copy of the object.

func (*Slice) SetVarname

func (s *Slice) SetVarname(n string)

SetVarname sets the name of the slice and its index variable.

func (*Slice) TypeName

func (s *Slice) TypeName() string

TypeName returns the canonical Go type name.

func (*Slice) Varname

func (c *Slice) Varname() string

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) Alias

func (c *Struct) Alias(typ string)

func (*Struct) Complexity

func (s *Struct) Complexity() int

Complexity returns a measure of the complexity of the element.

func (*Struct) Copy

func (s *Struct) Copy() Elem

Copy returns a deep copy of the object.

func (*Struct) SetVarname

func (s *Struct) SetVarname(a string)

SetVarname sets the name of the struct variable.

func (*Struct) TypeName

func (s *Struct) TypeName() string

TypeName returns the canonical Go type name.

func (*Struct) Varname

func (c *Struct) Varname() string

type TransformPass

type TransformPass func(Elem) Elem

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL