func.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2019 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package digreflect
  21. import (
  22. "fmt"
  23. "net/url"
  24. "reflect"
  25. "runtime"
  26. "strings"
  27. )
  28. // Func contains runtime information about a function.
  29. type Func struct {
  30. // Name of the function.
  31. Name string
  32. // Name of the package in which this function is defined.
  33. Package string
  34. // Path to the file in which this function is defined.
  35. File string
  36. // Line number in the file at which this function is defined.
  37. Line int
  38. }
  39. // String returns a string representation of the function.
  40. func (f *Func) String() string {
  41. return fmt.Sprint(f)
  42. }
  43. // Format implements fmt.Formatter for Func, printing a single-line
  44. // representation for %v and a multi-line one for %+v.
  45. func (f *Func) Format(w fmt.State, c rune) {
  46. if w.Flag('+') && c == 'v' {
  47. // "path/to/package".MyFunction
  48. // path/to/file.go:42
  49. fmt.Fprintf(w, "%q.%v", f.Package, f.Name)
  50. fmt.Fprintf(w, "\n\t%v:%v", f.File, f.Line)
  51. } else {
  52. // "path/to/package".MyFunction (path/to/file.go:42)
  53. fmt.Fprintf(w, "%q.%v (%v:%v)", f.Package, f.Name, f.File, f.Line)
  54. }
  55. }
  56. // InspectFunc inspects and returns runtime information about the given
  57. // function.
  58. func InspectFunc(function interface{}) *Func {
  59. fptr := reflect.ValueOf(function).Pointer()
  60. f := runtime.FuncForPC(fptr)
  61. pkgName, funcName := splitFuncName(f.Name())
  62. fileName, lineNum := f.FileLine(fptr)
  63. return &Func{
  64. Name: funcName,
  65. Package: pkgName,
  66. File: fileName,
  67. Line: lineNum,
  68. }
  69. }
  70. const _vendor = "/vendor/"
  71. func splitFuncName(function string) (pname string, fname string) {
  72. if len(function) == 0 {
  73. return
  74. }
  75. // We have something like "path.to/my/pkg.MyFunction". If the function is
  76. // a closure, it is something like, "path.to/my/pkg.MyFunction.func1".
  77. idx := 0
  78. // Everything up to the first "." after the last "/" is the package name.
  79. // Everything after the "." is the full function name.
  80. if i := strings.LastIndex(function, "/"); i >= 0 {
  81. idx = i
  82. }
  83. if i := strings.Index(function[idx:], "."); i >= 0 {
  84. idx += i
  85. }
  86. pname, fname = function[:idx], function[idx+1:]
  87. // The package may be vendored.
  88. if i := strings.Index(pname, _vendor); i > 0 {
  89. pname = pname[i+len(_vendor):]
  90. }
  91. // Package names are URL-encoded to avoid ambiguity in the case where the
  92. // package name contains ".git". Otherwise, "foo/bar.git.MyFunction" would
  93. // mean that "git" is the top-level function and "MyFunction" is embedded
  94. // inside it.
  95. if unescaped, err := url.QueryUnescape(pname); err == nil {
  96. pname = unescaped
  97. }
  98. return
  99. }