@@ -120,7 +120,7 @@ defmodule Iteraptor do
120
120
Map . put ( acc , key , v )
121
121
end
122
122
123
- with { _ , flattened } <- reduce ( input , % { } , reducer , opts ) , do: flattened
123
+ reduce ( input , % { } , reducer , opts )
124
124
end
125
125
126
126
@ doc """
@@ -214,7 +214,7 @@ defmodule Iteraptor do
214
214
- `yield`: `[:all | :none | :maps | :lists]` what to yield; _default:_ `:all`
215
215
for yielding _values only_
216
216
- `keys`: `[:reverse]` reverse keys list to ease pattern matching; _default:_ `nil`
217
- - `structs`: `[:values | :keep]` how to handle structs; _default:_ `nil `
217
+ - `structs`: `[:values | :keep]` how to handle structs; _default:_ `:values `
218
218
for treating them as `map`s. When `:values`, the nested structs
219
219
are considered leaves and returned to the iterator instead of being iterated
220
220
through; when `:keep` it returns a struct back after iteration
@@ -278,9 +278,7 @@ defmodule Iteraptor do
278
278
{ type , _ , into } = type ( input )
279
279
{ result , _ } = traverse ( input , fun , opts , { [ ] , into } )
280
280
281
- if opts [ :structs ] == :keep && is_map ( result ) and type != Map ,
282
- do: struct ( type , result ) ,
283
- else: result
281
+ maybe_struct ( opts [ :structs ] , result , type )
284
282
end
285
283
286
284
@ doc """
@@ -307,7 +305,7 @@ defmodule Iteraptor do
307
305
["a", "a_b", "a_b_c"]
308
306
"""
309
307
310
- @ spec reduce ( Access . t ( ) , Access . t ( ) , traverse_fun ( ) , options ( ) ) :: { Access . t ( ) , any ( ) }
308
+ @ spec reduce ( Access . t ( ) , Access . t ( ) , traverse_fun ( ) , options ( ) ) :: Access . t ( )
311
309
312
310
def reduce ( input , acc \\ nil , fun , opts \\ [ ] ) do
313
311
unless is_function ( fun , 2 ) , do: raise ( "Function or arity fun/2 is required" )
@@ -317,9 +315,7 @@ defmodule Iteraptor do
317
315
fun_wrapper = fn kv , acc -> { kv , fun . ( kv , acc ) } end
318
316
{ _ , result } = traverse ( input , fun_wrapper , opts , { [ ] , acc } )
319
317
320
- if opts [ :structs ] == :keep && is_map ( result ) and type != Map ,
321
- do: struct ( type , result ) ,
322
- else: result
318
+ maybe_struct ( opts [ :structs ] , result , type )
323
319
end
324
320
325
321
@ doc """
@@ -356,12 +352,7 @@ defmodule Iteraptor do
356
352
acc = if is_nil ( acc ) , do: into , else: acc
357
353
{ map_result , result } = traverse ( input , fun , opts , { [ ] , acc } )
358
354
359
- result =
360
- if opts [ :structs ] == :keep && is_map ( result ) and type != Map ,
361
- do: struct ( type , result ) ,
362
- else: result
363
-
364
- { map_result , result }
355
+ { map_result , maybe_struct ( opts [ :structs ] , result , type ) }
365
356
end
366
357
367
358
@ doc """
@@ -383,7 +374,7 @@ defmodule Iteraptor do
383
374
%{a: %{e: %{c: 42}, d: %{c: 42}}, c: 42}
384
375
"""
385
376
386
- @ spec filter ( Access . t ( ) , traverse_fun ( ) , options ( ) ) :: { Access . t ( ) , any ( ) }
377
+ @ spec filter ( Access . t ( ) , traverse_fun ( ) , options ( ) ) :: Access . t ( )
387
378
388
379
def filter ( input , fun , opts \\ [ ] ) do
389
380
unless is_function ( fun , 1 ) , do: raise ( "Function or arity fun/1 is required" )
@@ -395,9 +386,7 @@ defmodule Iteraptor do
395
386
396
387
{ _ , result } = traverse ( input , fun_wrapper , opts , { [ ] , acc } )
397
388
398
- if opts [ :structs ] == :keep && is_map ( result ) and type != Map ,
399
- do: struct ( type , result ) ,
400
- else: result
389
+ maybe_struct ( opts [ :structs ] , result , type )
401
390
end
402
391
403
392
@ doc """
@@ -444,6 +433,10 @@ defmodule Iteraptor do
444
433
)
445
434
end
446
435
436
+ @ spec maybe_struct ( :keep | :values , result :: any ( ) , type :: module ( ) ) :: Access . t ( )
437
+ defp maybe_struct ( :keep , % { } = result , type ) when type != Map , do: struct ( type , result )
438
+ defp maybe_struct ( _ , result , _ ) , do: result
439
+
447
440
@ spec do_stringify ( any ( ) , boolean ( ) ) :: any ( ) | binary ( )
448
441
defp do_stringify ( k , false ) , do: k
449
442
defp do_stringify ( k , _ ) when is_atom ( k ) , do: Atom . to_string ( k )
@@ -514,12 +507,7 @@ defmodule Iteraptor do
514
507
end
515
508
end )
516
509
517
- result = Enum . into ( value , into )
518
-
519
- result =
520
- if opts [ :structs ] == :keep && is_map ( result ) and type != Map ,
521
- do: struct ( type , result ) ,
522
- else: result
510
+ result = maybe_struct ( opts [ :structs ] , Enum . into ( value , into ) , type )
523
511
524
512
{ squeeze ( result , opts ) , acc }
525
513
end
0 commit comments