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 the generic struct support #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add generic struct and generic struct method support
  • Loading branch information
janartodesk committed May 9, 2024
commit 617279a9336d5115bb7177765825e454d0629373
10 changes: 10 additions & 0 deletions goextractors/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func TestDefinitionExtractor(t *testing.T) {
assert.Contains(t, defs[key], "Test")
}

key = "github.com/vorlif/testdata.methodStruct.Method"
if assert.Contains(t, defs, key) {
assert.Contains(t, defs[key], "0")
}

key = "github.com/vorlif/testdata.genericMethodStruct.Method"
if assert.Contains(t, defs, key) {
assert.Contains(t, defs[key], "0")
}

key = "github.com/vorlif/testdata.noop"
if assert.Contains(t, defs, key) {
assert.Contains(t, defs[key], "sing")
Expand Down
2 changes: 1 addition & 1 deletion goextractors/funccall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFuncCallExtractor(t *testing.T) {

"constCtxMsg", "constCtxVal",

"struct-method-call",
"struct-method-call", "generic-struct-method-call",
}
got := collectIssueStrings(issues)
assert.ElementsMatch(t, want, got)
Expand Down
21 changes: 12 additions & 9 deletions goextractors/structdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,26 @@ func (v structDefExtractor) Run(_ context.Context, extractCtx *extractors.Contex
return
}

var obj types.Object
var pkg *packages.Package
var ident *ast.Ident
switch val := node.Type.(type) {
case *ast.SelectorExpr:
pkg, obj = extractCtx.GetType(val.Sel)
if pkg == nil {
return
}
ident = val.Sel
case *ast.Ident:
pkg, obj = extractCtx.GetType(val)
if pkg == nil {
return
ident = val
case *ast.IndexExpr:
switch x := val.X.(type) {
case *ast.Ident:
ident = x
}
default:
return
}

pkg, obj := extractCtx.GetType(ident)
if pkg == nil {
return
}

if structAttr := extractCtx.Definitions.GetFields(util.ObjToKey(obj)); structAttr == nil {
return
}
Expand Down
2 changes: 2 additions & 0 deletions goextractors/structdef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestStructDefExtractor(t *testing.T) {
"struct msgid arr2", "struct plural arr2",
"A3", "B3", "C3",
"A4", "B4", "C4",
"GA3", "GB3", "GC3",
"GA4", "GB4", "GC4",
}
assert.ElementsMatch(t, want, got)
}
5 changes: 5 additions & 0 deletions testdata/project/funccall.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type methodStruct struct{}

func (methodStruct) Method(alias.Singular) {}

type genericMethodStruct[T any] struct{}

func (genericMethodStruct[T]) Method(alias.Singular) {}

func outerFuncDef() {
f := func(msgid alias.Singular, plural alias.Plural, context alias.Context, domain alias.Domain) {}

Expand Down Expand Up @@ -84,4 +88,5 @@ func builtInFunctions() {

func methodCall() {
(methodStruct{}).Method("struct-method-call")
(genericMethodStruct[string]{}).Method("generic-struct-method-call")
}
13 changes: 13 additions & 0 deletions testdata/project/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type OneLineStruct struct {
A, B, C localize.Singular
}

type OneLineGenericStruct[T any] struct {
A, B, C localize.Singular
}

func structLocalTest() []*sub.Sub {

// TRANSLATORS: Struct init
Expand All @@ -26,6 +30,15 @@ func structLocalTest() []*sub.Sub {

_ = OneLineStruct{"A4", "B4", "C4"}

// TRANSLATORS: Generic struct init
_ = OneLineGenericStruct[string]{
A: "GA3",
B: "GB3",
C: "GC3",
}

_ = OneLineGenericStruct[string]{"GA4", "GB4", "GC4"}

item := &sub.Sub{
Text: "local struct msgid",
Plural: "local struct plural",
Expand Down
8 changes: 8 additions & 0 deletions util/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ package util
import (
"fmt"
"go/types"
"strings"
)

func ObjToKey(obj types.Object) string {
switch v := obj.Type().(type) {
case *types.Signature:
if recv := v.Recv(); recv != nil {
// Strip out the generic type declaration from the type name.
// The ast.CallExpr reports its receiver as the actual type
// (e.g.`Generic[string]`), whereas the ast.FuncDecl on the
// same type as `Generic[T]`. The returned key values need
// to be consistent between different invocation patterns.
recv, _, _ := strings.Cut(recv.Type().String(), "[")

return fmt.Sprintf("%s.%s", recv, obj.Name())
}

Expand Down