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

Fix TypeAs.UNWRAP_MAP_VALUE_OF and TypeAs.UNWRAP_MAP_KEY_OF #342

Merged
merged 1 commit into from Oct 14, 2022

Conversation

xRodney
Copy link

@xRodney xRodney commented Oct 7, 2022

Fixes #341

This PR adds a new reusable routine TypeCast, which is used to investigate type relations with respect to generic arguments (like improved isInstanceOf).

TypeCast is then used by three new utility methods to Collections: getCollectionElementType, getMapKeyType and getMapValueType. These are similar to TypeAs.UNWRAP_MAP_VALUE_OF & friends, but return Optional.empty() when the supplied type is not a Collection / Map (as opposed to the supplied type itself). In fact, the UNWRAP_* functions now delegate to Collections and therefore now return more accurate results.

Last but not least, I have identified a new public routine TypeArguments.getGenericArgumentsMappings(ClassRef ref, TypeDef definition). This creates a mapping from generic types on a TypeDef to their instantiation on ClassRef. This code has been used at multiple places both in sundrio, but also in kubernetes-client. So I think it deserves a common implementation.

@xRodney
Copy link
Author

xRodney commented Oct 8, 2022

The build was failing on main also. I rebased this on #344 to fix that.

@xRodney xRodney marked this pull request as ready for review October 8, 2022 15:37
…ong results for multimaps (& other more complicated generic references)

Fixes sundrio#341

This PR adds a new reusable routine `TypeCast`, which is used to investigate type relations with respect to generic arguments (like improved `isInstanceOf`).

`TypeCast` is then used by three new utility methods to `Collections`: `getCollectionElementType`, `getMapKeyType` and `getMapValueType`.
These are similar to `TypeAs.UNWRAP_MAP_VALUE_OF` & friends, but return `Optional.empty()` when the supplied type is not a Collection / Map (as opposed to the supplied type itself).
In fact, the `UNWRAP_*` functions now delegate to `Collections` and therefore now return more accurate results.

Last but not least, I have identified a new public routine `TypeArguments.getGenericArgumentsMappings(ClassRef ref, TypeDef definition)`.
This creates a mapping from generic types on a TypeDef to their instantiation on ClassRef. This code has been used at multiple places both in sundrio, but also in kubernetes-client. So I think it deserves a common implementation.
Copy link
Collaborator

@iocanel iocanel left a comment

Choose a reason for hiding this comment

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

LGTM

@iocanel iocanel merged commit 1262be0 into sundrio:main Oct 14, 2022
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.

TypeAs.UNWRAP_MAP_VALUE_OF and UNWRAP_MAP_KEY_OF return wrong results for multimaps
2 participants