Skip to content

Commit 25c8e36

Browse files
committed
Add null
1 parent f1442a0 commit 25c8e36

File tree

4 files changed

+10
-24
lines changed

4 files changed

+10
-24
lines changed

docs/reference/schemas/config/functions/last.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ item in a sequence, the final stage in a deployment pipeline, or the last
2323
character in a configuration value.
2424

2525
For arrays, it returns the element at index `length - 1`. For strings, it
26-
returns the last character as a string. If the input is empty, an error is
27-
returned.
26+
returns the last character as a string.
2827

2928
## Examples
3029

@@ -207,16 +206,17 @@ Returns the last element of the array (preserving its original type) or the
207206
last character as a string. For arrays, the return type matches the element
208207
type. For strings, returns a single-character string.
209208

209+
If the input is an empty array, the function returns `null`. If the input is an
210+
empty string, the function returns an empty string.
211+
210212
```yaml
211-
Type: any | string
213+
Type: any | string | null
212214
```
213215

214216
## Errors
215217

216218
The function returns an error in the following cases:
217219

218-
- **Empty array**: The input array has no elements
219-
- **Empty string**: The input string has no characters
220220
- **Invalid type**: The argument is not an array or string
221221

222222
## Related functions

dsc/tests/dsc_functions.tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ Describe 'tests for function expressions' {
510510
@{ expression = "[last('hello')]"; expected = 'o' }
511511
@{ expression = "[last('a')]"; expected = 'a' }
512512
@{ expression = "[last(array('mixed'))]"; expected = 'mixed' }
513+
@{ expression = "[last(createArray())]"; expected = $null }
514+
@{ expression = "[last('')]"; expected = '' }
513515
) {
514516
param($expression, $expected)
515517

lib/dsc-lib/locales/en-us.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ invalidArgType = "Invalid argument type, argument must be an array or string"
346346
[functions.last]
347347
description = "Returns the last element of an array or last character of a string"
348348
invoked = "last function"
349-
emptyArray = "Cannot get last element of empty array"
350-
emptyString = "Cannot get last character of empty string"
351349
invalidArgType = "Invalid argument type, argument must be an array or string"
352350

353351
[functions.greater]

lib/dsc-lib/src/functions/last.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Function for Last {
2121
max_args: 1,
2222
accepted_arg_ordered_types: vec![vec![FunctionArgKind::Array, FunctionArgKind::String]],
2323
remaining_arg_accepted_types: None,
24-
return_types: vec![FunctionArgKind::String, FunctionArgKind::Number, FunctionArgKind::Array, FunctionArgKind::Object],
24+
return_types: vec![FunctionArgKind::String, FunctionArgKind::Number, FunctionArgKind::Array, FunctionArgKind::Object, FunctionArgKind::Null],
2525
}
2626
}
2727

@@ -30,14 +30,14 @@ impl Function for Last {
3030

3131
if let Some(array) = args[0].as_array() {
3232
if array.is_empty() {
33-
return Err(DscError::Parser(t!("functions.last.emptyArray").to_string()));
33+
return Ok(Value::Null);
3434
}
3535
return Ok(array[array.len() - 1].clone());
3636
}
3737

3838
if let Some(string) = args[0].as_str() {
3939
if string.is_empty() {
40-
return Err(DscError::Parser(t!("functions.last.emptyString").to_string()));
40+
return Ok(Value::String(String::new()));
4141
}
4242
return Ok(Value::String(string.chars().last().unwrap().to_string()));
4343
}
@@ -86,20 +86,6 @@ mod tests {
8686
assert_eq!(result.as_str(), Some("a"));
8787
}
8888

89-
#[test]
90-
fn empty_array() {
91-
let mut parser = Statement::new().unwrap();
92-
let result = parser.parse_and_execute("[last(createArray())]", &Context::new());
93-
assert!(result.is_err());
94-
}
95-
96-
#[test]
97-
fn empty_string() {
98-
let mut parser = Statement::new().unwrap();
99-
let result = parser.parse_and_execute("[last('')]", &Context::new());
100-
assert!(result.is_err());
101-
}
102-
10389
#[test]
10490
fn invalid_type_object() {
10591
let mut parser = Statement::new().unwrap();

0 commit comments

Comments
 (0)