Skip to content
Open
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
13 changes: 11 additions & 2 deletions internal/injector/aspect/join/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,24 @@ func (i *declarationOf) Matches(ctx context.AspectContext) bool {

switch node := ctx.Node().(type) {
case *dst.FuncDecl:
return node.Name != nil && node.Name.Name == i.Name
// Methods share their name space with the type they belong to, not with the package.
return node.Recv == nil && node.Name != nil && node.Name.Name == i.Name
case *dst.ValueSpec:
if parent := ctx.Chain().Parent(); parent == nil {
parent := ctx.Chain().Parent()
if parent == nil {
// No parent, this is almost certainly a syntax error...
return false
} else if _, isGenDecl := parent.Node().(*dst.GenDecl); !isGenDecl {
// Parent isn't a GenDecl, so this is not a top-level declaration.
return false
}
// A GenDecl is also what wraps declarations made inside of a function body (through a
// DeclStmt), so make sure that this one sits directly in the file.
if grandparent := parent.Parent(); grandparent == nil {
return false
} else if _, isFile := grandparent.Node().(*dst.File); !isFile {
return false
}
for _, name := range node.Names {
if name.Name == i.Name {
return true
Expand Down
140 changes: 140 additions & 0 deletions internal/injector/aspect/join/declaration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package join

import (
"go/parser"
"go/token"
"strings"
"testing"

"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/dave/dst/dstutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/DataDog/orchestrion/internal/injector/aspect/context"
)

// declarationContext is the part of [context.AspectContext] that the declaration-of join point reads.
type declarationContext struct {
context.AspectContext
chain *context.NodeChain
importPath string
}

func (c declarationContext) Chain() *context.NodeChain { return c.chain }
func (c declarationContext) Node() dst.Node { return c.chain.Node() }
func (c declarationContext) ImportPath() string { return c.importPath }

func TestDeclarationOf(t *testing.T) {
const src = `package pkg

type T struct{}

var Name = 1

const Limit = 2

var (
First, Second = 3, 4
)

func (T) Method() {}

func (*T) Other() {}

func Function() {
var Local = 5
const LocalConst = 6
_, _ = Local, LocalConst
{
var Name = 7
_ = Name
}
_ = func() {
var Limit = 8
_ = Limit
}
}

// Same is both a top-level function and the name of a method.
func Same() {}

func (T) Same() {}
`

file, err := decorator.ParseFile(token.NewFileSet(), "pkg.go", src, parser.ParseComments)
require.NoError(t, err)

// find describes every node that the join point matches.
find := func(point *declarationOf) []string {
var (
chain *context.NodeChain
matches []string
)
dstutil.Apply(file,
func(cursor *dstutil.Cursor) bool {
if cursor.Node() == nil {
return false
}
chain = chain.Child(cursor)
return true
},
func(*dstutil.Cursor) bool {
if point.Matches(declarationContext{chain: chain, importPath: "example.com/pkg"}) {
matches = append(matches, describe(chain.Node()))
}
chain = chain.Parent()
return true
},
)
return matches
}

const pkg = "example.com/pkg"
tests := map[string]struct {
point *declarationOf
want []string
}{
"variable": {DeclarationOf(pkg, "Name"), []string{"value Name"}},
"grouped variable": {DeclarationOf(pkg, "Second"), []string{"value First, Second"}},
"constant": {DeclarationOf(pkg, "Limit"), []string{"value Limit"}},
"function": {DeclarationOf(pkg, "Function"), []string{"func Function"}},
"function and method": {DeclarationOf(pkg, "Same"), []string{"func Same"}},
"value receiver method": {DeclarationOf(pkg, "Method"), nil},
"pointer receiver method": {DeclarationOf(pkg, "Other"), nil},
"local variable": {DeclarationOf(pkg, "Local"), nil},
"local constant": {DeclarationOf(pkg, "LocalConst"), nil},
"other package": {DeclarationOf("example.com/other", "Name"), nil},
"type declaration": {DeclarationOf(pkg, "T"), nil},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want, find(tc.point))
})
}
}

func describe(node dst.Node) string {
switch node := node.(type) {
case *dst.FuncDecl:
if node.Recv != nil {
return "method " + node.Name.Name
}
return "func " + node.Name.Name
case *dst.ValueSpec:
names := make([]string, len(node.Names))
for i, name := range node.Names {
names[i] = name.Name
}
return "value " + strings.Join(names, ", ")
default:
return "other"
}
}