Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HandleFuncs can now be Type Methods as well. #2

Closed
wants to merge 2 commits into from
Closed
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 doc/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"text/template"

"github.com/adams-sarah/test2doc/doc/parse"
"github.com/ivaningrooves/test2doc/doc/parse"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"text/template"

"github.com/adams-sarah/test2doc/doc/parse"
"github.com/ivaningrooves/test2doc/doc/parse"
)

var (
Expand Down
36 changes: 31 additions & 5 deletions doc/parse/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,36 @@ func IsFuncInPkg(longFnName string) bool {
return doc != nil
}

// getShortFnName returns the name of the function, given
// longFnName of the form:
// github.com/adams-sarah/test2doc/example.GetWidget
// getShortFnName returns the name of the function
// without the package name so:
// github.com/user/project/package.method
// becomes
// method
// and
// github.com/user/project/package.(*type).method
// becomes
// type.method
func getShortFnName(longFnName string) string {
splitName := strings.Split(longFnName, ".")
return splitName[len(splitName)-1]

// drop anything before the last '/'
slashed := strings.Split(longFnName, "/")
last := slashed[len(slashed)-1]

// split the final part by period
dotted := strings.Split(last, ".")

// drop the first part which is the package name
dotted = dotted[1:]

// loop over and drop pointer references (*v) => v
for i, p := range dotted {
if len(p) > 3 {
if p[0:2] == "(*" && p[len(p)-1] == ')' {
p = p[2 : len(p)-1]
}
}
dotted[i] = p
}

return strings.Join(dotted, ".")
}
23 changes: 22 additions & 1 deletion doc/parse/package.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parse

import (
"fmt"
"go/doc"
"go/parser"
"go/token"
Expand All @@ -13,6 +14,7 @@ var (

// go/doc stores package 'Funcs' as a slice
// - we need to look up documentation by func name

funcsMap map[string]*doc.Func
)

Expand All @@ -31,10 +33,29 @@ func NewPackageDoc(dir string) (*doc.Package, error) {
}

func setDocFuncsMap(pkgDoc *doc.Package) {
funcsMap = make(map[string]*doc.Func, len(pkgDoc.Funcs))
typeFuncsMap := getPkgTypesFunctions(pkgDoc)
funcsMap = make(map[string]*doc.Func, len(pkgDoc.Funcs)+len(typeFuncsMap))

for _, fn := range pkgDoc.Funcs {
funcsMap[fn.Name] = fn
}

for k, fn := range typeFuncsMap {
funcsMap[k] = fn
}
}

func getPkgTypesFunctions(pkgDoc *doc.Package) map[string]*doc.Func {
result := make(map[string]*doc.Func)
for _, t := range pkgDoc.Types {
for _, f := range t.Methods {
if f.Doc != "" {
result[fmt.Sprintf("%s.%s", t.Name, f.Name)] = f
}
}
}

return result
}

func getPackageDoc(dir string) (*doc.Package, error) {
Expand Down
2 changes: 1 addition & 1 deletion doc/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/url"
"strings"

"github.com/adams-sarah/test2doc/doc/parse"
"github.com/ivaningrooves/test2doc/doc/parse"
)

type URL struct {
Expand Down
28 changes: 0 additions & 28 deletions example/README.md

This file was deleted.

182 changes: 0 additions & 182 deletions example/apiary.apib

This file was deleted.

6 changes: 0 additions & 6 deletions example/apib.tmpl

This file was deleted.

Loading