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

Having a match expression syntax would be useful for inline lookups #1096

Open
bruceg opened this issue Oct 25, 2024 · 0 comments
Open

Having a match expression syntax would be useful for inline lookups #1096

bruceg opened this issue Oct 25, 2024 · 0 comments
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: compiler Changes to the compiler vrl: parser Changes to the syntax parser vrl: syntax Changes to the syntax

Comments

@bruceg
Copy link
Member

bruceg commented Oct 25, 2024

I am writing some event mapping code that does a number of what are effectively inline table lookups. For example:

activity_name = if activity_id == 0 { "Unknown" } else
                if activity_id == 1 { "Open" } else
                if activity_id == 2 { "Close" } else
                if activity_id == 3 { "Reset" } else
                if activity_id == 4 { "Fail" } else
                if activity_id == 5 { "Refuse" } else
                if activity_id == 6 { "Traffic" } else
                { "Other" }

I cannot use an object with get for this, as sometimes the key is string, integer, or null, but objects only have string keys. Also, doing a quick command-line microbenchmark seems to indicate that the above scaffolding is faster than using get when the average branch chosen is less than about 7-8 when using strings.

It would be handy to have some way of expressing the above as a series of cases, analogous to C's switch statement or Rust's match expression:

activity_name = match activity_id {
  0 => "Unknown",
  1 => "Open",
  2 => "Close",
  3 => "Reset",
  4 => "Fail",
  5 => "Refuse",
  6 => "Traffic",
  else => "Other",
}

Obviously, in C you can't easily do string matches which would be highly desirable, and Rust's syntax is much more sophisticated than would likely be useful for VRL, so some balance would be desirable.

@bruceg bruceg added vrl: syntax Changes to the syntax vrl: compiler Changes to the compiler vrl: parser Changes to the syntax parser type: feature A value-adding code addition that introduce new functionality. labels Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: compiler Changes to the compiler vrl: parser Changes to the syntax parser vrl: syntax Changes to the syntax
Projects
None yet
Development

No branches or pull requests

1 participant