bast

package
v0.0.0-...-6b7119f Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package bast implements a simple intermediate object model of top level Go declarations from AST of go source files and is designed for use in text based code-generation with Go's text/template templating engine.

Bast's structure can be easily traversed from a template and provides a number of functions to help with data retrieval and other utils.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Print

func Print(w io.Writer, bast *Bast)

Types

type Bast

type Bast struct {
	// Packages is a list of packages parsed into bast using Load().
	//
	// Files outside of a package given to Load will be placed in a package
	// with an empty name.
	Packages map[string]*Package
}

Bast is a top level struct that contains parsed go packages. It also implements all functions usable from a text/template.

func Load

func Load(patterns ...string) (bast *Bast, err error)

Load loads and returns bast of Go packages named by the given patterns.

Paths can be absolute or relative paths to a directory containing go source files possibly defining a package or a cmd or a go file. It can also be a go module path loading of which depends on the current go environment.

If an error in loading a file or a package occurs the error returned will be the first error of the first package that contains an error. Nil Bast is returned in case of an error.

Patterns which name file paths ar epared into an unnamed package in bast.

func New

func New() *Bast

New returns a new, empty *Bast.

func (*Bast) FuncMap

func (self *Bast) FuncMap() template.FuncMap

FuncMap returns a funcmap for use with text/template templates.

type Config

type Config struct {
	PrintConsts     bool
	PrintVars       bool
	PrintTypes      bool
	PrintFuncs      bool
	PrintMethods    bool
	PrintStructs    bool
	PrintInterfaces bool
}

func DefaultConfig

func DefaultConfig() *Config

func (*Config) Print

func (self *Config) Print(w io.Writer, bast *Bast)

type Const

type Const struct {
	// Comment is the const comment.
	Comment []string
	// Doc is the const doc comment.
	Doc []string
	// Name is the constant name.
	Name string
	// Type is the const type, empty if undpecified.
	Type string
	// Value is the const value, empty if undpecified.
	Value string
}

Const contains info about a constant.

func NewConst

func NewConst() *Const

NewConst returns a new *Const.

func (*Const) GetName

func (self *Const) GetName() string

type Declaration

type Declaration interface {
	// GetName returns the Declaration name.
	GetName() string
}

Declaration represents a top level declaration in a Go file.

type Field

type Field struct {
	// Comment is the field comment.
	Comment []string
	// Doc is the field doc comment.
	Doc []string
	// Name is the field name.
	Name string
	// Type is the field type.
	Type string
	// Tag is the field raw tag string.
	Tag string
}

Field contains info about a struct field, method receiver, or method or func type params, params or results.

func NewField

func NewField() *Field

NewField returns a new *Field.

func (*Field) GetName

func (self *Field) GetName() string

type File

type File struct {
	// Comments are the file comments, grouped by separation, including docs.
	Comments [][]string
	// Doc is the file doc comment.
	Doc []string
	// Name is the File name, without path.
	Name string
	// Imports is a list of file imports.
	Imports map[string]*Import
	// Declarations is a list of top level declarations in the file.
	Declarations map[string]Declaration
}

File contians info about a Go source file.

func NewFile

func NewFile() *File

NewFile returns a new *File.

func (*File) GetName

func (self *File) GetName() string

type Func

type Func struct {
	// Comment is the func comment.
	Comment []string
	// Doc is the func doc comment.
	Doc []string
	// Name is the func name.
	Name string
	// TypeParams are type parameters.
	TypeParams map[string]*Field
	//  Params is a list of func arguments.
	Params map[string]*Field
	// Results is a list of func returns.
	Results map[string]*Field
}

Func contains info about a function.

func NewFunc

func NewFunc() *Func

NewFunc returns a new *Func.

func (*Func) GetName

func (self *Func) GetName() string

type Import

type Import struct {
	// Comment is the import comment.
	Comment []string
	// Doc is the import doc.
	Doc []string
	// Name is the import name, possibly empty, "." or some custom name.
	Name string
	// Path is the import path.
	Path string
}

Import contians info about an import.

func NewImport

func NewImport() *Import

NewImport returns a new *Import.

func (*Import) GetName

func (self *Import) GetName() string

type Interface

type Interface struct {
	// Comment is the interface comment.
	Comment []string
	// Doc is the interface doc comment.
	Doc []string
	// Name is the interface name.
	Name string
	// Methods is a list of methods defined by the interface.
	Methods map[string]*Method
}

Interface contains info about an interface.

func NewInterface

func NewInterface() *Interface

NewInterface returns a new *Interface.

func (*Interface) GetName

func (self *Interface) GetName() string

type Method

type Method struct {
	// Func embeds all Func properties.
	Func
	// Receiver is the method receiver.
	Receivers map[string]*Field
}

Method contains info about a method.

func NewMethod

func NewMethod() *Method

NewMethod returns a new *Method.

func (*Method) GetName

func (self *Method) GetName() string

type Package

type Package struct {
	// Name is the package name, without path.
	Name string
	// Files is a list of files in the package.
	Files map[string]*File
}

Package contians info about a Go package.

func NewPackage

func NewPackage() *Package

NewPackage returns a new *Package.

func (*Package) GetName

func (self *Package) GetName() string

type Struct

type Struct struct {
	// Comment is the struct comment.
	Comment []string
	// Doc is the struct doc comment.
	Doc []string
	// Name is the struct name.
	Name string
	// Fields is a list of struct fields.
	Fields map[string]*Field
}

Struct contains info about a struct.

func NewStruct

func NewStruct() *Struct

NewStruct returns a new *Struct.

func (*Struct) GetName

func (self *Struct) GetName() string

type Type

type Type struct {
	// Comment is the struct comment.
	Comment []string
	// Doc is the struct doc comment.
	Doc []string
	// Name is the struct name.
	Name string
	// Type is Type's underlying type.
	Type string
	// IsAlias is true if type is an alias of the type it derives from.
	IsAlias bool
}

Type contains info about a type.

func NewType

func NewType() *Type

NewType returns a new *Type.

func (*Type) GetName

func (self *Type) GetName() string

type Var

type Var struct {
	// Comment is the const comment.
	Comment []string
	// Doc is the const doc comment.
	Doc []string
	// Name is the constant name.
	Name string
	// Type is the const type, empty if undpecified.
	Type string
	// Value is the const value, empty if undpecified.
	Value string
}

Var contains info about a variable.

func NewVar

func NewVar() *Var

NewVar returns a new *Var.

func (*Var) GetName

func (self *Var) GetName() string

Jump to

Keyboard shortcuts

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