Skip to content

Commit

Permalink
feat(typex): support generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Dec 29, 2024
1 parent 20e4eac commit ebb0904
Show file tree
Hide file tree
Showing 40 changed files with 5,222 additions and 1,901 deletions.
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PACKAGES=$(shell go list ./... | grep -E -v 'example|proto|testdata')
PACKAGES=$(shell go list ./... | grep -E -v 'example|proto|testdata|internal')
FORMAT_FILES=`find . -type f -name '*.go' | grep -E -v '_generated.go|.pb.go'`


debug:
@echo ${PACKAGES}
Expand Down Expand Up @@ -27,3 +29,19 @@ report:
@echo "done\n"

check: tidy vet test

MOD=$(shell cat go.mod | grep ^module -m 1 | awk '{ print $$2; }' || '')
FILE_LIST=$(shell find . -type f -name '*.go' | grep -E -v '_generated.go|.pb.go')

.PHONY: fmt
fmt:
@echo ${MOD}
@for item in ${FILE_LIST} ; \
do \
if [ -z ${MOD} ]; then \
goimports -d -w $$item ; \
else \
goimports -d -w -local "${MOD}" $$item ; \
fi \
done

13 changes: 6 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module github.com/xoctopus/x

go 1.22.0
go 1.23.0

require (
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7
golang.org/x/tools v0.23.0
golang.org/x/tools v0.28.0
)

require (
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
26 changes: 12 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7 h1:wDLEX9a7YQoKdKNQt88rtydkqDxeGaBUTnIYc3iG/mA=
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
85 changes: 85 additions & 0 deletions pkgx/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package pkgx

import (
"go/ast"
"go/token"
"sort"
"strings"
)

func newCommentScanner(fs *token.FileSet, f *ast.File) *CommentScanner {
return &CommentScanner{
File: f,
CommentMap: ast.NewCommentMap(fs, f, f.Comments),
}
}

type CommentScanner struct {
*ast.File
ast.CommentMap
}

func (c *CommentScanner) CommentsOf(n ast.Node) []string {
groups := c.CommentGroupsOf(n)
comments := make([]string, len(groups))
for i, g := range groups {
text := g.Text()
if strings.HasPrefix(text, "go:") {
continue
}
comments[i] = strings.TrimSpace(text)
}
return comments
}

func (c *CommentScanner) CommentGroupsOf(n ast.Node) (cgs []*ast.CommentGroup) {
if n == nil {
return
}

switch n.(type) {
case *ast.File, *ast.Field, ast.Stmt, ast.Decl:
if comments, ok := c.CommentMap[n]; ok {
cgs = comments
}
case ast.Spec:
if comments, ok := c.CommentMap[n]; ok {
cgs = append(cgs, comments...)
}
if len(cgs) == 0 {
for node, comments := range c.CommentMap {
if decl, ok := node.(*ast.GenDecl); ok {
for _, spec := range decl.Specs {
if n == spec {
cgs = append(cgs, comments...)
}
}
}
}
}
default:
var (
pos token.Pos = -1
parent ast.Node
)
ast.Inspect(c.File, func(node ast.Node) bool {
switch node.(type) {
case *ast.Field, ast.Stmt, ast.Decl, ast.Spec:
if n.Pos() >= node.Pos() && n.End() <= node.End() {
next := n.Pos() - node.Pos()
if pos == -1 || next <= pos {
pos, parent = next, node
}
}
}
return true
})
if parent != nil {
cgs = c.CommentGroupsOf(parent)
}
}
sort.Slice(cgs, func(i, j int) bool {
return cgs[i].Pos() < cgs[j].Pos()
})
return
}
47 changes: 47 additions & 0 deletions pkgx/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pkgx_test

import (
"go/types"
"testing"

. "github.com/onsi/gomega"
)

// func TestCommentScanner(t *testing.T) {
// fset := token.NewFileSet()
//
// fpth, err := filepath.Abs(path.Join(root, "comments.go"))
// if err != nil {
// t.Error(err)
// return
// }
//
// fast, err := parser.ParseFile(fset, fpth, nil, parser.ParseComments)
// if err != nil {
// t.Error(err)
// return
// }
//
// ast.Inspect(fast, func(node ast.Node) bool {
// comments := strings.Split(NewCommentScanner(fset, fast).CommentsOf(node), "\n")
// NewWithT(t).Expect(3 >= len(comments)).To(BeTrue())
// return true
// })
// }

func TestPkgComments(t *testing.T) {
for _, v := range []struct {
name string
object types.Object // identifier object
expect []string // expect identifier's comment
}{
{"Date", pkg.TypeName("Date"), []string{"Date defines corresponding time.Time"}},
{"Test", pkg.Var("test"), []string{"var"}},
{"A", pkg.Const("A"), []string{"A comment", "A inline comment"}},
{"Print", pkg.Func("Print"), []string{"Print function"}},
} {
t.Run(v.name, func(t *testing.T) {
NewWithT(t).Expect(pkg.CommentsOf(pkg.IdentOf(v.object))).To(Equal(v.expect))
})
}
}
Loading

0 comments on commit ebb0904

Please sign in to comment.