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
3 changes: 2 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ jobs:
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: cover.out
continue-on-error: true

- name: Modver
if: ${{ github.event_name == 'pull_request' }}
uses: bobg/modver@v2.7.0
uses: bobg/modver@v2.12.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pull_request_url: https://github.com/${{ github.repository }}/pull/${{ github.event.number }}
5 changes: 3 additions & 2 deletions 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/bobg/decouple/badge.svg?branch=main)](https://coveralls.io/github/bobg/decouple?branch=main)
[![Coverage Status](https://coveralls.io/repos/github.com/bobg/decouple/badge.svg?branch=main)](https://coveralls.io/github.com/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 Expand Up @@ -135,7 +135,8 @@ The same report with `-json` specified looks like this:
"Methods": [
"Write"
],
"InterfaceName": "io.Writer"
"InterfaceName": "Writer",
"InterfacePkg": "io"
}
]
}
Expand Down
7 changes: 6 additions & 1 deletion _testdata/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"os"
)

// This named type should never be suggested,
// since it is superseded by an identical one in the stdlib.
type JankyReader interface {
Read([]byte) (int, error)
}

// {"r": {"Read": "func([]byte) (int, error)"}}
// {"r": "io.Reader"}
func F1(r *os.File, n int) ([]byte, error) {
Expand Down Expand Up @@ -274,7 +280,6 @@ func F34(r *os.File, ch chan<- *os.File) ([]byte, error) {
}

// {"x": {"foo": "func()"}}
// {"x": ""}
func F35(x interface {
foo()
bar()
Expand Down
4 changes: 2 additions & 2 deletions _testdata/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module m
module github.com/bobg/decouple/testdata

go 1.19
go 1.23
10 changes: 6 additions & 4 deletions cmd/decouple/decouple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"encoding/json"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"

"github.com/bobg/go-generics/v3/iter"
"github.com/bobg/seqs"
)

func TestRunJSON(t *testing.T) {
Expand All @@ -33,7 +34,7 @@ func TestRunJSON(t *testing.T) {
want := []jtuple{{
PackageName: "main",
FileName: "main.go",
Line: 100,
Line: 106,
Column: 6,
FuncName: "showJSON",
Params: []jparam{{
Expand All @@ -55,8 +56,9 @@ func TestRunPlain(t *testing.T) {
t.Fatal(err)
}

lines, err := iter.ToSlice(iter.Lines(buf))
if err != nil {
linesSeq, errptr := seqs.Lines(buf)
lines := slices.Collect(linesSeq)
if err := *errptr; err != nil {
t.Fatal(err)
}

Expand Down
24 changes: 16 additions & 8 deletions cmd/decouple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"flag"
"fmt"
"io"
"maps"
"os"
"slices"
"sort"
"strings"

"github.com/bobg/errors"
"github.com/bobg/go-generics/v3/maps"

"github.com/bobg/decouple"
)
Expand Down Expand Up @@ -37,7 +39,7 @@ func run(w io.Writer, verbose, doJSON bool, args []string) error {
case 1:
dir = args[0]
default:
return fmt.Errorf("Usage: %s [-v] [-json] [DIR]", os.Args[0])
return fmt.Errorf("usage: %s [-v] [-json] [DIR]", os.Args[0])
}

checker, err := decouple.NewCheckerFromDir(dir)
Expand Down Expand Up @@ -70,7 +72,7 @@ func run(w io.Writer, verbose, doJSON bool, args []string) error {
for _, tuple := range tuples {
var showedFuncName bool

params := maps.Keys(tuple.M)
params := slices.Collect(maps.Keys(tuple.M))
sort.Strings(params)
for _, param := range params {
mm := tuple.M[param]
Expand All @@ -83,12 +85,16 @@ func run(w io.Writer, verbose, doJSON bool, args []string) error {
showedFuncName = true
}

if intfName := checker.NameForMethods(mm); intfName != "" {
fmt.Fprintf(w, " %s: %s\n", param, intfName)
if pkg, intfName := checker.NameForMethods(mm); intfName != "" {
pkgpath := pkg.PkgPath
if strings.ContainsAny(pkgpath, "./") {
pkgpath = fmt.Sprintf(`"%s"`, pkgpath)
}
fmt.Fprintf(w, " %s: %s.%s\n", param, pkgpath, intfName)
continue
}

methods := maps.Keys(tuple.M[param])
methods := slices.Collect(maps.Keys(tuple.M[param]))
sort.Strings(methods)
fmt.Fprintf(w, " %s: %v\n", param, methods)
}
Expand Down Expand Up @@ -116,10 +122,11 @@ func showJSON(w io.Writer, checker decouple.Checker, tuples []decouple.Tuple) er
}
jp := jparam{
Name: param,
Methods: maps.Keys(mm),
Methods: slices.Collect(maps.Keys(mm)),
}
sort.Strings(jp.Methods)
if intfName := checker.NameForMethods(mm); intfName != "" {
if pkg, intfName := checker.NameForMethods(mm); intfName != "" {
jp.InterfacePkg = pkg.PkgPath
jp.InterfaceName = intfName
}
jt.Params = append(jt.Params, jp)
Expand Down Expand Up @@ -149,5 +156,6 @@ type jtuple struct {
type jparam struct {
Name string
Methods []string `json:",omitempty"`
InterfacePkg string `json:",omitempty"`
InterfaceName string `json:",omitempty"`
}
4 changes: 2 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func (a *analyzer) debugf(format string, args ...any) {
return
}
s := fmt.Sprintf(format, args...)
strings.TrimRight(s, "\r\n")
s = strings.TrimRight(s, "\r\n")
if a.level > 0 {
fmt.Fprintf(os.Stderr, strings.Repeat(" ", a.level))
fmt.Fprint(os.Stderr, strings.Repeat(" ", a.level))
}
fmt.Fprintln(os.Stderr, s)
}
Loading