2018-08-03 15:08:30 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/importer"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"go/types"
|
|
|
|
"log"
|
2018-08-06 12:11:15 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-08-03 15:08:30 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2018-08-06 12:14:16 -04:00
|
|
|
|
|
|
|
"gitlab.wow.st/gmp/persist/generate"
|
2018-08-03 15:08:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type visitor struct{}
|
|
|
|
|
|
|
|
func trimType(n *string) {
|
|
|
|
*n = pkgreg.ReplaceAllString(*n,"")
|
2018-08-04 16:39:39 -04:00
|
|
|
*n = treg.ReplaceAllString(*n,"")
|
2018-08-03 15:08:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) Visit(node ast.Node) ast.Visitor {
|
|
|
|
x, ok := node.(*ast.CallExpr); if !ok { return v }
|
|
|
|
id,ok := x.Fun.(*ast.Ident); if !ok { return v }
|
|
|
|
|
|
|
|
if !reg.MatchString(id.Name) { return v }
|
|
|
|
|
2018-08-04 16:03:00 -04:00
|
|
|
if len(x.Args) < 2 {
|
2018-08-04 15:41:34 -04:00
|
|
|
log.Fatal("Wrong number of arguments in persistT call.")
|
2018-08-03 15:08:30 -04:00
|
|
|
}
|
|
|
|
var tp string
|
|
|
|
switch arg := x.Args[1].(type) {
|
|
|
|
case *ast.BasicLit:
|
|
|
|
tp = strings.ToLower(arg.Kind.String())
|
|
|
|
if tp == "float" {
|
|
|
|
tp = "float64"
|
|
|
|
}
|
2018-08-04 15:41:34 -04:00
|
|
|
case *ast.CompositeLit:
|
|
|
|
tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg))
|
2018-08-03 15:08:30 -04:00
|
|
|
case *ast.Ident:
|
2018-08-04 16:39:39 -04:00
|
|
|
tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg))
|
2018-08-03 15:08:30 -04:00
|
|
|
trimType(&tp)
|
2018-08-04 15:41:34 -04:00
|
|
|
for _, s := range impreg.FindAllStringSubmatchIndex(tp, -1) {
|
|
|
|
pkgname := impreg.ExpandString(make([]byte,0), "$1", tp, s)
|
2018-08-03 15:08:30 -04:00
|
|
|
needImps[string(pkgname)] = true
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
name := reg.ReplaceAllString(id.Name,"$1")
|
|
|
|
ts[name] = tps{fun: id.Name, name: name, typ: tp}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type tps struct {
|
|
|
|
fun,name,typ string
|
|
|
|
}
|
|
|
|
|
|
|
|
var fset *token.FileSet
|
|
|
|
var pkg *types.Package
|
|
|
|
var ts map[string]tps
|
|
|
|
var reg *regexp.Regexp
|
|
|
|
var pkgreg *regexp.Regexp
|
|
|
|
var impreg *regexp.Regexp
|
2018-08-04 16:39:39 -04:00
|
|
|
var treg *regexp.Regexp
|
2018-08-03 15:08:30 -04:00
|
|
|
var imps map[string]string
|
|
|
|
var needImps map[string]bool
|
2018-08-04 15:41:34 -04:00
|
|
|
var info *types.Info
|
2018-08-03 15:08:30 -04:00
|
|
|
|
|
|
|
func chkpkg(p *ast.Package) {
|
|
|
|
fs := make([]*ast.File,0)
|
|
|
|
for _,f := range p.Files {
|
|
|
|
fs = append(fs,f)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := types.Config{Error: func(error) {}, Importer: importer.Default()}
|
|
|
|
var err error
|
2018-08-04 15:41:34 -04:00
|
|
|
info = &types.Info{
|
|
|
|
Types: make(map[ast.Expr]types.TypeAndValue),
|
|
|
|
Defs: make(map[*ast.Ident]types.Object),
|
|
|
|
Uses: make(map[*ast.Ident]types.Object)}
|
|
|
|
|
|
|
|
pkg, err = conf.Check("main", fset, fs, info)
|
2018-08-03 15:08:30 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print("Check:",err)
|
|
|
|
}
|
|
|
|
var v = &visitor{}
|
|
|
|
for _,f := range fs {
|
|
|
|
for _,is := range f.Imports {
|
|
|
|
name := is.Path.Value
|
|
|
|
shortName := pkgreg.ReplaceAllString(name,"")
|
|
|
|
imps[shortName] = name
|
|
|
|
}
|
|
|
|
ast.Walk(v,f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reg = regexp.MustCompile("^persist([a-zA-Z_]+[a-zA-Z_0-9]*)")
|
|
|
|
|
|
|
|
pkgreg = regexp.MustCompile(`[a-zA-Z_]+[a-zA-Z_0-9\.]*/|"`)
|
|
|
|
impreg = regexp.MustCompile(`([a-zA-Z_]+[a-zA-Z_0-9]+)\.[a-zA-Z_]+[a-zA-Z_0-9]+`)
|
2018-08-04 16:39:39 -04:00
|
|
|
treg = regexp.MustCompile(`untyped `)
|
2018-08-03 15:08:30 -04:00
|
|
|
|
|
|
|
imps = make(map[string]string)
|
|
|
|
needImps = make(map[string]bool)
|
|
|
|
fset = token.NewFileSet()
|
2018-08-06 12:11:15 -04:00
|
|
|
|
|
|
|
// clear out old generated files
|
|
|
|
fs,err := filepath.Glob("pgen*.go")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Glob error")
|
|
|
|
}
|
|
|
|
for _,f := range(fs) {
|
|
|
|
err := os.Remove(f)
|
|
|
|
if err != nil && os.IsExist(err) {
|
|
|
|
log.Fatal("Removing ",f,err)
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:08:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
pkgs, err := parser.ParseDir(fset, ".", nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Parse:",err)
|
|
|
|
}
|
2018-08-06 10:01:40 -04:00
|
|
|
// run type checker on each package
|
2018-08-03 15:08:30 -04:00
|
|
|
for _,pkg := range pkgs {
|
2018-08-06 11:04:25 -04:00
|
|
|
log.Print("Processing package ",pkg.Name)
|
2018-08-06 10:01:40 -04:00
|
|
|
ts = make(map[string]tps)
|
2018-08-03 15:08:30 -04:00
|
|
|
chkpkg(pkg)
|
2018-08-06 12:11:15 -04:00
|
|
|
if len(ts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-06 12:14:16 -04:00
|
|
|
g := generate.NewGenerator(pkg.Name)
|
2018-08-06 10:01:40 -04:00
|
|
|
for _,v := range ts {
|
|
|
|
g.Add(v.fun, v.name, v.typ)
|
|
|
|
}
|
2018-08-06 11:04:25 -04:00
|
|
|
for name := range needImps {
|
|
|
|
g.Import(imps[name])
|
|
|
|
}
|
2018-08-06 12:11:15 -04:00
|
|
|
var of *os.File
|
|
|
|
var err error
|
|
|
|
if pkg.Name == "main" {
|
|
|
|
of, err = os.Create("pgen.go")
|
|
|
|
} else {
|
|
|
|
oname := strings.Join([]string{"pgen_",pkg.Name,".go"},"")
|
|
|
|
of, err = os.Create(oname)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Cannot open output file: ",err)
|
|
|
|
}
|
2018-08-06 10:01:40 -04:00
|
|
|
// process template for each type identified and
|
|
|
|
// generate output
|
2018-08-06 12:11:15 -04:00
|
|
|
g.Save(of)
|
2018-08-03 15:08:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|