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

Add support for import alias #18

Merged
merged 1 commit into from
Oct 19, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func IgnoreHandler(w http.ResponseWriter, req *http.Request) {
- The function/method signature has `*http.Request`.
- `defer newrelic.FromContext(req.Context()).StartSegment("func_name").End()`
- [x] Support any variable name of `context.Context`/`*http.Request`.
- [x] Support import alias of `"context"`/`"net/http"`.
- [x] Use function/method name to segment name.
- [x] This processing is recursively repeated.
- [x] Able to ignore function/method by `nrseg:ignore` comment.
Expand Down
2 changes: 1 addition & 1 deletion inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (nrseg *nrseg) Inspect(filename string, src []byte) error {
return false
}
if fd.Body != nil && len(fd.Body.List) > 0 {
if _, t := parseParams(fd.Type); !(t == TypeContext || t == TypeHttpRequest) {
if _, t := parseParams(f.Imports, fd.Type); !(t == TypeContext || t == TypeHttpRequest) {
return false
}
if !existFromContext(pkg, fd.Body.List[0]) {
Expand Down
27 changes: 21 additions & 6 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Process(filename string, src []byte) ([]byte, error) {
}
if fd.Body != nil && len(fd.Body.List) > 0 {
sn := getSegName(fd)
vn, t := parseParams(fd.Type)
vn, t := parseParams(f.Imports, fd.Type)
var ds ast.Stmt
switch t {
case TypeContext:
Expand Down Expand Up @@ -225,19 +225,24 @@ const (
TypeUnknown = "Unknown"
)

func parseParams(t *ast.FuncType) (string, string) {
var types = map[string]string{
TypeContext: "\"context\"",
TypeHttpRequest: "\"net/http\"",
}

func parseParams(is []*ast.ImportSpec, t *ast.FuncType) (string, string) {
var cname = getImportName(is, TypeContext)
var hname = getImportName(is, TypeHttpRequest)
n, typ := "", TypeUnknown
for _, f := range t.Params.List {
if se, ok := f.Type.(*ast.SelectorExpr); ok {
// TODO: support named import
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == "context" && se.Sel.Name == "Context" {
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == cname && se.Sel.Name == "Context" {
return f.Names[0].Name, TypeContext
}
}
if se, ok := f.Type.(*ast.StarExpr); ok {
if se, ok := se.X.(*ast.SelectorExpr); ok {
// TODO: support named import
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == "http" && se.Sel.Name == "Request" {
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == hname && se.Sel.Name == "Request" {
n = f.Names[0].Name
typ = TypeHttpRequest
}
Expand All @@ -246,3 +251,13 @@ func parseParams(t *ast.FuncType) (string, string) {
}
return n, typ
}

func getImportName(is []*ast.ImportSpec, typ string) string {
var def = strings.Replace(strings.Split(typ, ".")[0], "*", "", 1)
for _, i := range is {
if i.Name != nil && i.Path != nil && i.Path.Value == types[typ] {
return i.Name.Name
}
}
return def
}
126 changes: 125 additions & 1 deletion process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ import (
)

func Hoge(ctx context.Context) {}
`,
wantName: "ctx", wantType: TypeContext,
},
{
name: "NamedContext",
src: `
package main

import (
c "context"
)

func Hoge(ctx c.Context) {}
`,
wantName: "ctx", wantType: TypeContext,
},
Expand All @@ -344,6 +357,20 @@ import (
)

func SampleHandler(w http.ResponseWriter, req *http.Request) {}
`,
wantName: "req", wantType: TypeHttpRequest,
},

{
name: "NamedHttp",
src: `
package main

import (
h "net/http"
)

func SampleHandler(w h.ResponseWriter, req *h.Request) {}
`,
wantName: "req", wantType: TypeHttpRequest,
},
Expand All @@ -364,7 +391,7 @@ func SampleHandler(w http.ResponseWriter, req *http.Request) {}
break
}
}
name, gtype := parseParams(decl.Type)
name, gtype := parseParams(f.Imports, decl.Type)
if name != tt.wantName {
t.Errorf("parseParams() name = %q, want %q", name, tt.wantName)
}
Expand All @@ -374,3 +401,100 @@ func SampleHandler(w http.ResponseWriter, req *http.Request) {}
})
}
}

func Test_getImportName(t *testing.T) {
tests := []struct {
name string
src string
typ string
want string
}{
{
name: "Context",
src: `
package main

import (
"context"
)

func Hoge() {}`,
typ: TypeContext, want: "context",
},
{
name: "NoContext",
src: `
package main

import (
"net/http"
)

func Hoge() {}`,
typ: TypeContext, want: "context",
},
{
name: "NamedContext",
src: `
package main

import (
c "context"
)

func Hoge() {}`,
typ: TypeContext, want: "c",
},
{
name: "Http",
src: `
package main

import (
"net/http"
)

func Hoge() {}`,
typ: TypeHttpRequest, want: "http",
},
{
name: "NoHttp",
src: `
package main

import (
"context"
)

func Hoge() {}`,
typ: TypeHttpRequest, want: "http",
},
{
name: "NamedHttp",
src: `
package main

import (
myHttp "net/http"
)

func Hoge() {}`,
typ: TypeHttpRequest, want: "myHttp",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, "sample.go", tt.src, parser.Mode(0))
if err != nil {
t.Fatal(err)
}
if got := getImportName(f.Imports, tt.typ); got != tt.want {
t.Errorf("getImportName() = %v, want %v", got, tt.want)
}
})
}
}