Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- name: Modver
if: ${{ github.event_name == 'pull_request' }}
uses: bobg/modver@v2.12.1
uses: bobg/modver@v2.12.2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pull_request_url: https://github.com/${{ github.repository }}/pull/${{ github.event.number }}
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/bobg/decouple.svg)](https://pkg.go.dev/github.com/bobg/decouple)
[![Go Report Card](https://goreportcard.com/badge/github.com/bobg/decouple)](https://goreportcard.com/report/github.com/bobg/decouple)
[![Tests](https://github.com/bobg/decouple/actions/workflows/go.yml/badge.svg)](https://github.com/bobg/decouple/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github.com/bobg/decouple/badge.svg?branch=main)](https://coveralls.io/github.com/bobg/decouple?branch=main)
[![Coverage Status](https://coveralls.io/repos/github/bobg/decouple/badge.svg?branch=main)](https://coveralls.io/github/bobg/decouple?branch=main)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)

This is decouple,
Expand Down
6 changes: 3 additions & 3 deletions cmd/decouple/decouple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestRunJSON(t *testing.T) {
buf := new(bytes.Buffer)
if err := run(buf, false, true, []string{"../.."}); err != nil {
if err := run2(buf, false, true, false, []string{"../.."}); err != nil {
t.Fatal(err)
}

Expand All @@ -34,7 +34,7 @@ func TestRunJSON(t *testing.T) {
want := []jtuple{{
PackageName: "main",
FileName: "main.go",
Line: 106,
Line: 113,
Column: 6,
FuncName: "showJSON",
Params: []jparam{{
Expand All @@ -52,7 +52,7 @@ func TestRunJSON(t *testing.T) {

func TestRunPlain(t *testing.T) {
buf := new(bytes.Buffer)
if err := run(buf, false, false, []string{"../.."}); err != nil {
if err := run2(buf, false, false, false, []string{"../.."}); err != nil {
t.Fatal(err)
}

Expand Down
23 changes: 15 additions & 8 deletions cmd/decouple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,27 @@ import (
)

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

func run() error {
var (
verbose bool
doJSON bool
deprecated bool
doJSON bool
verbose bool
)
flag.BoolVar(&verbose, "v", false, "verbose")
flag.BoolVar(&deprecated, "deprecated", false, "include deprecated functions in the analysis")
flag.BoolVar(&doJSON, "json", false, "output in JSON format")
flag.BoolVar(&verbose, "v", false, "verbose")
flag.Parse()

if err := run(os.Stdout, verbose, doJSON, flag.Args()); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return run2(os.Stdout, deprecated, doJSON, verbose, flag.Args())
}

func run(w io.Writer, verbose, doJSON bool, args []string) error {
func run2(w io.Writer, deprecated, doJSON, verbose bool, args []string) error {
var dir string
switch len(args) {
case 0:
Expand All @@ -46,6 +52,7 @@ func run(w io.Writer, verbose, doJSON bool, args []string) error {
if err != nil {
return errors.Wrapf(err, "creating checker for %s", dir)
}
checker.Deprecated = deprecated
checker.Verbose = verbose

tuples, err := checker.Check()
Expand Down
21 changes: 20 additions & 1 deletion decouple.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ const PkgMode = packages.NeedName | packages.NeedFiles | packages.NeedImports |
// or a single such package,
// or a function or function parameter in one.
//
// Set Deprecated to include deprecated functions in the analysis, which are normally excluded.
// Set Verbose to true to get (very) verbose debugging output.
type Checker struct {
Verbose bool
Deprecated bool
Verbose bool

pkgs []*packages.Package
namedInterfaces map[*packages.Package]map[string]namedInterfacePair // pkg -> named interface type -> (method map, is alias)
Expand Down Expand Up @@ -191,6 +193,10 @@ type MethodMap = map[string]*types.Signature
// which should be one of the packages contained in the Checker.
// The result is a map from parameter names eligible for decoupling to MethodMaps.
func (ch Checker) CheckFunc(pkg *packages.Package, fndecl *ast.FuncDecl) (map[string]MethodMap, error) {
if !ch.Deprecated && isDeprecated(fndecl) {
return nil, nil
}

result := make(map[string]MethodMap)
for _, field := range fndecl.Type.Params.List {
for _, name := range field.Names {
Expand All @@ -210,6 +216,19 @@ func (ch Checker) CheckFunc(pkg *packages.Package, fndecl *ast.FuncDecl) (map[st
return result, nil
}

func isDeprecated(fndecl *ast.FuncDecl) bool {
if fndecl.Doc == nil {
return false
}
pars := strings.Split(fndecl.Doc.Text(), "\n\n")
for _, par := range pars {
if strings.HasPrefix(par, "Deprecated:") {
return true
}
}
return false
}

// CheckParam checks a single named parameter in a given function declaration,
// which must apepar in the given package,
// which should be one of the packages in the Checker.
Expand Down
Loading