persist/generate/generate.go

158 lines
3.1 KiB
Go
Raw Normal View History

package generate
2018-08-03 15:08:30 -04:00
import (
2018-08-06 12:11:15 -04:00
"io"
2018-08-03 15:08:30 -04:00
"log"
"go/ast"
"go/format"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"regexp"
)
type subspec struct {
r *regexp.Regexp
s string
}
2018-08-06 10:01:40 -04:00
type Generator struct{
2018-08-03 15:08:30 -04:00
firstone bool
2018-08-06 10:01:40 -04:00
fset *token.FileSet
2018-08-03 15:08:30 -04:00
fspec, tspec, nspec subspec
2018-08-06 10:01:40 -04:00
nspecs []subspec
2018-08-03 15:08:30 -04:00
nodes *ast.File
name string
2018-08-06 10:01:40 -04:00
}
2018-08-03 15:08:30 -04:00
func subs(dst *string, specs []subspec) {
for _,spec := range(specs) {
sub(dst,spec)
}
}
func sub(dst *string,spec subspec) {
if spec.r.MatchString(*dst) {
*dst = spec.r.ReplaceAllString(*dst,spec.s)
}
}
2018-08-06 10:01:40 -04:00
func (g *Generator) Visit(node ast.Node) ast.Visitor {
2018-08-03 15:08:30 -04:00
switch n := node.(type) {
case *ast.Ident:
if n.Obj != nil && n.Obj.Kind == ast.Typ {
2018-08-06 10:01:40 -04:00
subs(&n.Name,g.nspecs)
subs(&n.Obj.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
case *ast.TypeSwitchStmt:
for _,s := range(n.Body.List) {
cs, ok := s.(*ast.CaseClause); if !ok { continue }
for _,c := range(cs.List) {
ci, ok := c.(*ast.Ident); if !ok { continue }
2018-08-06 10:01:40 -04:00
subs(&ci.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
}
case *ast.TypeAssertExpr:
2018-08-06 10:01:40 -04:00
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.Ellipsis:
2018-08-06 10:01:40 -04:00
nt, ok := n.Elt.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.Field:
2018-08-06 10:01:40 -04:00
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.StarExpr:
2018-08-06 10:01:40 -04:00
nt, ok := n.X.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.ValueSpec:
2018-08-06 10:01:40 -04:00
nt, ok := n.Type.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.ArrayType:
2018-08-06 10:01:40 -04:00
nt, ok := n.Elt.(*ast.Ident); if !ok { return g }
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
case *ast.MapType:
nt, ok := n.Key.(*ast.Ident); if ok {
2018-08-06 10:01:40 -04:00
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
nt, ok = n.Value.(*ast.Ident); if ok {
2018-08-06 10:01:40 -04:00
subs(&nt.Name,g.nspecs)
2018-08-03 15:08:30 -04:00
}
case *ast.FuncDecl:
2018-08-06 10:01:40 -04:00
sub(&n.Name.Name,g.fspec)
//ast.Print(g.fset,n)
2018-08-03 15:08:30 -04:00
}
2018-08-06 10:01:40 -04:00
return g
2018-08-03 15:08:30 -04:00
}
var (
qexp *regexp.Regexp
)
2018-08-03 15:08:30 -04:00
func init() {
qexp = regexp.MustCompile(`"`)
2018-08-03 15:08:30 -04:00
}
func NewGenerator(ns ...string) *Generator {
2018-08-06 10:01:40 -04:00
g := &Generator{}
g.firstone = true
g.fset = token.NewFileSet()
if len(ns) > 0 {
g.name = ns[0]
} else {
g.name = "main"
}
2018-08-06 10:01:40 -04:00
return g
}
func (g *Generator) Add(newName, typName, typ string) {
g.fspec = subspec{regexp.MustCompile("New"), newName}
g.nspecs = []subspec{
subspec{regexp.MustCompile("_N"), typName},
subspec{regexp.MustCompile("_T"), typ}}
2018-08-03 15:08:30 -04:00
f, err := parser.ParseFile(g.fset, "", template, parser.ParseComments)
2018-08-03 15:08:30 -04:00
if err != nil {
log.Fatal("Parsing persist/template.T:",err)
}
2018-08-06 10:01:40 -04:00
ast.Walk(g,f)
2018-08-03 15:08:30 -04:00
2018-08-06 10:01:40 -04:00
if g.firstone {
g.nodes = f
2018-08-03 15:08:30 -04:00
} else {
for _,decl := range(f.Decls) {
imp, ok := decl.(*ast.GenDecl); if ok {
if imp.Tok == token.IMPORT {
continue // skip imports
}
}
2018-08-06 10:01:40 -04:00
g.nodes.Decls = append(g.nodes.Decls,decl)
2018-08-03 15:08:30 -04:00
}
}
2018-08-06 10:01:40 -04:00
g.firstone = false
2018-08-03 15:08:30 -04:00
}
func (g *Generator) Import(name string) {
2018-08-06 10:01:40 -04:00
if g.nodes == nil {
return
}
// strip quotation marks from imports
name = qexp.ReplaceAllString(name,"")
astutil.AddImport(g.fset, g.nodes, name)
2018-08-03 15:08:30 -04:00
}
2018-08-06 12:11:15 -04:00
func (g *Generator) Save(of io.Writer) {
2018-08-06 10:01:40 -04:00
if g.nodes == nil {
return
}
2018-08-06 12:11:15 -04:00
if g.name != "main" {
g.nodes.Name = ast.NewIdent(g.name)
}
2018-08-03 15:08:30 -04:00
2018-08-06 12:11:15 -04:00
err := format.Node(of,g.fset,g.nodes)
2018-08-03 15:08:30 -04:00
if err != nil {
log.Fatal("Generate error:",err)
}
}