-
Notifications
You must be signed in to change notification settings - Fork 41
Description
Here's a small example of what I'm trying to do. Given this code:
type CustomCompare[T] interface {
comparable
GetT() T
}
type impl struct{}
func (impl) GetT() int {
return 0
}
func main() {
x := impl{}
y := impl{}
fmt.Println(x == y)
}I'd like to write a rule kinda like this:
func CustomComparer(m dsl.Matcher) {
m.Match(`$x == $y`).
Where(m["x"].Type.Implements("CustomCompare"))
}I'd imagine this would require some support from the DSL, since I'm not specifying T in my rule and in practice impl only implements CustomCompare[int]... There might be multiple possible use cases, although for my purposes I think either of the first two would be fine:
Implements("CustomCompare")meaning the type implementsCustomCompare[T]for at least oneT. Maybe could also use syntax like"CustomCompare[$_]"or somethingImplements("CustomCompare[$t]")meaning the type implementsCustomCompare[T]for a specificTbased on some other matcher, e.g. maybe some syntax like:m.Match(`$x == $y`).Where(m["x"].Type.Implements("CustomCompare[$y.Type]"))
Implements("CustomCompare[int]")- implements a concrete interface
It might also be useful to have HasMethod work in a similar way, although I'm not sure of the technical differences between how those are implemented.
Is something like this possible? Or if not currently, is there any workaround I could use for a less-precise way of getting similar functionality?
If not, where in the code would I look if I were to try adding this feature?
Thanks in advance! go-ruleguard has been a big help in my organization so I appreciate the work you've done here!