Evaluate char literals in pipeline rules (#24295)

* evaluate char literals in pipeline rules

* CL

---------

Co-authored-by: Maxwell <98284293+kodjo-anipah@users.noreply.github.com>
This commit is contained in:
Patrick Mann
2025-11-24 15:33:22 +01:00
committed by GitHub
parent 15ceca34eb
commit d32ba6c9c0
4 changed files with 31 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
type = "f"
message = "Honor character literals in pipeline rules."
pulls = ["24295"]
issues = ["24247"]

View File

@@ -511,10 +511,18 @@ public class PipelineRuleParser {
exprs.put(ctx, expr);
}
/**
* Handle chars like strings.
* There is no dedicated char type for Graylog rules and, strictly speaking, we should error out. But we have
* been silently allowing (and ignoring) chars for 10 years.To not break existing rules, we continue to allow
* them. Except now we actually honor the expression value.
*/
@Override
public void exitChar(RuleLangParser.CharContext ctx) {
// TODO
super.exitChar(ctx);
final String text = unescape(unquote(ctx.getText(), '\''));
final StringExpression expr = new StringExpression(ctx.getStart(), text);
log.trace("CHAR: ctx {} => {}", ctx, expr);
exprs.put(ctx, expr);
}
@Override

View File

@@ -266,6 +266,16 @@ class PipelineRuleParserTest extends BaseParserTest {
assertTrue(actionsTriggered.get());
}
@Test
void charLiteral() {
final Rule rule = parseRuleWithOptionalCodegen();
Message message = messageFactory.createMessage("hello test", "source", DateTime.now(DateTimeZone.UTC));
final Message processedMsg = evaluateRule(rule, message);
assertNotNull(processedMsg);
assertEquals("x", processedMsg.getField("x"));
}
@Test
void typedFieldAccess() throws Exception {
final Rule rule = parseRuleWithOptionalCodegen();

View File

@@ -0,0 +1,6 @@
rule "char_literal"
when
true
then
set_field(field: "x", value: 'x');
end