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 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
19 changes: 15 additions & 4 deletions goextractors/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,43 @@ func TestDefinitionExtractor(t *testing.T) {
assert.Empty(t, issues)

defs := extractCtx.Definitions

key := "github.com/vorlif/testdata.M"
if assert.Contains(t, defs, key) {
assert.Contains(t, defs[key], "Test")
}

key = "github.com/vorlif/testdata.func github.com/vorlif/testdata.noop(sing string, plural string, context string, domain string)"
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")
assert.Contains(t, defs[key], "plural")
assert.Contains(t, defs[key], "context")
assert.Contains(t, defs[key], "domain")
}

key = "github.com/vorlif/testdata.func github.com/vorlif/testdata.multiNamesFunc(a string, b string)"
key = "github.com/vorlif/testdata.multiNamesFunc"
if assert.Contains(t, defs, key) {
assert.Contains(t, defs[key], "a")
assert.Contains(t, defs[key], "b")
}

key = "github.com/vorlif/testdata.func github.com/vorlif/testdata.noParamNames(string, string)"
key = "github.com/vorlif/testdata.noParamNames"
if assert.Contains(t, defs, key) {
assert.Contains(t, defs[key], "0")
assert.Contains(t, defs[key], "1")
}

key = "github.com/vorlif/testdata.func github.com/vorlif/testdata.variadicFunc(a string, vars ...string)"
key = "github.com/vorlif/testdata.variadicFunc"
if assert.Contains(t, defs, key) {
if assert.Contains(t, defs[key], "a") {
assert.Equal(t, 0, defs[key]["a"].FieldPos)
Expand Down
2 changes: 2 additions & 0 deletions goextractors/funccall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestFuncCallExtractor(t *testing.T) {
"inline function",

"constCtxMsg", "constCtxVal",

"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)
}
13 changes: 13 additions & 0 deletions testdata/project/funccall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func GenericFunc[V int64 | float64](log alias.Singular, i V) V {
return i
}

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 @@ -77,3 +85,8 @@ func builtInFunctions() {
t.DPGetf("domain-dp", "context-dp", "singular-dp")
t.DNPGetf("domain-dnp", "context-dnp", "msgid-dnp", "pluralid-dnp", 10)
}

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
14 changes: 12 additions & 2 deletions util/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ package util
import (
"fmt"
"go/types"
"strings"
)

func ObjToKey(obj types.Object) string {
switch v := obj.Type().(type) {
case *types.Signature:
return fmt.Sprintf("%s.%s", obj.Pkg().Path(), obj.String())
case *types.Named:
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())
}

return fmt.Sprintf("%s.%s", obj.Pkg().Path(), obj.Name())
case *types.Pointer:
return v.Elem().String()
Expand Down