3
3
4
4
use crate :: DscError ;
5
5
use crate :: configure:: context:: Context ;
6
- use crate :: functions:: { AcceptedArgKind , Function , FunctionCategory } ;
6
+ use crate :: functions:: { FunctionArgKind , Function , FunctionCategory , FunctionMetadata } ;
7
7
use rust_i18n:: t;
8
8
use serde_json:: Value ;
9
9
use tracing:: debug;
@@ -12,30 +12,35 @@ use tracing::debug;
12
12
pub struct Coalesce { }
13
13
14
14
impl 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
+ }
39
44
}
40
45
41
46
fn invoke ( & self , args : & [ Value ] , _context : & Context ) -> Result < Value , DscError > {
@@ -61,15 +66,15 @@ mod tests {
61
66
fn direct_function_call_with_nulls ( ) {
62
67
let coalesce = Coalesce { } ;
63
68
let context = Context :: new ( ) ;
64
-
69
+
65
70
let args = vec ! [ Value :: Null , Value :: Null , Value :: String ( "hello" . to_string( ) ) ] ;
66
71
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
67
72
assert_eq ! ( result, Value :: String ( "hello" . to_string( ) ) ) ;
68
-
73
+
69
74
let args = vec ! [ Value :: Null , Value :: Null , Value :: Null ] ;
70
75
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
71
76
assert_eq ! ( result, Value :: Null ) ;
72
-
77
+
73
78
let args = vec ! [ Value :: String ( "first" . to_string( ) ) , Value :: String ( "second" . to_string( ) ) ] ;
74
79
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
75
80
assert_eq ! ( result, Value :: String ( "first" . to_string( ) ) ) ;
@@ -79,11 +84,11 @@ mod tests {
79
84
fn direct_function_call_mixed_types ( ) {
80
85
let coalesce = Coalesce { } ;
81
86
let context = Context :: new ( ) ;
82
-
87
+
83
88
let args = vec ! [ Value :: Null , serde_json:: json!( 42 ) , Value :: String ( "fallback" . to_string( ) ) ] ;
84
89
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
85
90
assert_eq ! ( result, serde_json:: json!( 42 ) ) ;
86
-
91
+
87
92
let args = vec ! [ Value :: Null , Value :: Bool ( true ) ] ;
88
93
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
89
94
assert_eq ! ( result, Value :: Bool ( true ) ) ;
@@ -93,14 +98,14 @@ mod tests {
93
98
fn direct_function_call_with_arrays ( ) {
94
99
let coalesce = Coalesce { } ;
95
100
let context = Context :: new ( ) ;
96
-
101
+
97
102
let first_array = serde_json:: json!( [ "a" , "b" , "c" ] ) ;
98
103
let second_array = serde_json:: json!( [ "x" , "y" , "z" ] ) ;
99
-
104
+
100
105
let args = vec ! [ Value :: Null , first_array. clone( ) , second_array] ;
101
106
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
102
107
assert_eq ! ( result, first_array) ;
103
-
108
+
104
109
let args = vec ! [ Value :: Null , Value :: Null , serde_json:: json!( [ 1 , 2 , 3 ] ) ] ;
105
110
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
106
111
assert_eq ! ( result, serde_json:: json!( [ 1 , 2 , 3 ] ) ) ;
@@ -110,14 +115,14 @@ mod tests {
110
115
fn direct_function_call_with_objects ( ) {
111
116
let coalesce = Coalesce { } ;
112
117
let context = Context :: new ( ) ;
113
-
118
+
114
119
let first_obj = serde_json:: json!( { "name" : "test" , "value" : 42 } ) ;
115
120
let second_obj = serde_json:: json!( { "name" : "fallback" , "value" : 0 } ) ;
116
-
121
+
117
122
let args = vec ! [ Value :: Null , first_obj. clone( ) , second_obj] ;
118
123
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
119
124
assert_eq ! ( result, first_obj) ;
120
-
125
+
121
126
let args = vec ! [ Value :: Null , Value :: Null , serde_json:: json!( { "key" : "value" } ) ] ;
122
127
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
123
128
assert_eq ! ( result, serde_json:: json!( { "key" : "value" } ) ) ;
@@ -127,12 +132,12 @@ mod tests {
127
132
fn direct_function_call_with_empty_collections ( ) {
128
133
let coalesce = Coalesce { } ;
129
134
let context = Context :: new ( ) ;
130
-
135
+
131
136
let empty_array = serde_json:: json!( [ ] ) ;
132
137
let args = vec ! [ Value :: Null , empty_array. clone( ) , Value :: String ( "fallback" . to_string( ) ) ] ;
133
138
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
134
139
assert_eq ! ( result, empty_array) ;
135
-
140
+
136
141
let empty_obj = serde_json:: json!( { } ) ;
137
142
let args = vec ! [ Value :: Null , empty_obj. clone( ) , Value :: String ( "fallback" . to_string( ) ) ] ;
138
143
let result = coalesce. invoke ( & args, & context) . unwrap ( ) ;
@@ -144,10 +149,10 @@ mod tests {
144
149
let mut parser = Statement :: new ( ) . unwrap ( ) ;
145
150
let result = parser. parse_and_execute ( "[coalesce('hello', 'world')]" , & Context :: new ( ) ) . unwrap ( ) ;
146
151
assert_eq ! ( result, "hello" ) ;
147
-
152
+
148
153
let result = parser. parse_and_execute ( "[coalesce(42, 'fallback')]" , & Context :: new ( ) ) . unwrap ( ) ;
149
154
assert_eq ! ( result, 42 ) ;
150
-
155
+
151
156
let result = parser. parse_and_execute ( "[coalesce(true)]" , & Context :: new ( ) ) . unwrap ( ) ;
152
157
assert_eq ! ( result, true ) ;
153
158
}
0 commit comments