Skip to content

Commit 2482bbd

Browse files
authored
Merge pull request #1028 from SteveL-MSFT/refactor-functions
Refactor functions on how they return metadata
2 parents 751b908 + 41f29fb commit 2482bbd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+703
-837
lines changed

dsc/locales/en-us.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ tableHeader_functionName = "Function"
115115
tableHeader_functionCategory = "Category"
116116
tableHeader_minArgs = "MinArgs"
117117
tableHeader_maxArgs = "MaxArgs"
118-
tableHeader_argTypes = "ArgTypes"
118+
tableHeader_argTypes = "ReturnTypes"
119119
invalidFunctionFilter = "Invalid function filter"
120120
maxInt = "maxInt"
121121
invalidManifest = "Error in manifest for"

dsc/src/subcommand.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::resolve::{get_contents, Include};
66
use crate::resource_command::{get_resource, self};
77
use crate::tablewriter::Table;
88
use crate::util::{get_input, get_schema, in_desired_state, set_dscconfigroot, validate_json, write_object, DSC_CONFIG_ROOT, EXIT_DSC_ASSERTION_FAILED, EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_INVALID_INPUT, EXIT_JSON_ERROR};
9-
use dsc_lib::functions::AcceptedArgKind;
9+
use dsc_lib::functions::FunctionArgKind;
1010
use dsc_lib::{
1111
configure::{
1212
config_doc::{
@@ -697,12 +697,12 @@ fn list_functions(functions: &FunctionDispatcher, function_name: Option<&String>
697697
write_table = true;
698698
}
699699
let mut include_separator = false;
700-
let accepted_arg_types= [
701-
(AcceptedArgKind::Array, "a"),
702-
(AcceptedArgKind::Boolean, "b"),
703-
(AcceptedArgKind::Number, "n"),
704-
(AcceptedArgKind::String, "s"),
705-
(AcceptedArgKind::Object, "o"),
700+
let returned_types= [
701+
(FunctionArgKind::Array, "a"),
702+
(FunctionArgKind::Boolean, "b"),
703+
(FunctionArgKind::Number, "n"),
704+
(FunctionArgKind::String, "s"),
705+
(FunctionArgKind::Object, "o"),
706706
];
707707

708708
let asterisks = String::from("*");
@@ -724,9 +724,9 @@ fn list_functions(functions: &FunctionDispatcher, function_name: Option<&String>
724724

725725
if write_table {
726726
// construct arg_types from '-' times number of accepted_arg_types
727-
let mut arg_types = "-".repeat(accepted_arg_types.len());
728-
for (i, (arg_type, letter)) in accepted_arg_types.iter().enumerate() {
729-
if function.accepted_arg_types.contains(arg_type) {
727+
let mut arg_types = "-".repeat(returned_types.len());
728+
for (i, (arg_type, letter)) in returned_types.iter().enumerate() {
729+
if function.return_types.contains(arg_type) {
730730
arg_types.replace_range(i..=i, letter);
731731
}
732732
}

dsc/tests/dsc_function_list.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Describe 'Tests for function list subcommand' {
1919
$out.name | Should -BeExactly 'resourceId'
2020
$out.minArgs | Should -Be 2
2121
$out.maxArgs | Should -Be 2
22-
$out.acceptedArgTypes | Should -Be @('String')
22+
$out.returnTypes | Should -Be @('String')
2323
$out.description | Should -Not -BeNullOrEmpty
2424
}
2525
}

dsc/tests/dsc_functions.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Describe 'tests for function expressions' {
155155
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
156156
if ($isError) {
157157
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.log -Raw)
158-
(Get-Content $TestDrive/error.log -Raw) | Should -Match 'Invalid item to find, must be a string or number'
158+
(Get-Content $TestDrive/error.log -Raw) | Should -Match 'accepted types are: String, Number'
159159
} else {
160160
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
161161
($out.results[0].result.actualState.output | Out-String) | Should -BeExactly ($expected | Out-String)

dsc_lib/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ argCountRequired = "Function '%{name}' requires between %{min_args} and %{max_ar
205205
noArrayArgs = "Function '%{name}' does not accept array arguments, accepted types are: %{accepted_args_string}"
206206
noBooleanArgs = "Function '%{name}' does not accept boolean arguments, accepted types are: %{accepted_args_string}"
207207
noNumberArgs = "Function '%{name}' does not accept number arguments, accepted types are: %{accepted_args_string}"
208+
noNullArgs = "Function '%{name}' does not accept null arguments, accepted types are: %{accepted_args_string}"
208209
noObjectArgs = "Function '%{name}' does not accept object arguments, accepted types are: %{accepted_args_string}"
209210
noStringArgs = "Function '%{name}' does not accept string arguments, accepted types are: %{accepted_args_string}"
210211

dsc_lib/src/functions/add.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::DscError;
55
use crate::configure::context::Context;
6-
use crate::functions::{AcceptedArgKind, Function, FunctionCategory};
6+
use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata};
77
use rust_i18n::t;
88
use serde_json::Value;
99
use tracing::debug;
@@ -12,24 +12,20 @@ use tracing::debug;
1212
pub struct Add {}
1313

1414
impl Function for Add {
15-
fn description(&self) -> String {
16-
t!("functions.add.description").to_string()
17-
}
18-
19-
fn category(&self) -> FunctionCategory {
20-
FunctionCategory::Numeric
21-
}
22-
23-
fn min_args(&self) -> usize {
24-
2
25-
}
26-
27-
fn max_args(&self) -> usize {
28-
2
29-
}
30-
31-
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
32-
vec![AcceptedArgKind::Number]
15+
fn get_metadata(&self) -> FunctionMetadata {
16+
FunctionMetadata {
17+
name: "add".to_string(),
18+
description: t!("functions.add.description").to_string(),
19+
category: FunctionCategory::Numeric,
20+
min_args: 2,
21+
max_args: 2,
22+
accepted_arg_ordered_types: vec![
23+
vec![FunctionArgKind::Number],
24+
vec![FunctionArgKind::Number],
25+
],
26+
remaining_arg_accepted_types: None,
27+
return_types: vec![FunctionArgKind::Number],
28+
}
3329
}
3430

3531
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {

dsc_lib/src/functions/and.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::DscError;
55
use crate::configure::context::Context;
6-
use crate::functions::{AcceptedArgKind, Function, FunctionCategory};
6+
use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata};
77
use rust_i18n::t;
88
use serde_json::Value;
99
use tracing::debug;
@@ -12,24 +12,20 @@ use tracing::debug;
1212
pub struct And {}
1313

1414
impl Function for And {
15-
fn description(&self) -> String {
16-
t!("functions.and.description").to_string()
17-
}
18-
19-
fn category(&self) -> FunctionCategory {
20-
FunctionCategory::Logical
21-
}
22-
23-
fn min_args(&self) -> usize {
24-
2
25-
}
26-
27-
fn max_args(&self) -> usize {
28-
usize::MAX
29-
}
30-
31-
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
32-
vec![AcceptedArgKind::Boolean]
15+
fn get_metadata(&self) -> FunctionMetadata {
16+
FunctionMetadata {
17+
name: "and".to_string(),
18+
description: t!("functions.and.description").to_string(),
19+
category: FunctionCategory::Logical,
20+
min_args: 2,
21+
max_args: usize::MAX,
22+
accepted_arg_ordered_types: vec![
23+
vec![FunctionArgKind::Boolean],
24+
vec![FunctionArgKind::Boolean],
25+
],
26+
remaining_arg_accepted_types: Some(vec![FunctionArgKind::Boolean]),
27+
return_types: vec![FunctionArgKind::Boolean],
28+
}
3329
}
3430

3531
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {

dsc_lib/src/functions/base64.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use base64::{Engine as _, engine::general_purpose};
55

66
use crate::DscError;
77
use crate::configure::context::Context;
8-
use crate::functions::{AcceptedArgKind, FunctionCategory};
8+
use crate::functions::{FunctionArgKind, FunctionCategory, FunctionMetadata};
99
use rust_i18n::t;
1010
use serde_json::Value;
1111
use super::Function;
@@ -14,24 +14,17 @@ use super::Function;
1414
pub struct Base64 {}
1515

1616
impl Function for Base64 {
17-
fn description(&self) -> String {
18-
t!("functions.base64.description").to_string()
19-
}
20-
21-
fn category(&self) -> FunctionCategory {
22-
FunctionCategory::String
23-
}
24-
25-
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
26-
vec![AcceptedArgKind::String]
27-
}
28-
29-
fn min_args(&self) -> usize {
30-
1
31-
}
32-
33-
fn max_args(&self) -> usize {
34-
1
17+
fn get_metadata(&self) -> FunctionMetadata {
18+
FunctionMetadata {
19+
name: "base64".to_string(),
20+
description: t!("functions.base64.description").to_string(),
21+
category: FunctionCategory::String,
22+
min_args: 1,
23+
max_args: 1,
24+
accepted_arg_ordered_types: vec![vec![FunctionArgKind::String]],
25+
remaining_arg_accepted_types: None,
26+
return_types: vec![FunctionArgKind::String],
27+
}
3528
}
3629

3730
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {

dsc_lib/src/functions/bool.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::DscError;
55
use crate::configure::context::Context;
6-
use crate::functions::{AcceptedArgKind, Function, FunctionCategory};
6+
use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata};
77
use rust_i18n::t;
88
use serde_json::Value;
99
use tracing::debug;
@@ -12,24 +12,17 @@ use tracing::debug;
1212
pub struct Bool {}
1313

1414
impl Function for Bool {
15-
fn description(&self) -> String {
16-
t!("functions.bool.description").to_string()
17-
}
18-
19-
fn category(&self) -> FunctionCategory {
20-
FunctionCategory::Logical
21-
}
22-
23-
fn min_args(&self) -> usize {
24-
1
25-
}
26-
27-
fn max_args(&self) -> usize {
28-
1
29-
}
30-
31-
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
32-
vec![AcceptedArgKind::String, AcceptedArgKind::Number]
15+
fn get_metadata(&self) -> FunctionMetadata {
16+
FunctionMetadata {
17+
name: "bool".to_string(),
18+
description: t!("functions.bool.description").to_string(),
19+
category: FunctionCategory::Logical,
20+
min_args: 1,
21+
max_args: 1,
22+
accepted_arg_ordered_types: vec![vec![FunctionArgKind::String, FunctionArgKind::Number]],
23+
remaining_arg_accepted_types: None,
24+
return_types: vec![FunctionArgKind::Boolean],
25+
}
3326
}
3427

3528
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {

0 commit comments

Comments
 (0)