Skip to content

Commit

Permalink
Merge pull request #10 from gostaticanalysis/add-typels
Browse files Browse the repository at this point in the history
Add typels
  • Loading branch information
tenntenn committed May 1, 2023
2 parents 7df96ca + 93dd26d commit c6f7c07
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
84 changes: 84 additions & 0 deletions cmd/typels/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"flag"
"fmt"
"io"
"os"

"github.com/gostaticanalysis/knife"
)

var (
flagFilter string
flagExported bool
)

func init() {
flag.StringVar(&flagFilter, "f", "all", "object filter(all|interface|func|struct|chan|array|slice|map)")
flag.BoolVar(&flagExported, "exported", false, "filter only exported types")
flag.Parse()
}

func main() {
if err := run(flag.Args()); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}

func run(args []string) error {
k, err := knife.New(args...)
if err != nil {
return err
}

var w io.Writer = os.Stdout

pkgs := k.Packages()
for i, pkg := range pkgs {

kpkg := knife.NewPackage(pkg.Types)

if len(pkgs) > 1 {
fmt.Fprintf(w, "# %s\n", kpkg.Path)
}

for name, typ := range kpkg.Types {
if flagExported && !typ.Exported {
continue
}

if typ.Exported && match(flagFilter, typ) {
fmt.Fprintln(w, name)
}
}

if i != len(pkgs)-1 {
fmt.Fprintln(w)
}
}

return nil
}

func match(filter string, typ *knife.TypeName) bool {
switch filter {
case "interface":
return knife.ToInterface(typ) != nil
case "func":
return knife.ToSignature(typ) != nil
case "struct":
return knife.ToStruct(typ) != nil
case "chan":
return knife.ToChan(typ) != nil
case "array":
return knife.ToArray(typ) != nil
case "slice":
return knife.ToSlice(typ) != nil
case "map":
return knife.ToMap(typ) != nil
default:
return true
}
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
module github.com/gostaticanalysis/knife

go 1.14
go 1.18

require (
github.com/gostaticanalysis/analysisutil v0.6.2
github.com/gostaticanalysis/astquery v0.0.0-20200823120951-321f091076cd
github.com/gostaticanalysis/comment v1.4.2
golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a
)

require (
github.com/antchfx/xpath v1.1.10 // indirect
golang.org/x/mod v0.4.1 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc=
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=
github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0=
Expand Down

0 comments on commit c6f7c07

Please sign in to comment.