bast

module
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

README

BAST

Bastard AST is lightweight model of top level Go declarations constructed from go/ast and intended for code generation using text/template.

Status

Experimental. API is subject to change.

Example

Loading is straightforward like with golang.org/x/tools/go/packages. Specify file, directory or module path one or more times to load into bast then pass it to a template.

package main

import (
	"os"
	"text/template"
	"github.com/vedranvuk/bast"
)

var (
	err error
	data *bast.Bast
	file []byte
	tmpl *template.Template = template.New("bast")
)

func main() {
	if data, err = bast.Load("file.go", "./pkg/package", "net/http"); err != nil {
		fatal(err)
	}
	if file, err = os.ReadFile("template.tmpl"); err != nil {
		panic(err)
	}
	if tmpl, err = tmpl.Funcs(data.FuncMap()).Parse(string(file)); err != nil {
		panic(err)
	}
	if err = tmpl.ExecuteTemplate("bast", os.Stdout, data); err != nil {
		panic(err)
	}
}

Reference command

Bast comes with a command that serves as use example. It can execute a single template file at a time. It also has a lot more docs but best docs are the source docs.

To install it: go install github.com/vedranvuk/bast.

Tool reference is at bast -f.

For slightly more boilerplating power check out boil.

License

MIT.

Directories

Path Synopsis
_testdata
cmd
bast
Bast is a reference implementation of bast functionality.
Bast is a reference implementation of bast functionality.
pkg
bast
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.
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.

Jump to

Keyboard shortcuts

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