Skip to content

Commit 0cdb87e

Browse files
committed
refactor: return package pointer
1 parent dcaf2db commit 0cdb87e

4 files changed

Lines changed: 23 additions & 55 deletions

File tree

cmd/pkgdmp/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
log.Fatal(err)
3737
}
3838

39-
parsed := make([]pkgdmp.Package, 0, len(unparsed))
39+
parsed := make([]*pkgdmp.Package, 0, len(unparsed))
4040

4141
for _, uPkg := range unparsed {
4242
pkg, err := pkgParser.Package(doc.New(uPkg, "", doc.AllDecls))
@@ -73,7 +73,7 @@ func getPackages(dirs []string) ([]*ast.Package, error) {
7373
return all, nil
7474
}
7575

76-
func printPackages(pkgs []pkgdmp.Package, cfg *cli.Config) error {
76+
func printPackages(pkgs []*pkgdmp.Package, cfg *cli.Config) error {
7777
if cfg.JSON {
7878
encoder := json.NewEncoder(os.Stdout)
7979
encoder.SetIndent("", " ")

entities.go

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,9 @@ import (
44
"fmt"
55
"go/format"
66
"io"
7-
"sort"
87
"strings"
98
)
109

11-
// Identifier represents a program entity such as a function, struct, method,
12-
// interface, etc. that can be exported by a package or kept private.
13-
type Identifier interface {
14-
Ident() string
15-
IsExported() bool
16-
}
17-
1810
// Package represents a go package containing functions and types such as
1911
// structs and interfaces.
2012
type Package struct {
@@ -24,14 +16,8 @@ type Package struct {
2416
Types []TypeDef `json:"types"`
2517
}
2618

27-
// OrderIdents arranges package entities, sorting them first by whether they are
28-
// exported or unexported, and then alphabetically.
29-
func (p Package) OrderIdents() {
30-
p.orderFuncs()
31-
}
32-
3319
// Source returns the formatted package signature source.
34-
func (p Package) Source() (string, error) {
20+
func (p *Package) Source() (string, error) {
3521
formatted, err := format.Source([]byte(p.String()))
3622
if err != nil {
3723
return "", fmt.Errorf("formatting source: %w", err)
@@ -41,7 +27,7 @@ func (p Package) Source() (string, error) {
4127
}
4228

4329
// String returns the unformatted package signature source.
44-
func (p Package) String() string {
30+
func (p *Package) String() string {
4531
var b strings.Builder
4632

4733
if p.Doc != "" {
@@ -63,17 +49,6 @@ func (p Package) String() string {
6349
return b.String()
6450
}
6551

66-
func (p Package) orderFuncs() {
67-
sortFn := func(i, j int) bool {
68-
funcI := p.Funcs[i]
69-
funcJ := p.Funcs[j]
70-
71-
return funcI.Less(funcJ)
72-
}
73-
74-
sort.SliceStable(p.Funcs, sortFn)
75-
}
76-
7752
// Func represents a function or a struct method if the Receiver field contains
7853
// a pointer to a [FuncReceiver].
7954
type Func struct {
@@ -87,30 +62,15 @@ type Func struct {
8762
}
8863

8964
// Ident returns the function's name.
90-
//
91-
// Part of the [Identifier] interface implementation.
9265
func (f Func) Ident() string {
9366
return f.Name
9467
}
9568

9669
// IsExported returns true if the function is exported.
97-
//
98-
// Part of the [Identifier] interface implementation.
9970
func (f Func) IsExported() bool {
10071
return isExportedIdent(f.Name)
10172
}
10273

103-
// Less returns true if Func must sort before other Func.
104-
func (f Func) Less(other Func) bool {
105-
if f.IsExported() && !other.IsExported() {
106-
return true
107-
} else if !f.IsExported() && other.IsExported() {
108-
return false
109-
}
110-
111-
return f.Ident() > other.Ident()
112-
}
113-
11474
// String returns the function signature code.
11575
func (f Func) String() string {
11676
var b strings.Builder
@@ -136,6 +96,7 @@ func (f Func) String() string {
13696
return b.String()
13797
}
13898

99+
// TypeDef represents a type definition.
139100
type TypeDef struct {
140101
Type string `json:"type"`
141102
Name string `json:"name"`
@@ -151,6 +112,17 @@ type TypeDef struct {
151112
Methods []Func `json:"methods,omitempty"`
152113
}
153114

115+
// Ident returns the type definition's name.
116+
func (td TypeDef) Ident() string {
117+
return td.Name
118+
}
119+
120+
// IsExported returns true if the type definition is exported.
121+
func (td TypeDef) IsExported() bool {
122+
return isExportedIdent(td.Name)
123+
}
124+
125+
// String returns the type definition code.
154126
func (td TypeDef) String() string {
155127
var b strings.Builder
156128

@@ -191,15 +163,11 @@ type Field struct {
191163
}
192164

193165
// Ident returns the name of the struct field.
194-
//
195-
// Part of the [Identifier] interface implementation.
196166
func (sf Field) Ident() string {
197167
return sf.Names[0]
198168
}
199169

200170
// IsExported returns true if the struct field is exported.
201-
//
202-
// Part of the [Identifier] interface implementation.
203171
func (sf Field) IsExported() bool {
204172
return isExportedIdent(sf.Names[0])
205173
}

parse.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ func NewParser(opts ParserOptions) *Parser {
3333
}
3434

3535
// Package parses dPkg to a simplified [Package].
36-
func (p *Parser) Package(dPkg *doc.Package) (Package, error) {
37-
pkg := Package{
36+
func (p *Parser) Package(dPkg *doc.Package) (*Package, error) {
37+
pkg := &Package{
3838
Name: dPkg.Name,
3939
Doc: p.mkDoc(dPkg.Doc),
4040
}
4141

42-
if err := p.parseFuncs(&pkg, dPkg.Funcs); err != nil {
43-
return Package{}, fmt.Errorf("parsing functions: %w", err)
42+
if err := p.parseFuncs(pkg, dPkg.Funcs); err != nil {
43+
return nil, fmt.Errorf("parsing functions: %w", err)
4444
}
4545

46-
if err := p.parseTypes(&pkg, dPkg.Types); err != nil {
47-
return Package{}, fmt.Errorf("parsing types: %w", err)
46+
if err := p.parseTypes(pkg, dPkg.Types); err != nil {
47+
return nil, fmt.Errorf("parsing types: %w", err)
4848
}
4949

5050
return pkg, nil

parse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (tc *parserTestCase) pkgDoc(tb testing.TB) *doc.Package {
199199
return doc.New(pkg, "", doc.AllDecls)
200200
}
201201

202-
func (tc *parserTestCase) compareGolden(tb testing.TB, pkg pkgdmp.Package) {
202+
func (tc *parserTestCase) compareGolden(tb testing.TB, pkg *pkgdmp.Package) {
203203
tb.Helper()
204204

205205
actual, err := pkg.Source()

0 commit comments

Comments
 (0)