Fix: Use Go Generics to Consolidate Subset functions#5444
Conversation
Signed-off-by: Shubham Singh <shubhsoch@gmail.com>
b5f52f5 to
36631fa
Compare
|
It seems you've hit some unit tests. |
|
hey @pfi79 , it's the the tests are running for so long and then failing, it might even be a race condition. |
When your pipe turns green locally, I'm ready to consider it further. So far, I've restarted your pr pipe 7-8 times and so far everything is bad. |
|
hey @pfi79 , i ran the tests on my PR replica, on shubhammms#3. I was earlier re running all jobs by commiting on it, that didn't work. Been trying to figure it out for a whole day.. |
|
YES. |
|
hey @pfi79 all are green. |
| // The empty set is a subset of all sets | ||
| if len(inner) == 0 { | ||
| return true | ||
| } | ||
|
|
||
| // If inner has more elements than outer, it cannot be a subset | ||
| if len(inner) > len(outer) { | ||
| return false | ||
| } | ||
|
|
||
| // If any element in inner is not in outer, it is not a subset |
There was a problem hiding this comment.
I ask you to restore the comments in the new solution or write your own.

Description
subsetOfGroups,subsetOfPolicies, andsubsetOfValueswere three identical functions differing only in map value type.A TODO had already existed since the code was written acknowledging the duplication, but noted the signatures "need to be different".
At the time, Go had no generics, so there was no clean way to unify them without losing type safety.
Go 1.18 introduced generics. This PR resolves the TODO by replacing all three functions with a single subsetOf[V any] function, letting the compiler infer the concrete type at each call site. ZERO behavior change - the logic is identical.
Now, 60 lines -> 10 lines of code
I have also added a new test case for partial error corresponding to the changes I've done.