11use super :: util;
22use crate :: compiler:: prelude:: * ;
3+ use crate :: stdlib:: util:: BoolConstOrExpr ;
34
45static DEFAULT_RECURSIVE : Value = Value :: Boolean ( true ) ;
56static DEFAULT_NULL : Value = Value :: Boolean ( true ) ;
@@ -29,21 +30,21 @@ const PARAMETERS: &[Parameter] = &[
2930] ;
3031
3132fn compact (
32- recursive : Value ,
33- null : Value ,
34- string : Value ,
35- object : Value ,
36- array : Value ,
37- nullish : Value ,
33+ recursive : bool ,
34+ null : bool ,
35+ string : bool ,
36+ object : bool ,
37+ array : bool ,
38+ nullish : bool ,
3839 value : Value ,
3940) -> Resolved {
4041 let options = CompactOptions {
41- recursive : recursive . try_boolean ( ) ? ,
42- null : null . try_boolean ( ) ? ,
43- string : string . try_boolean ( ) ? ,
44- object : object . try_boolean ( ) ? ,
45- array : array . try_boolean ( ) ? ,
46- nullish : nullish . try_boolean ( ) ? ,
42+ recursive,
43+ null,
44+ string,
45+ object,
46+ array,
47+ nullish,
4748 } ;
4849
4950 match value {
@@ -117,17 +118,35 @@ impl Function for Compact {
117118
118119 fn compile (
119120 & self ,
120- _state : & state:: TypeState ,
121+ state : & state:: TypeState ,
121122 _ctx : & mut FunctionCompileContext ,
122123 arguments : ArgumentList ,
123124 ) -> Compiled {
124125 let value = arguments. required ( "value" ) ;
125- let recursive = arguments. optional ( "recursive" ) ;
126- let null = arguments. optional ( "null" ) ;
127- let string = arguments. optional ( "string" ) ;
128- let object = arguments. optional ( "object" ) ;
129- let array = arguments. optional ( "array" ) ;
130- let nullish = arguments. optional ( "nullish" ) ;
126+ let recursive = BoolConstOrExpr :: new_with_default (
127+ arguments. optional ( "recursive" ) ,
128+ state,
129+ & DEFAULT_RECURSIVE ,
130+ ) ?;
131+ let null =
132+ BoolConstOrExpr :: new_with_default ( arguments. optional ( "null" ) , state, & DEFAULT_NULL ) ?;
133+ let string = BoolConstOrExpr :: new_with_default (
134+ arguments. optional ( "string" ) ,
135+ state,
136+ & DEFAULT_STRING ,
137+ ) ?;
138+ let object = BoolConstOrExpr :: new_with_default (
139+ arguments. optional ( "object" ) ,
140+ state,
141+ & DEFAULT_OBJECT ,
142+ ) ?;
143+ let array =
144+ BoolConstOrExpr :: new_with_default ( arguments. optional ( "array" ) , state, & DEFAULT_ARRAY ) ?;
145+ let nullish = BoolConstOrExpr :: new_with_default (
146+ arguments. optional ( "nullish" ) ,
147+ state,
148+ & DEFAULT_NULLISH ,
149+ ) ?;
131150
132151 Ok ( CompactFn {
133152 value,
@@ -145,12 +164,12 @@ impl Function for Compact {
145164#[ derive( Debug , Clone ) ]
146165struct CompactFn {
147166 value : Box < dyn Expression > ,
148- recursive : Option < Box < dyn Expression > > ,
149- null : Option < Box < dyn Expression > > ,
150- string : Option < Box < dyn Expression > > ,
151- object : Option < Box < dyn Expression > > ,
152- array : Option < Box < dyn Expression > > ,
153- nullish : Option < Box < dyn Expression > > ,
167+ recursive : BoolConstOrExpr ,
168+ null : BoolConstOrExpr ,
169+ string : BoolConstOrExpr ,
170+ object : BoolConstOrExpr ,
171+ array : BoolConstOrExpr ,
172+ nullish : BoolConstOrExpr ,
154173}
155174
156175#[ derive( Debug ) ]
@@ -196,24 +215,12 @@ impl CompactOptions {
196215
197216impl FunctionExpression for CompactFn {
198217 fn resolve ( & self , ctx : & mut Context ) -> Resolved {
199- let recursive = self
200- . recursive
201- . map_resolve_with_default ( ctx, || DEFAULT_RECURSIVE . clone ( ) ) ?;
202- let null = self
203- . null
204- . map_resolve_with_default ( ctx, || DEFAULT_NULL . clone ( ) ) ?;
205- let string = self
206- . string
207- . map_resolve_with_default ( ctx, || DEFAULT_STRING . clone ( ) ) ?;
208- let object = self
209- . object
210- . map_resolve_with_default ( ctx, || DEFAULT_OBJECT . clone ( ) ) ?;
211- let array = self
212- . array
213- . map_resolve_with_default ( ctx, || DEFAULT_ARRAY . clone ( ) ) ?;
214- let nullish = self
215- . nullish
216- . map_resolve_with_default ( ctx, || DEFAULT_NULLISH . clone ( ) ) ?;
218+ let recursive = self . recursive . resolve ( ctx) ?;
219+ let null = self . null . resolve ( ctx) ?;
220+ let string = self . string . resolve ( ctx) ?;
221+ let object = self . object . resolve ( ctx) ?;
222+ let array = self . array . resolve ( ctx) ?;
223+ let nullish = self . nullish . resolve ( ctx) ?;
217224 let value = self . value . resolve ( ctx) ?;
218225
219226 compact ( recursive, null, string, object, array, nullish, value)
@@ -272,7 +279,7 @@ mod test {
272279
273280 #[ test]
274281 fn test_compacted_array ( ) {
275- let cases = vec ! [
282+ let cases = [
276283 (
277284 vec ! [ "" . into( ) , "" . into( ) ] , // expected
278285 vec ! [ "" . into( ) , Value :: Null , "" . into( ) ] , // original
@@ -333,7 +340,7 @@ mod test {
333340 #[ test]
334341 #[ allow( clippy:: too_many_lines) ]
335342 fn test_compacted_map ( ) {
336- let cases = vec ! [
343+ let cases = [
337344 (
338345 btreemap ! {
339346 "key1" => "" ,
0 commit comments