33
44use crate :: DscError ;
55use crate :: configure:: context:: Context ;
6- use crate :: functions:: { AcceptedArgKind , Function , FunctionCategory } ;
6+ use crate :: functions:: { FunctionArgKind , Function , FunctionCategory , FunctionMetadata } ;
77use rust_i18n:: t;
88use serde_json:: Value ;
99use tracing:: debug;
@@ -12,30 +12,35 @@ use tracing::debug;
1212pub struct Coalesce { }
1313
1414impl Function for Coalesce {
15- fn description ( & self ) -> String {
16- t ! ( "functions.coalesce.description" ) . to_string ( )
17- }
18-
19- fn category ( & self ) -> FunctionCategory {
20- FunctionCategory :: Comparison
21- }
22-
23- fn min_args ( & self ) -> usize {
24- 1
25- }
26-
27- fn max_args ( & self ) -> usize {
28- usize:: MAX
29- }
30-
31- fn accepted_arg_types ( & self ) -> Vec < AcceptedArgKind > {
32- vec ! [
33- AcceptedArgKind :: Array ,
34- AcceptedArgKind :: Boolean ,
35- AcceptedArgKind :: Number ,
36- AcceptedArgKind :: Object ,
37- AcceptedArgKind :: String ,
38- ]
15+ fn get_metadata ( & self ) -> FunctionMetadata {
16+ FunctionMetadata {
17+ name : "coalesce" . to_string ( ) ,
18+ description : t ! ( "functions.coalesce.description" ) . to_string ( ) ,
19+ category : FunctionCategory :: Comparison ,
20+ min_args : 1 ,
21+ max_args : usize:: MAX ,
22+ accepted_arg_ordered_types : vec ! [ vec![
23+ FunctionArgKind :: Array ,
24+ FunctionArgKind :: Boolean ,
25+ FunctionArgKind :: Number ,
26+ FunctionArgKind :: Object ,
27+ FunctionArgKind :: String ,
28+ ] ] ,
29+ remaining_arg_accepted_types : Some ( vec ! [
30+ FunctionArgKind :: Array ,
31+ FunctionArgKind :: Boolean ,
32+ FunctionArgKind :: Number ,
33+ FunctionArgKind :: Object ,
34+ FunctionArgKind :: String ,
35+ ] ) ,
36+ return_types : vec ! [
37+ FunctionArgKind :: Array ,
38+ FunctionArgKind :: Boolean ,
39+ FunctionArgKind :: Number ,
40+ FunctionArgKind :: Object ,
41+ FunctionArgKind :: String ,
42+ ] ,
43+ }
3944 }
4045
4146 fn invoke ( & self , args : & [ Value ] , _context : & Context ) -> Result < Value , DscError > {
@@ -61,15 +66,15 @@ mod tests {
6166 fn direct_function_call_with_nulls ( ) {
6267 let coalesce = Coalesce { } ;
6368 let context = Context :: new ( ) ;
64-
69+
6570 let args = vec ! [ Value :: Null , Value :: Null , Value :: String ( "hello" . to_string( ) ) ] ;
6671 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
6772 assert_eq ! ( result, Value :: String ( "hello" . to_string( ) ) ) ;
68-
73+
6974 let args = vec ! [ Value :: Null , Value :: Null , Value :: Null ] ;
7075 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
7176 assert_eq ! ( result, Value :: Null ) ;
72-
77+
7378 let args = vec ! [ Value :: String ( "first" . to_string( ) ) , Value :: String ( "second" . to_string( ) ) ] ;
7479 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
7580 assert_eq ! ( result, Value :: String ( "first" . to_string( ) ) ) ;
@@ -79,11 +84,11 @@ mod tests {
7984 fn direct_function_call_mixed_types ( ) {
8085 let coalesce = Coalesce { } ;
8186 let context = Context :: new ( ) ;
82-
87+
8388 let args = vec ! [ Value :: Null , serde_json:: json!( 42 ) , Value :: String ( "fallback" . to_string( ) ) ] ;
8489 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
8590 assert_eq ! ( result, serde_json:: json!( 42 ) ) ;
86-
91+
8792 let args = vec ! [ Value :: Null , Value :: Bool ( true ) ] ;
8893 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
8994 assert_eq ! ( result, Value :: Bool ( true ) ) ;
@@ -93,14 +98,14 @@ mod tests {
9398 fn direct_function_call_with_arrays ( ) {
9499 let coalesce = Coalesce { } ;
95100 let context = Context :: new ( ) ;
96-
101+
97102 let first_array = serde_json:: json!( [ "a" , "b" , "c" ] ) ;
98103 let second_array = serde_json:: json!( [ "x" , "y" , "z" ] ) ;
99-
104+
100105 let args = vec ! [ Value :: Null , first_array. clone( ) , second_array] ;
101106 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
102107 assert_eq ! ( result, first_array) ;
103-
108+
104109 let args = vec ! [ Value :: Null , Value :: Null , serde_json:: json!( [ 1 , 2 , 3 ] ) ] ;
105110 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
106111 assert_eq ! ( result, serde_json:: json!( [ 1 , 2 , 3 ] ) ) ;
@@ -110,14 +115,14 @@ mod tests {
110115 fn direct_function_call_with_objects ( ) {
111116 let coalesce = Coalesce { } ;
112117 let context = Context :: new ( ) ;
113-
118+
114119 let first_obj = serde_json:: json!( { "name" : "test" , "value" : 42 } ) ;
115120 let second_obj = serde_json:: json!( { "name" : "fallback" , "value" : 0 } ) ;
116-
121+
117122 let args = vec ! [ Value :: Null , first_obj. clone( ) , second_obj] ;
118123 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
119124 assert_eq ! ( result, first_obj) ;
120-
125+
121126 let args = vec ! [ Value :: Null , Value :: Null , serde_json:: json!( { "key" : "value" } ) ] ;
122127 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
123128 assert_eq ! ( result, serde_json:: json!( { "key" : "value" } ) ) ;
@@ -127,12 +132,12 @@ mod tests {
127132 fn direct_function_call_with_empty_collections ( ) {
128133 let coalesce = Coalesce { } ;
129134 let context = Context :: new ( ) ;
130-
135+
131136 let empty_array = serde_json:: json!( [ ] ) ;
132137 let args = vec ! [ Value :: Null , empty_array. clone( ) , Value :: String ( "fallback" . to_string( ) ) ] ;
133138 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
134139 assert_eq ! ( result, empty_array) ;
135-
140+
136141 let empty_obj = serde_json:: json!( { } ) ;
137142 let args = vec ! [ Value :: Null , empty_obj. clone( ) , Value :: String ( "fallback" . to_string( ) ) ] ;
138143 let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
@@ -144,10 +149,10 @@ mod tests {
144149 let mut parser = Statement :: new ( ) . unwrap ( ) ;
145150 let result = parser. parse_and_execute ( "[coalesce('hello', 'world')]" , & Context :: new ( ) ) . unwrap ( ) ;
146151 assert_eq ! ( result, "hello" ) ;
147-
152+
148153 let result = parser. parse_and_execute ( "[coalesce(42, 'fallback')]" , & Context :: new ( ) ) . unwrap ( ) ;
149154 assert_eq ! ( result, 42 ) ;
150-
155+
151156 let result = parser. parse_and_execute ( "[coalesce(true)]" , & Context :: new ( ) ) . unwrap ( ) ;
152157 assert_eq ! ( result, true ) ;
153158 }
0 commit comments