Skip to content

Commit

Permalink
Merge pull request #21 from alchemy/fix_gopath_callerframe
Browse files Browse the repository at this point in the history
Two fixes
  • Loading branch information
pkieltyka authored Nov 20, 2022
2 parents bab248e + 195a237 commit c3cb6e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 2 additions & 3 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"

Expand All @@ -14,7 +13,7 @@ import (
func BuildDoc(r chi.Routes) (Doc, error) {
d := Doc{}

goPath := os.Getenv("GOPATH")
goPath := getGoPath()
if goPath == "" {
return d, errors.New("docgen: unable to determine your $GOPATH")
}
Expand Down Expand Up @@ -83,7 +82,7 @@ func buildDocRouter(r chi.Routes) DocRouter {
func buildFuncInfo(i interface{}) FuncInfo {
fi := FuncInfo{}
frame := getCallerFrame(i)
goPathSrc := filepath.Join(os.Getenv("GOPATH"), "src")
goPathSrc := filepath.Join(getGoPath(), "src")

if frame == nil {
fi.Unresolvable = true
Expand Down
9 changes: 6 additions & 3 deletions funcinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docgen
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"runtime"
Expand All @@ -23,7 +22,7 @@ type FuncInfo struct {
func GetFuncInfo(i interface{}) FuncInfo {
fi := FuncInfo{}
frame := getCallerFrame(i)
goPathSrc := filepath.Join(os.Getenv("GOPATH"), "src")
goPathSrc := filepath.Join(getGoPath(), "src")

if frame == nil {
fi.Unresolvable = true
Expand Down Expand Up @@ -67,7 +66,11 @@ func GetFuncInfo(i interface{}) FuncInfo {
}

func getCallerFrame(i interface{}) *runtime.Frame {
pc := reflect.ValueOf(i).Pointer()
value := reflect.ValueOf(i)
if value.Kind() != reflect.Func {
return nil
}
pc := value.Pointer()
frames := runtime.CallersFrames([]uintptr{pc})
if frames == nil {
return nil
Expand Down
13 changes: 13 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package docgen

import (
"go/build"
"os"
)

func copyDocRouter(dr DocRouter) DocRouter {
var cloneRouter func(dr DocRouter) DocRouter
var cloneRoutes func(drt DocRoutes) DocRoutes
Expand Down Expand Up @@ -35,3 +40,11 @@ func copyDocRouter(dr DocRouter) DocRouter {

return cloneRouter(dr)
}

func getGoPath() string {
goPath := os.Getenv("GOPATH")
if goPath == "" {
goPath = build.Default.GOPATH
}
return goPath
}

0 comments on commit c3cb6e0

Please sign in to comment.