-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from gostaticanalysis/add-typels
Add typels
- Loading branch information
Showing
3 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters