Skip to content

Commit

Permalink
Merge pull request #29 from kcmvp/type-impl
Browse files Browse the repository at this point in the history
Type impl
  • Loading branch information
kcmvp authored Jun 13, 2024
2 parents 92f7705 + 86440b3 commit 9a447fc
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 84 deletions.
10 changes: 7 additions & 3 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"log"
)

type Functions []lo.Tuple2[string, []string]
type Functions []internal.Function

func FunctionsOfType(fTypName string) Functions {
typ, ok := internal.Arch().Type(fTypName)
if !ok || !typ.Func() {
if !ok || !typ.FuncType() {
log.Fatalf("can not find function type %s", fTypName)
}
lo.ForEach(lo.Filter(internal.Arch().Packages(), func(pkg *internal.Package, _ int) bool {
Expand Down Expand Up @@ -56,7 +56,11 @@ func (functions Functions) ShouldBeInPackage(pkgPath ...string) error {
panic("to be implemented")
}

func (functions Functions) ShouldBe(visibility Visibility) error {
func (functions Functions) ShouldBe(visibility Visible) error {
panic("to be implemented")
}

func (functions Functions) LineOfCodeLessThan(n int) error {
panic("to be implemented")
}

Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kcmvp/gob v1.0.17 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
Expand All @@ -42,5 +44,6 @@ require (
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 15 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJ
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
Expand All @@ -27,15 +28,22 @@ github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBEx
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kcmvp/gob v1.0.17 h1:yjin/Y59rHbQ4WpxV4CNaR0TgBBMH54Y8Sm275a0XNs=
github.com/kcmvp/gob v1.0.17/go.mod h1:uFlwrOYFuRBw1mRKBtMNYHcM/64L98qyiNpRv9RM75Y=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand All @@ -50,8 +58,9 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -94,8 +103,9 @@ golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
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/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
2 changes: 1 addition & 1 deletion gob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins:
golangci-lint:
alias: lint
args: run ./...
url: github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2
url: github.com/golangci/golangci-lint/cmd/golangci-lint@v1.58.2
gotestsum:
alias: test
args: --format testname -- -coverprofile=target/cover.out ./...
Expand Down
33 changes: 19 additions & 14 deletions internal/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Function struct {
}

type Type struct {
raw *types.Named
raw *types.TypeName
}

type Variable struct {
Expand Down Expand Up @@ -121,8 +121,8 @@ func parse(pkg *packages.Package, mode ParseMode) *Package {
}
case *types.TypeName:
if ParseTyp&mode == ParseTyp {
if namedType, ok := vType.Type().(*types.Named); ok {
archPkg.types = append(archPkg.types, Type{raw: namedType})
if _, ok := vType.Type().(*types.Named); ok {
archPkg.types = append(archPkg.types, Type{raw: vType})
}
}
case *types.Var:
Expand Down Expand Up @@ -181,7 +181,7 @@ func (artifact *Artifact) Type(typName string) (Type, bool) {
}
}
return lo.Find(pkg.(*Package).types, func(typ Type) bool {
return typ.raw.String() == typName
return typ.Name() == typName
})
}

Expand Down Expand Up @@ -222,42 +222,47 @@ func (pkg *Package) Path() string {
}

func (typ Type) Interface() bool {
_, ok := typ.raw.Underlying().(*types.Interface)
_, ok := typ.Raw().Underlying().(*types.Interface)
return ok
}

func (typ Type) Package() string {
return typ.raw.Obj().Pkg().Path()
return typ.Raw().Obj().Pkg().Path()
}

func (typ Type) Func() bool {
panic("not implemented")
func (typ Type) FuncType() bool {
_, ok := typ.raw.Type().Underlying().(*types.Signature)
return ok
}

func (typ Type) Raw() *types.Named {
return typ.raw
return typ.raw.Type().(*types.Named)
}

func (typ Type) Name() string {
return typ.raw.String()
return typ.Raw().String()
}

func (typ Type) GoFile() string {
return Arch().Package(typ.Package()).raw.Fset.Position(typ.raw.Obj().Pos()).Filename
return Arch().Package(typ.Package()).raw.Fset.Position(typ.Raw().Obj().Pos()).Filename
}

func (typ Type) Exported() bool {
return typ.raw.Exported()
}

func (typ Type) Methods() []Function {
var functions []Function
if typ.Interface() {
iTyp := typ.raw.Underlying().(*types.Interface)
iTyp := typ.Raw().Underlying().(*types.Interface)
n := iTyp.NumMethods()
for i := 0; i < n; i++ {
functions = append(functions, Function{raw: iTyp.Method(i)})
}
} else {
n := typ.raw.NumMethods()
n := typ.Raw().NumMethods()
for i := 0; i < n; i++ {
functions = append(functions, Function{raw: typ.raw.Method(i)})
functions = append(functions, Function{raw: typ.Raw().Method(i)})
}
}
return functions
Expand Down
43 changes: 40 additions & 3 deletions internal/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ func TestPackage_Functions(t *testing.T) {
"FunctionsOfType",
"HavePrefix",
"HaveSuffix",
"Lay",
"AllTypes",
"AllPackages",
"LayerByPath",
"AppTypes",
"AppPackages",
"SourceNameShould",
"TypesEmbeddedWith",
"TypesImplement",
"TypesWith",
"PackageByPath",
},
imports: []string{
"fmt",
Expand Down Expand Up @@ -342,3 +344,38 @@ func TestArtifact(t *testing.T) {
assert.NotEmpty(t, Arch().RootDir())
assert.Equal(t, "github.com/kcmvp/archunit", Arch().Module())
}

func TestArchType(t *testing.T) {
size := len(arch.Packages())
typ, ok := Arch().Type("github.com/samber/lo.Entry[K comparable, V any]")
assert.True(t, ok)
assert.Equal(t, "github.com/samber/lo.Entry[K comparable, V any]", typ.Name())
assert.True(t, len(arch.Packages()) > size)
}

func TestArchFuncType(t *testing.T) {
tests := []struct {
name string
typ string
exp bool
}{
{
name: "valid",
typ: "internal/sample/controller.CustomizeHandler",
exp: true,
},
{
name: "invalid",
typ: "internal/sample/controller.EmbeddedGroup",
exp: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
typ, ok := Arch().Type(test.typ)
assert.True(t, ok)
assert.Equal(t, test.exp, typ.FuncType())
})
}
}
2 changes: 2 additions & 0 deletions internal/sample/controller/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/gin-gonic/gin"
)

type CustomizeHandler func(c *gin.Context) error

var SayHello gin.HandlerFunc = func(c *gin.Context) {}

func LoginHandler(ctx gin.Context) {
Expand Down
44 changes: 23 additions & 21 deletions layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"strings"
)

type Visibility int
type Visible int

const (
Public Visibility = iota
Public Visible = iota
Private
)

Expand Down Expand Up @@ -46,20 +46,6 @@ func SourceNameShould(pattern NamePattern, args ...string) error {
return nil
}

//func MethodsOfTypeShouldBeDefinedInSameFile() error {
// for _, pkg := range internal.Arch().Packages() {
// for _, typ := range pkg.Types() {
// files := lo.Uniq(lo.Map(typ.Methods(), func(f internal.Function, _ int) string {
// return f.GoFile()
// }))
// if len(files) > 1 {
// return fmt.Errorf("methods of type %s are defined in files %v", typ.Name(), files)
// }
// }
// }
// return nil
//}

func ConstantsShouldBeDefinedInOneFileByPackage() error {
for _, pkg := range internal.Arch().Packages() {
files := pkg.ConstantFiles()
Expand All @@ -70,7 +56,7 @@ func ConstantsShouldBeDefinedInOneFileByPackage() error {
return nil
}

func Lay(pkgPaths ...string) Layer {
func LayerByPath(pkgPaths ...string) Layer {
patterns := lo.Map(pkgPaths, func(path string, _ int) *regexp.Regexp {
reg, err := internal.PkgPattern(path)
if err != nil {
Expand Down Expand Up @@ -130,6 +116,18 @@ func (layer Layer) Sub(name string, paths ...string) Layer {
})
}

func (layer Layer) Packages() Packages {
return Packages(layer)
}

func (layer Layer) Functions() Functions {
var fs Functions
lo.ForEach(layer, func(pkg *internal.Package, _ int) {
fs = append(fs, pkg.Functions()...)
})
return fs
}

func (layer Layer) packages() []string {
return lo.Map(layer, func(item *internal.Package, _ int) string {
return item.Path()
Expand All @@ -156,7 +154,7 @@ func (layer Layer) ShouldNotReferLayers(layers ...Layer) error {
}

func (layer Layer) ShouldNotReferPackages(paths ...string) error {
return layer.ShouldNotReferLayers(Lay(paths...))
return layer.ShouldNotReferLayers(LayerByPath(paths...))
}

func (layer Layer) ShouldOnlyReferLayers(layers ...Layer) error {
Expand All @@ -171,7 +169,7 @@ func (layer Layer) ShouldOnlyReferLayers(layers ...Layer) error {
}

func (layer Layer) ShouldOnlyReferPackages(paths ...string) error {
return layer.ShouldOnlyReferLayers(Lay(paths...))
return layer.ShouldOnlyReferLayers(LayerByPath(paths...))
}

func (layer Layer) ShouldBeOnlyReferredByLayers(layers ...Layer) error {
Expand All @@ -195,7 +193,7 @@ func (layer Layer) ShouldBeOnlyReferredByLayers(layers ...Layer) error {
}

func (layer Layer) ShouldBeOnlyReferredByPackages(paths ...string) error {
layer1 := Lay(paths...)
layer1 := LayerByPath(paths...)
return layer.ShouldBeOnlyReferredByLayers(layer1)
}

Expand All @@ -210,7 +208,11 @@ func (layer Layer) DepthShouldLessThan(depth int) error {
}

func (layer Layer) Types() Types {
panic("to be implemented")
var ts Types
lo.ForEach(layer, func(pkg *internal.Package, _ int) {
ts = append(ts, pkg.Types()...)
})
return ts
}

func (layer Layer) Files() Files {
Expand Down
Loading

0 comments on commit 9a447fc

Please sign in to comment.