Skip to content

Commit

Permalink
fix: Fixes for client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed Jun 7, 2023
1 parent c3430b5 commit b5f0644
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
12 changes: 11 additions & 1 deletion generate/find_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package generate
import (
"go/ast"
"go/types"
"strings"

"github.com/rs/zerolog/log"
"github.com/tigrisdata/tigrisgen/util"
Expand Down Expand Up @@ -109,7 +110,15 @@ func exprToFuncDecl(tp string, f ast.Expr, pi *packages.Package) (string, *ast.F
case *ast.Ident: // function
if a.Obj != nil && a.Obj.Kind == ast.Fun {
log.Debug().Str("API", tp).Str("name", a.Name).Int("pos", int(a.Pos())).Msg("detected simple function")
return pi.Name + "." + a.Name, a.Obj.Decl.(*ast.FuncDecl), pi

p := strings.TrimPrefix(Pwd, pi.Module.Dir)
if p != "" {
p = pi.Module.Path + p + "."
} else {
p = pi.Name + "."
}

return p + a.Name, a.Obj.Decl.(*ast.FuncDecl), pi
}
case *ast.SelectorExpr:
if s, ok := a.X.(*ast.Ident); ok { // method or external package function
Expand Down Expand Up @@ -152,6 +161,7 @@ func exprToFuncDecl(tp string, f ast.Expr, pi *packages.Package) (string, *ast.F
if !ok {
util.Fatal("not a signature type: %v", fn.Type())
}

path := pkg.Imported().Path()
log.Debug().Str("API", tp).Str("name", sig.String()).Str("package", path).
Msg("detected external package document method")
Expand Down
3 changes: 3 additions & 0 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
_ "embed"
"io"
"os"
"strings"
"text/template"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -62,6 +63,8 @@ func writeGenFile(name string, pkg string, filters []FilterDef, updates []Filter
return err
}

pkg = strings.TrimSuffix(pkg, "_test")

if err = writeGenFileLow(f, pkg, filters, updates); err != nil {
_ = f.Close()
return err
Expand Down
77 changes: 50 additions & 27 deletions generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import (
var tigrisPkg = "github.com/tigrisdata/tigris-client-go/tigris"

// Program source loaded into memory.
var Program = map[string]*packages.Package{}
var (
Program = map[string]*packages.Package{}
Pwd string
)

func getFilterFunc(name string, pi *packages.Package, ident ast.Expr, flt ast.Expr, upd ast.Expr, filtersFNs []ast.Expr,
updatesFNs []ast.Expr,
Expand Down Expand Up @@ -105,15 +108,16 @@ func findAPIcalls(node *ast.File, pi *packages.Package, apiName string) ([]ast.E
func loadProgram(program map[string]*packages.Package, args []string) {
start := time.Now()

cfg := packages.Config{Tests: true, Mode: packages.NeedName | packages.NeedDeps | packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo}
cfg := packages.Config{Tests: true, Mode: packages.NeedName | packages.NeedDeps | packages.NeedSyntax |
packages.NeedTypes | packages.NeedTypesInfo | packages.NeedModule}

pkgs, err := packages.Load(&cfg, args...)
if err != nil {
panic(err)
}

for _, v := range pkgs {
log.Info().Str("id", v.ID).Msg("loading package")
log.Info().Str("id", v.ID).Str("path", v.PkgPath).Str("name", v.Name).Msg("loading package")
program[v.ID] = v
}

Expand All @@ -132,30 +136,36 @@ func findAndParseAPI(api string, f *ast.File, pi *packages.Package,

flt, u := findAPIcalls(f, pi, api)

for _, ff := range flt {
name, body, pi := exprToFuncDecl(api, ff, pi)
if body != nil {
if fltName[name] {
log.Debug().Str("name", name).Msg("skipping duplicate filter")
continue
}
if api != "UpdateAll" {
for _, ff := range flt {
name, body, pi := exprToFuncDecl(api, ff, pi)
if body != nil {
if fltName[name] {
log.Debug().Str("name", name).Msg("skipping duplicate filter")
continue
}

fltName[name] = true
n, flt := parseFilterFunction(name, body, pi)
log.Info().Str("name", n).Str("filter", flt).Msg("filter")
filters = append(filters, FilterDef{n, flt})
} else {
log.Warn().Str("package", pi.Name).Str("expr",
reflect.TypeOf(ff).Name()).Msg("not a filter function")
fltName[name] = true
n, flt := parseFilterFunction(name, body, pi)
log.Info().Str("name", n).Str("filter", flt).Msg("filter")
filters = append(filters, FilterDef{n, flt})
} else {
log.Warn().Str("package", pi.Name).Str("expr",
reflect.TypeOf(ff).Name()).Msg("not a filter function")
}
}
}

if api != "Update" {
if api != "Update" && api != "UpdateOne" && api != "UpdateAll" {
return filters, updates
}

if api == "UpdateAll" {
u = flt
}

for _, ff := range u {
name, body, pi := exprToFuncDecl("Update", ff, pi)
name, body, pi := exprToFuncDecl(api, ff, pi)
if body != nil {
if updName[name] {
log.Debug().Str("name", name).Msg("skipping duplicate update")
Expand Down Expand Up @@ -187,12 +197,14 @@ func findAndParse(pi *packages.Package) ([]FilterDef, []FilterDef) {

log.Debug().Str("package", pi.Name).Msg("processing package")

apis := []string{"Update", "UpdateOne", "UpdateAll", "Read", "ReadOne", "ReadWithOptions", "Delete", "DeleteOne"}

for _, f := range pi.Syntax {
log.Debug().Str("file", pi.Fset.File(f.Pos()).Name()).Msg("processing file")

filters, updates = findAndParseAPI("Update", f, pi, filters, updates, fltName, updName)
filters, updates = findAndParseAPI("Read", f, pi, filters, updates, fltName, updName)
filters, updates = findAndParseAPI("Delete", f, pi, filters, updates, fltName, updName)
for _, v := range apis {
filters, updates = findAndParseAPI(v, f, pi, filters, updates, fltName, updName)
}
}

return filters, updates
Expand All @@ -204,19 +216,30 @@ func MainLow() {
s, _ := os.Getwd()
log.Debug().Strs("args", os.Args).Str("pwd", s).Msg("Starting")

args := []string{os.Getenv("GOFILE")}
if len(os.Args) >= 2 {
args = os.Args
var err error

Pwd, err = os.Getwd()
if err != nil {
util.Fatal("%v", err)
}

loadProgram(Program, args)
loadProgram(Program, []string{Pwd})

for _, pi := range Program {
if pi.Name != os.Getenv("GOPACKAGE") {
continue
}

log.Debug().Str("package", pi.Name).Msg("processing")

flts, upds := findAndParse(pi)

// log.Debug().Interface("filters", flts).Interface("updates", upds).Msg("parsed")
if len(flts) == 0 && len(upds) == 0 {
log.Debug().Msg("No filters or updates found in package")
continue
}

log.Debug().Interface("filters", flts).Interface("updates", upds).Msg("parsed")

err := writeGenFile("tigris.gen.go", pi.Name, flts, upds)
if err != nil {
Expand Down

0 comments on commit b5f0644

Please sign in to comment.