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

Introduce break for aborting iterations #1297

Open
enotspe opened this issue Feb 25, 2025 · 1 comment
Open

Introduce break for aborting iterations #1297

enotspe opened this issue Feb 25, 2025 · 1 comment
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: compiler Changes to the compiler

Comments

@enotspe
Copy link

enotspe commented Feb 25, 2025

based on this discussion

a nice feature would be to have a break instruction to finish an ongoing iteration, like when you iterate on a for_each instruction like in

      # Define a map of IP ranges and their classifications
      range_map = {
          "10.0.0.0/8": "private",
          "172.16.0.0/12": "private",
          "192.168.0.0/16": "private",
          "127.0.0.0/8": "loopback",
          "169.254.0.0/16": "link_local",
          "224.0.0.0/4": "multicast",
          "100.64.0.0/10": "carrier_nat",
          "192.0.0.0/24": "documentation",
          "192.0.2.0/24": "documentation",
          "198.51.100.0/24": "documentation",
          "203.0.113.0/24": "documentation",
          "240.0.0.0/4": "reserved",
          
          # IPv6 Ranges
          "::1/128": "loopback",
          "fc00::/7": "private",  # Unique Local Addresses (ULA)
          "fe80::/10": "link_local",
          "ff00::/8": "multicast",
          "100::/64": "discard",  # RFC 6666
          "2001:db8::/32": "documentation",  # RFC 3849
          "2002::/16": "6to4_tunnel",
          "::ffff:0:0/96": "ipv4_mapped",
          "::/128": "unspecified",
          "::ffff:0:0:0/96": "ipv4_translated",
          "2001::/23": "reserved",  # IANA reserved block
          "4000::/2": "reserved"   # Reserved for future use
      }
      
      # Default to "public"
      .source.ip_type = "public"
      
      # Check which range the IP belongs to
      for_each(range_map) -> |cidr, ip_type| {
          if ip_cidr_contains!(cidr, to_string!(.source.ip)) {
              .source.ip_type = ip_type
          }
      }

it would be more efficient for sure if there would be a way to break the for_each iteration

      for_each(range_map) -> |cidr, ip_type| {
          if ip_cidr_contains!(cidr, to_string!(.source.ip)) {
              .source.ip_type = ip_type
              break
          }
      }

but break is not supported and reserved for future use

error[E205]: reserved keyword
   ┌─ :46:9
   │
46 │         break
   │         ^^^^^
   │         │
   │         this identifier name is reserved for future use in the language
   │         use a different name instead
   │
   = see language documentation at https://vrl.dev
   = try your code in the VRL REPL, learn more at https://vrl.dev/examples
@JakubOnderka
Copy link
Contributor

Since 0.45 ip_cidr_contains function supports multiple cidr, so you can use array for cidr argument:

ip_cidr_contains!(["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"], "192.168.10.32")

This will not directly solve your problem, but at least it can make your code more efficient.

@pront pront added vrl: compiler Changes to the compiler type: feature A value-adding code addition that introduce new functionality. labels Feb 25, 2025
@pront pront changed the title break for aborting iterations Introduce break for aborting iterations Feb 25, 2025
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
Projects
None yet
Development

No branches or pull requests

3 participants