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

Conversation

janartodesk
Copy link

Add support for extracting translations from generic struct fields and methods.

The struct field extraction logic is aligned with the function call's extraction logic, and the generic struct handling logic is added.

Redesign util.ObjectToKey() helper to support the generic struct use-case.

The funcCallExtractor targets *ast.CallExpr, whereas the definitionExtractorRunner *ast.FuncDecl AST nodes. Using the util.ObjectToKey() helper on these two targets returns inconsistent object keys for the same logical AST node.

Given a generic struct

type Struct[T any] struct{}

func (Struct[T]) Method(localize.Singular) {}

used as

(Struct[string]{}).Method("To be translated")

resolves to the following object key specification

  • github.com/vorlif/testdata/debug.func (github.com/vorlif/testdata/debug.GenericStruct[string]).Method(string) for the funcCallExtractor, but
  • github.com/vorlif/testdata/debug.func (github.com/vorlif/testdata/debug.GenericStruct[T]).Method(string) for the definitionExtractorRunner

resulting in translation targets not being mappable between different invocations, and omitted from the output.

The change in design introduces a simplified object keying pattern, which relies on symbol names being unique within a package context to generate globally unique keys, allowing more control over the key's format.

The *ast.CallExpr and *ast.FuncDecl type name formatting discrepancy is solved by reformatting the type name string. This simplistic approach is used due to the *types.Signature type's limited API which doesn't expose higher granularity typing information.

…values

Symbol names must be unique within a package, which allows using a more
generalized (and coincidentally less convoluted) keying pattern.
The solution supports this, but there is no test coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant