ccg

package module
v0.0.0-...-d084e9d Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2016 License: Apache-2.0 Imports: 14 Imported by: 0

README

Introduction

ccg is a template-based code generation tool for golang.

Install

go get github.com/reusee/ccg/cmd/ccg

Example 0: parameterized types

First define the type

package pair

type T1 interface{}
type T2 interface{}

type Pair struct {
 first  T1
 second T2
}

func New(first T1, second T2) Pair {
 return Pair{first, second}
}

func (p Pair) First() T1 {
 return p.first
}

func (p Pair) Second() T2 {
 return p.second
}

Then put this package to a importable path, assuming $GOPATH/src/pair

Now invoke the ccg command to get type specialized codes

 ccg -f pair -t T1=int,T2=string -r Pair=IntStrPair,New=NewIntStrPair

The above command generates:

type IntStrPair struct {
 first  int
 second string
}

func NewIntStrPair(first int, second string) IntStrPair {
 return IntStrPair{first, second}
}

func (p IntStrPair) First() int {
 return p.first
}

func (p IntStrPair) Second() string {
 return p.second
}

Type T1 and T2 are substituted by int and string. Pair and New are also renamed.

Example 1: output to file / update existing file

Use option -o to write generated codes to a file instead of stdout.

 ccg -f pair -t T1=int,T2=string -r Pair=IntStrPair,New=NewIntStrPair -o foo.go

If the specified file is already exists, ccg will update declarations if they're present in that file, or append to if not. Other non-generated declarations will be preserved.

This means after updating template codes, you can re-invoke the command to update generated codes. So it's friendly to go generate

//go:generate ccg -f pair -t T1=int,T2=string -r Pair=IntStrPair,New=NewIntStrPair -o foo.go

Example 2: partial generation

By default, ccg will generate all declarations from template package (except params). If this is not what you want, you can use -u option to specify what to generate

 ccg -f pair -t T1=int,T2=string -r Pair=IntStrPair,New=NewIntStrPair -u NewIntStrPair,IntStrPair.First

The above command generates:

type IntStrPair struct {
  first  int
  second string
}

func NewIntStrPair(first int, second string) IntStrPair {
  return IntStrPair{first, second}
}

func (p IntStrPair) First() int {
  return p.first
}

Method Second is not generated. And type IntStrPair is automatically generated because it's depended by NewIntStrPair and First method.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(config Config) (ret error)

Types

type AstDecls

type AstDecls []ast.Decl

func (AstDecls) Filter

func (s AstDecls) Filter(filter func(ast.Decl) bool) (ret AstDecls)

type AstSpecs

type AstSpecs []ast.Spec

func (AstSpecs) Filter

func (s AstSpecs) Filter(filter func(ast.Spec) bool) (ret AstSpecs)

type Config

type Config struct {
	// generation options
	From     string
	Params   map[string]string
	Renames  map[string]string
	Existing []*ast.File
	FileSet  *token.FileSet
	Uses     []string

	// output options
	Writer     io.Writer
	Package    string
	OutputFile string
}

type Err

type Err struct {
	Pkg  string
	Info string
	Prev error
}

func (*Err) Error

func (e *Err) Error() string

type ObjectSet

type ObjectSet map[types.Object]struct{}

func NewObjectSet

func NewObjectSet() ObjectSet

func (ObjectSet) Add

func (s ObjectSet) Add(t types.Object)

func (ObjectSet) In

func (s ObjectSet) In(t types.Object) (ok bool)

type StrSet

type StrSet map[string]struct{}

func NewStrSet

func NewStrSet() StrSet

func (StrSet) Add

func (s StrSet) Add(t string)

func (StrSet) In

func (s StrSet) In(t string) (ok bool)

Directories

Path Synopsis
cmd
ccg

Jump to

Keyboard shortcuts

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