mirror of
https://github.com/foomo/gocontemplate.git
synced 2025-10-16 12:35:36 +00:00
refactor: structure
This commit is contained in:
parent
1142d15bfc
commit
76f3402d0d
@ -4,7 +4,7 @@
|
|||||||
[](https://goreportcard.com/report/github.com/foomo/gocontemplate)
|
[](https://goreportcard.com/report/github.com/foomo/gocontemplate)
|
||||||
[](https://godoc.org/github.com/foomo/gocontemplate)
|
[](https://godoc.org/github.com/foomo/gocontemplate)
|
||||||
|
|
||||||
> A code generation helper, that
|
> A code generation helper.
|
||||||
|
|
||||||
Wrapper library around `golang.org/x/tools/go/packages` to filter only defined types and their dependencies.
|
Wrapper library around `golang.org/x/tools/go/packages` to filter only defined types and their dependencies.
|
||||||
|
|
||||||
|
|||||||
1
pkg/assume/doc.go
Normal file
1
pkg/assume/doc.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package assume
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package gocontemplate
|
package assume
|
||||||
|
|
||||||
// TC type conversion helper
|
// T type conversion helper
|
||||||
func TC[T any](input any) T {
|
func T[T any](input any) T {
|
||||||
var output T
|
var output T
|
||||||
if input == nil {
|
if input == nil {
|
||||||
return output
|
return output
|
||||||
@ -1,10 +1,10 @@
|
|||||||
package gocontemplate
|
package contemplate
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Packages []*ConfigPackage `json:"packages" yaml:"packages"`
|
Packages []*PackageConfig `json:"packages" yaml:"packages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) Package(path string) *ConfigPackage {
|
func (c *Config) Package(path string) *PackageConfig {
|
||||||
for _, value := range c.Packages {
|
for _, value := range c.Packages {
|
||||||
if value.Path == path {
|
if value.Path == path {
|
||||||
return value
|
return value
|
||||||
@ -1,10 +1,11 @@
|
|||||||
package gocontemplate
|
package contemplate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/types"
|
"go/types"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/foomo/gocontemplate/pkg/assume"
|
||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
)
|
)
|
||||||
@ -34,7 +35,7 @@ func (s *Contemplate) LookupExpr(name string) ast.Expr {
|
|||||||
func (s *Contemplate) LookupTypesByType(obj types.Object) []types.Object {
|
func (s *Contemplate) LookupTypesByType(obj types.Object) []types.Object {
|
||||||
var ret []types.Object
|
var ret []types.Object
|
||||||
|
|
||||||
expr := TC[*ast.Ident](s.LookupExpr(obj.Name()))
|
expr := assume.T[*ast.Ident](s.LookupExpr(obj.Name()))
|
||||||
if expr == nil {
|
if expr == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -43,17 +44,17 @@ func (s *Contemplate) LookupTypesByType(obj types.Object) []types.Object {
|
|||||||
for _, object := range pkg.Types() {
|
for _, object := range pkg.Types() {
|
||||||
switch objectType := object.(type) {
|
switch objectType := object.(type) {
|
||||||
case *types.Const:
|
case *types.Const:
|
||||||
if objectTypeNamed := TC[*types.Named](objectType.Type()); objectTypeNamed != nil {
|
if objectTypeNamed := assume.T[*types.Named](objectType.Type()); objectTypeNamed != nil {
|
||||||
if objectTypeNamed.Obj() == obj {
|
if objectTypeNamed.Obj() == obj {
|
||||||
ret = append(ret, objectType)
|
ret = append(ret, objectType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *types.TypeName:
|
case *types.TypeName:
|
||||||
if objectExpr := pkg.LookupExpr(object.Name()); objectExpr != nil {
|
if objectExpr := pkg.LookupExpr(object.Name()); objectExpr != nil {
|
||||||
if objectExprIdent := TC[*ast.Ident](objectExpr); objectExprIdent != nil {
|
if objectExprIdent := assume.T[*ast.Ident](objectExpr); objectExprIdent != nil {
|
||||||
if objectExprDecl := TC[*ast.TypeSpec](objectExprIdent.Obj.Decl); objectExprDecl != nil {
|
if objectExprDecl := assume.T[*ast.TypeSpec](objectExprIdent.Obj.Decl); objectExprDecl != nil {
|
||||||
if objectExprType, ok := pkg.pkg.TypesInfo.Types[objectExprDecl.Type]; ok {
|
if objectExprType, ok := pkg.pkg.TypesInfo.Types[objectExprDecl.Type]; ok {
|
||||||
if objectExprTypeNamed := TC[*types.Named](objectExprType.Type); objectExprTypeNamed != nil {
|
if objectExprTypeNamed := assume.T[*types.Named](objectExprType.Type); objectExprTypeNamed != nil {
|
||||||
if objectExprTypeNamed.Obj() == obj {
|
if objectExprTypeNamed.Obj() == obj {
|
||||||
ret = append(ret, objectType)
|
ret = append(ret, objectType)
|
||||||
}
|
}
|
||||||
@ -84,7 +85,7 @@ func (s *Contemplate) addPackages(pkgs ...*packages.Package) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Contemplate) addPackagesConfigs(confs ...*ConfigPackage) {
|
func (s *Contemplate) addPackagesConfigs(confs ...*PackageConfig) {
|
||||||
for _, conf := range confs {
|
for _, conf := range confs {
|
||||||
s.Package(conf.Path).AddScopeTypes(conf.Types...)
|
s.Package(conf.Path).AddScopeTypes(conf.Types...)
|
||||||
}
|
}
|
||||||
@ -101,10 +102,6 @@ func (s *Contemplate) LookupAstIdentDefsByDeclType(input types.TypeAndValue) []t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// for _, p := range s.pkgs {
|
|
||||||
// pkgs = append(pkgs, p)
|
|
||||||
// addImports(p)
|
|
||||||
// }
|
|
||||||
|
|
||||||
var ret []types.Object
|
var ret []types.Object
|
||||||
for _, p := range pkgs {
|
for _, p := range pkgs {
|
||||||
@ -114,18 +111,6 @@ func (s *Contemplate) LookupAstIdentDefsByDeclType(input types.TypeAndValue) []t
|
|||||||
ret = append(ret, child)
|
ret = append(ret, child)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// for defAstIdent, defTypeObject := range p.TypesInfo.Defs {
|
|
||||||
// if defAstIdent != nil && defAstIdent.Obj != nil && defTypeObject != nil {
|
|
||||||
// if declValueSpec := TC[*ast.ValueSpec](defAstIdent.Obj.Decl); declValueSpec != nil {
|
|
||||||
// if declValueSpecIdent := TC[*ast.Ident](declValueSpec.Type); declValueSpecIdent != nil {
|
|
||||||
// if declValueSpecIdent.Obj == input.Obj {
|
|
||||||
// ret[defAstIdent] = defTypeObject
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
@ -136,36 +121,4 @@ func (s *Contemplate) addPackageTypeNames(pkg *packages.Package, typeNames ...st
|
|||||||
}
|
}
|
||||||
// add request scopes
|
// add request scopes
|
||||||
s.Packages[pkg.PkgPath].AddScopeTypes(typeNames...)
|
s.Packages[pkg.PkgPath].AddScopeTypes(typeNames...)
|
||||||
|
|
||||||
// for k, v := range s.Packages[pkg.PkgPath].Imports {
|
|
||||||
// s.addPackageTypeNames(k, v...)
|
|
||||||
// }
|
|
||||||
// check underlying added scopes
|
|
||||||
// for _, name := range added {
|
|
||||||
// s.typesType(pkg, s.Packages[pkg.PkgPath].Scope[name].Underlying())
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (s *Loader) typesType(pkg *packages.Package, v types.Type) {
|
|
||||||
// switch t := v.(type) {
|
|
||||||
// case *types.Struct:
|
|
||||||
// // iterate fields
|
|
||||||
// for i := range t.NumFields() {
|
|
||||||
// s.typesVar(pkg, t.Field(i))
|
|
||||||
// }
|
|
||||||
// default:
|
|
||||||
// fmt.Println(t)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (s *Loader) typesVar(pkg *packages.Package, v *types.Var) {
|
|
||||||
// if !v.Exported() {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// switch t := v.Type().(type) {
|
|
||||||
// case *types.Named:
|
|
||||||
// if p, ok := pkg.Imports[v.Pkg().Path()]; ok {
|
|
||||||
// s.addPackageTypeNames(p, t.Obj().Name())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@ -1,9 +1,9 @@
|
|||||||
package gocontemplate_test
|
package contemplate_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/foomo/gocontemplate"
|
"github.com/foomo/gocontemplate/pkg/contemplate"
|
||||||
_ "github.com/foomo/sesamy-go" // force inclusion
|
_ "github.com/foomo/sesamy-go" // force inclusion
|
||||||
_ "github.com/foomo/sesamy-go/event/params" // force inclusion
|
_ "github.com/foomo/sesamy-go/event/params" // force inclusion
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -12,8 +12,8 @@ import (
|
|||||||
|
|
||||||
func TestNewLoader(t *testing.T) {
|
func TestNewLoader(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
goctpl, err := gocontemplate.Load(&gocontemplate.Config{
|
goctpl, err := contemplate.Load(&contemplate.Config{
|
||||||
Packages: []*gocontemplate.ConfigPackage{
|
Packages: []*contemplate.PackageConfig{
|
||||||
{
|
{
|
||||||
Path: "github.com/foomo/sesamy-go/event",
|
Path: "github.com/foomo/sesamy-go/event",
|
||||||
Types: []string{"PageView"},
|
Types: []string{"PageView"},
|
||||||
@ -27,8 +27,8 @@ func TestNewLoader(t *testing.T) {
|
|||||||
|
|
||||||
func TestLoader_LookupTypesByType(t *testing.T) {
|
func TestLoader_LookupTypesByType(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
goctpl, err := gocontemplate.Load(&gocontemplate.Config{
|
goctpl, err := contemplate.Load(&contemplate.Config{
|
||||||
Packages: []*gocontemplate.ConfigPackage{
|
Packages: []*contemplate.PackageConfig{
|
||||||
{
|
{
|
||||||
Path: "github.com/foomo/sesamy-go/event",
|
Path: "github.com/foomo/sesamy-go/event",
|
||||||
Types: []string{"PageView"},
|
Types: []string{"PageView"},
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package gocontemplate
|
package contemplate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
@ -1,10 +1,10 @@
|
|||||||
package gocontemplate
|
package contemplate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/types"
|
"go/types"
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/foomo/gocontemplate/pkg/assume"
|
||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -105,11 +105,11 @@ func (s *Package) LookupScopeExpr(name string) ast.Expr {
|
|||||||
|
|
||||||
func (s *Package) FilterExprsByTypeExpr(expr ast.Expr) []ast.Expr {
|
func (s *Package) FilterExprsByTypeExpr(expr ast.Expr) []ast.Expr {
|
||||||
var ret []ast.Expr
|
var ret []ast.Expr
|
||||||
if exprIdent := TC[*ast.Ident](expr); exprIdent != nil {
|
if exprIdent := assume.T[*ast.Ident](expr); exprIdent != nil {
|
||||||
for _, child := range s.exprs {
|
for _, child := range s.exprs {
|
||||||
if childIdent := TC[*ast.Ident](child); childIdent != nil && childIdent.Obj != nil {
|
if childIdent := assume.T[*ast.Ident](child); childIdent != nil && childIdent.Obj != nil {
|
||||||
if childDecl := TC[*ast.ValueSpec](childIdent.Obj.Decl); childDecl != nil {
|
if childDecl := assume.T[*ast.ValueSpec](childIdent.Obj.Decl); childDecl != nil {
|
||||||
if childDeclType := TC[*ast.Ident](childDecl.Type); childDeclType != nil {
|
if childDeclType := assume.T[*ast.Ident](childDecl.Type); childDeclType != nil {
|
||||||
if childDeclType.Obj == exprIdent.Obj {
|
if childDeclType.Obj == exprIdent.Obj {
|
||||||
ret = append(ret, child)
|
ret = append(ret, child)
|
||||||
}
|
}
|
||||||
@ -147,7 +147,8 @@ func (s *Package) addScopeTypeAstExpr(input ast.Expr) {
|
|||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
if t.Obj != nil {
|
if t.Obj != nil {
|
||||||
s.addScopeTypeAstObject(t.Obj.Decl)
|
s.addScopeTypeAstObject(t.Obj.Decl)
|
||||||
} else {
|
}
|
||||||
|
if t.IsExported() {
|
||||||
s.l.addPackageTypeNames(s.pkg, t.Name)
|
s.l.addPackageTypeNames(s.pkg, t.Name)
|
||||||
}
|
}
|
||||||
case *ast.StructType:
|
case *ast.StructType:
|
||||||
@ -157,35 +158,45 @@ func (s *Package) addScopeTypeAstExpr(input ast.Expr) {
|
|||||||
case *ast.IndexExpr:
|
case *ast.IndexExpr:
|
||||||
s.addScopeTypeAstExpr(t.X)
|
s.addScopeTypeAstExpr(t.X)
|
||||||
s.addScopeTypeAstExpr(t.Index)
|
s.addScopeTypeAstExpr(t.Index)
|
||||||
|
case *ast.ArrayType:
|
||||||
|
s.addScopeTypeAstExpr(t.Elt)
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
s.addScopeTypeAstSelectorExpr(t)
|
s.addScopeTypeAstSelectorExpr(t)
|
||||||
|
default:
|
||||||
|
// fmt.Println(input, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Package) addScopeTypeAstSelectorExpr(input *ast.SelectorExpr) {
|
func (s *Package) addScopeTypeAstSelectorExpr(input *ast.SelectorExpr) {
|
||||||
if x := TC[*ast.Ident](input.X); x != nil {
|
if inputTypeNamed := assume.T[*types.Named](s.pkg.TypesInfo.TypeOf(input)); inputTypeNamed != nil {
|
||||||
if xPkgName := TC[*types.PkgName](s.pkg.TypesInfo.Uses[x]); xPkgName != nil {
|
s.l.addPackageTypeNames(s.pkg.Imports[inputTypeNamed.Obj().Pkg().Path()], inputTypeNamed.Obj().Name())
|
||||||
if selIdent := TC[*ast.Ident](input.Sel); selIdent != nil {
|
|
||||||
for node, object := range s.pkg.TypesInfo.Implicits {
|
|
||||||
if object == xPkgName {
|
|
||||||
if nodeImportSepc := TC[*ast.ImportSpec](node); nodeImportSepc != nil {
|
|
||||||
v := strings.Trim(nodeImportSepc.Path.Value, "\"")
|
|
||||||
s.l.addPackageTypeNames(s.pkg.Imports[v], selIdent.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Package) addScopeTypeAstObject(input any) {
|
func (s *Package) addScopeTypeAstObject(input any) {
|
||||||
switch t := input.(type) {
|
switch t := input.(type) {
|
||||||
case *ast.TypeSpec:
|
case *ast.TypeSpec:
|
||||||
|
s.addScopeTypeAstFieldList(t.TypeParams)
|
||||||
s.addScopeTypeAstExpr(t.Type)
|
s.addScopeTypeAstExpr(t.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Package) addScopeTypeAstFieldList(input *ast.FieldList) {
|
||||||
|
if input != nil {
|
||||||
|
for _, field := range input.List {
|
||||||
|
s.addScopeTypeAstField(field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Package) addScopeTypeAstField(input *ast.Field) {
|
||||||
|
// switch t := input.(type) {
|
||||||
|
// case *ast.TypeSpec:
|
||||||
|
// s.addScopeTypeAstFieldList(t.TypeParams)
|
||||||
|
// s.addScopeTypeAstExpr(t.Type)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Package) addExprs(source map[*ast.Ident]types.Object) {
|
func (s *Package) addExprs(source map[*ast.Ident]types.Object) {
|
||||||
for expr, object := range source {
|
for expr, object := range source {
|
||||||
if object != nil {
|
if object != nil {
|
||||||
@ -1,6 +1,6 @@
|
|||||||
package gocontemplate
|
package contemplate
|
||||||
|
|
||||||
type ConfigPackage struct {
|
type PackageConfig struct {
|
||||||
Path string `json:"path" yaml:"path"`
|
Path string `json:"path" yaml:"path"`
|
||||||
Types []string `json:"types" yaml:"types"`
|
Types []string `json:"types" yaml:"types"`
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user