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 generics in reflect mode #136

Closed
wants to merge 1 commit into from
Closed

add support for generics in reflect mode #136

wants to merge 1 commit into from

Conversation

MartinForReal
Copy link

Parse TypeParameters from type name only when "[]" is present in type name.
related: #128

@CLAassistant
Copy link

CLAassistant commented Jan 5, 2024

CLA assistant check
All committers have signed the CLA.

@MartinForReal MartinForReal changed the title add support for generics in reflex mode add support for generics in reflect mode Jan 5, 2024
@MartinForReal
Copy link
Author

@r-hang PTAL. Thanks!

@bcho
Copy link
Contributor

bcho commented Jan 5, 2024

Does your implementation work for cases like

  • foo.T[int[], bool, string[]]
  • foo.T[bool[], context.Context[], github.com/foo/bar.X[], int[]]

@bcho
Copy link
Contributor

bcho commented Jan 5, 2024

also see: #97

if typeName := t.Name(); strings.ContainsAny(typeName, "[]") {
var ts []Type
startIndex := strings.Index(typeName, "[")
endIndex := strings.Index(typeName, "]")
Copy link
Contributor

@bcho bcho Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Golang's type parameter can be a slice X[] or another type parameter, therefore, I am afraid the assumption here for unwrapping [] is incorrect giving case like foo.bar[baz[]].

FWIW, since golang's reflection doesn't provide the type parameter details, we don't have a good way to tell what is the concrete type used. One way I was attempting to use is to use a recursive parser to extract the nested type parameters, but not that simple to write right... Another way is to wait for support from reflect...

Another workaround is to use source mode instead of reflect mode...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try the recursive parser. Thanks for the hint!

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.

3 participants