gen

package
v0.0.0-...-ad26f7f Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package gen generates code for errguard.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTemplate = template.Must(template.New("defaultTemplate").Parse(`// Code generated by "{{.CommandLine}}"; DO NOT EDIT

package {{.Package}}

import ({{range .Imports}}
    {{.}}{{end}} 
    "github.com/jjeffery/errguard"
)

{{range .Interfaces}}
type guard{{.Name}} struct{
    inner {{.Name}}
}

func newGuard{{.Name}}(inner {{.Name}}) {{.Name}} {
    return &guard{{.Name}}{ inner: inner }
}
{{range .Methods}}

func (g *guard{{.Interface.Name}}) {{.Name}}({{.ParamDecl}}) ({{.ResultDecl}}) {
    var guard errguard.Guard
    {{.ErrorVar}} = guard.Run({{.ContextExpr}}, func() error {
        {{.ResultNames}} = g.inner.{{.Name}}({{.ArgNames}})
        return {{.ErrorVar}}
    })
    return {{.ResultNames}}
}
{{end}}
{{end}}`))

DefaultTemplate is the template used by default for generating code.

Functions

This section is empty.

Types

type Import

type Import struct {
	Name string // Local name, or blank
	Path string
}

Import describes a single import line required for the generated file.

func (*Import) String

func (imp *Import) String() string

type Interface

type Interface struct {
	Name    string
	Methods []*Method
}

Interface contains information about a single interface needed by the template

type Method

type Method struct {
	Interface   *Interface
	Name        string
	ArgNames    string // Comma separated list of input argument names
	ParamDecl   string // Parameters and types for method declaration
	ResultNames string // Comma separated list of result names
	ResultDecl  string // Results for method declaration
	ErrorVar    string // Name of the result error var
	ContextExpr string // Expression to use to obtain the context
}

Method contains information about a single method needed by the template.

type Model

type Model struct {
	CommandLine string
	Package     string
	Imports     []*Import
	Interfaces  []*Interface
}

Model contains all of the information required to generate the file.

func NewModel

func NewModel(file *ast.File, names []string) (*Model, error)

NewModel returns a model suitable for generating code from the information in the file AST and the list of names to generate code for. Each name should be the name of an interface or a function.

Jump to

Keyboard shortcuts

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