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

using GO generics #425

Open
shireenf-ibm opened this issue Oct 23, 2024 · 0 comments
Open

using GO generics #425

shireenf-ibm opened this issue Oct 23, 2024 · 0 comments

Comments

@shireenf-ibm
Copy link
Contributor

shireenf-ibm commented Oct 23, 2024

why could not success yet with using generics to avoid duplicates in eval/k8s/adminnetpol.go?

according to https://tip.golang.org/doc/go1.18#generics :

The Go compiler does not support accessing a struct field x.f where x is of type parameter type even if all types in the type parameter’s type set have a field f. We may remove this restriction in a future release.

till GO 1.23 this restriction is not removed yet.
for example:
replacing func egressRuleSelectsPeer(rulePeers []apisv1a.AdminNetworkPolicyEgressPeer, dst Peer) (bool, error) and
func ingressRuleSelectsPeer(rulePeers []apisv1a.AdminNetworkPolicyIngressPeer, src Peer) (bool, error)

with a func using generics like this :
func xgressRuleSelectsPeer[T apisv1a.AdminNetworkPolicyEgressPeer | apisv1a.AdminNetworkPolicyIngressPeer](rulePeers []T, dst Peer) (bool, error)

will fail with errors such as :
rulePeers[i].Namespaces undefined (type T has no field or method Namespaces)
a useful way to skip the errors is to define an interface with some Getter funcs to be implemented on the "inheriting types"
of the parameters.
but in this case : since our parameters are not of local types, we can not define new methods (like getters) on them;
not even with using aliases since then we'll need to copy values in calling funcs into the aliases and
this is not more efficient than current solutions.

@todo: if GO is upgraded to a release that does not has this restriction on types with same fields, replace following two "duplicated" funcs with one func that uses generics type
following two funcs egressRuleSelectsPeer and ingressRuleSelectsPeer:
(same for updateConnsIfEgressRuleSelectsPeer and updateConnsIfIngressRuleSelectsPeer and maybe other funcs)

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

No branches or pull requests

1 participant