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

Support for string constexpr evaluation #444

Open
oschwald opened this issue Apr 27, 2023 · 2 comments
Open

Support for string constexpr evaluation #444

oschwald opened this issue Apr 27, 2023 · 2 comments

Comments

@oschwald
Copy link

As discussed in #443, I would like the ability to do something like the following in a Where:

m["msg"].Value.String().Matches(`^[A-Z]`)

Right now, it is possible to approximate this using Text.Matches, e.g.:

m["msg"].Text.Matches(`^"[A-Z]`)

However, this has the disadvantage of requiring you to handle the different kinds of string literals (or, more likely, only handle the most common case as above).

Thank you!

@ian-h-chamberlain
Copy link

If this were implemented, I would expect it to work similar to Int(), e.g.

// ExprValue describes a compile-time computable value of a matched expr.
type ExprValue struct{}

// Int returns compile-time computable int value of the expression.
// If value can't be computed, condition will fail.
func (ExprValue) Int() int { return intResult }


// Int returns compile-time computable int value of the expression.
// If value can't be computed, condition will fail.
func (ExprValue) String() string { return stringResult }

I think your example would be a little trickier in that case, since it would have to be something more like

matched, err := regexp.Match(m["msg"].Value.String())
m.Match["$msg"].Where(err != nil && matched)

I'm not sure the DSL is really capable of handling something like that, without a lot of special casing...

I was hoping for something similar to this for Suggest, although I think a similar problem presents itself, e.g.

m.Match["$msg"].Report("error").Suggest(strings.ToTitle(m["msg"].Value.String()))

Once again the use of arbitrary strings functions seems problematic, but maybe ruleguard could provide a small known set of string manipulation operations or something... Still seems like a big feature though.

@quasilyte
Copy link
Owner

quasilyte commented Feb 1, 2024

The String() method should return the String type (not string) that has utility methods like Matches.
The ruleguard already has the regexp support for strings in forms of methods, we need to allow some constant string to be operated in the same way.

DSL does not support the arbitrary code.
It's just a way to describe the rules, it's not the code to be executed.

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

3 participants