-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add return expression * feat: add returns to `TypeDef` * feat(vrl): add possible return types to type information * feat(vrl): check return types in closure blocks * feat(vrl): handle explicit returns in closure blocks * fix: ui tests * Update changelog.d/712.feature.md Co-authored-by: Jesse Szwedko <jesse@szwedko.me> --------- Co-authored-by: Jesse Szwedko <jesse@szwedko.me>
- Loading branch information
Showing
34 changed files
with
553 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added the `return` expression as per [RFC 7496](https://github.com/vectordotdev/vector/blob/4671ccbf0a6359ef8b752fa99fae9eb9c60fdee5/rfcs/2023-02-08-7496-vrl-return.md). This expression can be used to terminate the VRL program early while still emitting a value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
lib/tests/tests/expressions/return/expressions_after_return.vrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# result: { "foo": true } | ||
|
||
.foo = true | ||
return . | ||
.foo = false | ||
abort "unreachable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# result: { "foo": true } | ||
|
||
.foo = true | ||
return . |
24 changes: 24 additions & 0 deletions
24
lib/tests/tests/expressions/return/return_bad_type_in_closure.vrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# DIAGNOSTICS | ||
# result: | ||
# | ||
# error[E122]: type mismatch in closure return type | ||
# ┌─ :2:37 | ||
# │ | ||
# 2 │ filter([1, 2, 3]) -> |_key, _value| { | ||
# │ ╭─────────────────────────────────────────^ | ||
# │ │ ╭───────────────────────────────────────' | ||
# │ │ │ ╭─────────────────────────────────────' | ||
# 3 │ │ │ │ return 0 | ||
# 4 │ │ │ │ true | ||
# 5 │ │ │ │ } | ||
# │ ╰─│─│─^ block returns invalid value type | ||
# │ ╰─│─' expected: boolean | ||
# │ ╰─' received: integer or boolean | ||
# │ | ||
# = see language documentation at https://vrl.dev | ||
# = try your code in the VRL REPL, learn more at https://vrl.dev/examples | ||
|
||
filter([1, 2, 3]) -> |_key, _value| { | ||
return 0 | ||
true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# result: { "foo": true, "bar": 42 } | ||
|
||
ret.foo = true | ||
ret.bar = 42 | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# result: 0 | ||
|
||
foo = 0 | ||
|
||
for_each({ "foo": "bar", "lorem": "ipsum" }) -> |_key, _value| { | ||
return 42 | ||
foo = 1 | ||
} | ||
|
||
foo |
10 changes: 10 additions & 0 deletions
10
lib/tests/tests/expressions/return/return_in_closure_after_closure.vrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# result: null | ||
|
||
for_each({ "foo": "bar", "lorem": "ipsum" }) -> |_key, _value| { | ||
|
||
for_each({ "foo": "bar", "lorem": "ipsum" }) -> |_key, _value| { | ||
true | ||
} | ||
|
||
return 42 | ||
} |
10 changes: 10 additions & 0 deletions
10
lib/tests/tests/expressions/return/return_in_closure_argument.vrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# result: [0, 1] | ||
|
||
foo = 0 | ||
bar = 0 | ||
|
||
for_each({ "foo": for_each({}) -> |_key, _value| { foo = 1; return true } }) -> |_key, _value| { | ||
bar = 1 | ||
} | ||
|
||
[foo, bar] |
9 changes: 9 additions & 0 deletions
9
lib/tests/tests/expressions/return/return_in_closure_in_closure.vrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# result: {} | ||
|
||
filter({ "bar": null }) -> |_key, _value| { | ||
for_each({ "foo": null }) -> |_key, _value| { | ||
return 0 | ||
} | ||
|
||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# result: true | ||
|
||
return true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# result: { | ||
# "a": { | ||
# "never": true | ||
# }, | ||
# "b": { | ||
# "bytes": true | ||
# }, | ||
# "b2": { | ||
# "bytes": true | ||
# }, | ||
# "c": { | ||
# "never": true | ||
# }, | ||
# "d": { | ||
# "object": {} | ||
# }, | ||
# "e": { | ||
# "null": true | ||
# }, | ||
# "f": { | ||
# "bytes": true, | ||
# "integer": true | ||
# }, | ||
# "g": { | ||
# "bytes": true | ||
# } | ||
# } | ||
|
||
x = "string" | ||
|
||
.a = type_def({return .}) | ||
.b = type_def({if false {return .} else {"string"}}) | ||
.b2 = type_def({if true {return .} else {"string"}}) | ||
.c = type_def({return {if false {abort} else {"string"}}}) | ||
.d = type_def({{} | {return .}}) | ||
.e = type_def({ if false {return .} }) | ||
.f = type_def({ | ||
x = "string" | ||
if false { | ||
return . | ||
} else { | ||
x = 3 | ||
} | ||
x | ||
}) | ||
.g = type_def({ | ||
return . | ||
"string" | ||
}) | ||
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1 @@ | ||
# object: { "message": "username=Hupsakee" } | ||
# result: { "username": "Hupsakee" } | ||
|
||
. = parse_groks!( | ||
.message, | ||
patterns: [ "%{TEST}" ], | ||
alias_sources: [ "lib/tests/tests/functions/parse_groks_alias_source.json" ] | ||
) | ||
# result: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# result: { "abort": true, "all": true, "any": true, "array": true, "bool": true, "boolean": true, "break": true, "continue": true, "do": true, "duration": true, "each": true, "else": true, "emit": true, "false": true, "floa": true, "for": true, "forall": true, "foreach": true, "if": true, "int": true, "integer": true, "iter": true, "loop": true, "null": true, "object": true, "regex": true, "return": true, "string": true, "timestamp": true, "travers": true, "true": true, "try": true, "undefine": true, "unless": true, "walk": true, "while": true } | ||
|
||
.abort = true | ||
.all = true | ||
.any = true | ||
.array = true | ||
.bool = true | ||
.boolean = true | ||
.break = true | ||
.continue = true | ||
.do = true | ||
.duration = true | ||
.each = true | ||
.else = true | ||
.emit = true | ||
.false = true | ||
.floa = true | ||
.for = true | ||
.forall = true | ||
.foreach = true | ||
.if = true | ||
.int = true | ||
.integer = true | ||
.iter = true | ||
.loop = true | ||
.null = true | ||
.object = true | ||
.regex = true | ||
.return = true | ||
.string = true | ||
.timestamp = true | ||
.travers = true | ||
.true = true | ||
.try = true | ||
.undefine = true | ||
.unless = true | ||
.walk = true | ||
.while = true | ||
|
||
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.