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

Is it possible to match implementors of generic interfaces? #476

Open
ian-h-chamberlain opened this issue May 2, 2024 · 1 comment
Open

Comments

@ian-h-chamberlain
Copy link

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:

  1. Implements("CustomCompare") meaning the type implements CustomCompare[T] for at least one T. Maybe could also use syntax like "CustomCompare[$_]" or something
  2. Implements("CustomCompare[$t]") meaning the type implements CustomCompare[T] for a specific T based on some other matcher, e.g. maybe some syntax like:
    m.Match(`$x == $y`).Where(m["x"].Type.Implements("CustomCompare[$y.Type]"))
  3. 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!

@quasilyte
Copy link
Owner

Adding generics support to any of the filters mentioned should be possible, but generics are not really supported as of yet as I didn't need to match anything like that.

Embedding the submatch into filter like that is not possible though.

m.Match(`$x == $y`).Where(m["x"].Type.Implements("CustomCompare[$y.Type]"))

To do that, we would need a more complicated way of interpreting the strings inside Implements. Almost evaluation-style levels. Instead of making that, I would recommend looking into custom filters direction that allows almost scripting-like abilities (and if it lacks something, it can be added).

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

2 participants