@@ -133,8 +133,6 @@ struct ConstructIfChanged{C}
133
133
constructor:: C
134
134
end
135
135
136
- _constructor (x, t) = _constructor (x. handler, t)
137
-
138
136
# TODO what do we call these things?
139
137
struct Construct end
140
138
_constructor (:: Construct , :: Type{T} ) where T = constructorof (T)
@@ -251,19 +249,6 @@ function modify(f, obj, w::If)
251
249
end
252
250
end
253
251
254
- struct Select{C}
255
- select_condition:: C
256
- end
257
- OpticStyle (:: Type{<:If} ) = ModifyBased ()
258
-
259
- function modify (f, obj, w:: If )
260
- if w. modify_condition (obj)
261
- (obj,)
262
- else
263
- ()
264
- end
265
- end
266
-
267
252
"""
268
253
mapproperties(f, obj)
269
254
@@ -280,7 +265,7 @@ julia> Accessors.mapproperties(x -> x+1, obj)
280
265
```
281
266
$EXPERIMENTAL
282
267
"""
283
- function mapproperties (f, obj:: O , optic , itr:: Nothing = nothing ) where O
268
+ function mapproperties (f, obj:: O , handler = Construct () , itr:: Nothing = nothing ) where O
284
269
# TODO move this helper elsewhere?
285
270
pnames = propertynames (obj)
286
271
if isempty (pnames)
@@ -290,11 +275,11 @@ function mapproperties(f, obj::O, optic, itr::Nothing=nothing) where O
290
275
new_props = map (pnames) do p
291
276
f (getproperty (obj, p))
292
277
end
293
- ctr = _constructor (optic , O)
278
+ ctr = _constructor (handler , O)
294
279
return ctr (new_props... )
295
280
end
296
281
end
297
- function mapproperties (f, obj:: O , optic , itr:: Int ) where O
282
+ function mapproperties (f, obj:: O , handler , itr:: Int ) where O
298
283
pnames = propertynames (obj)
299
284
if isempty (pnames)
300
285
return _maybeitr (obj, itr)
@@ -304,7 +289,7 @@ function mapproperties(f, obj::O, optic, itr::Int) where O
304
289
val, itr = f (getproperty (obj, p), itr)
305
290
(vals... , val), itr
306
291
end
307
- ctr = _constructor (optic , O)
292
+ ctr = _constructor (handler , O)
308
293
return _maybeitr (ctr (new_props... ), itr)
309
294
end
310
295
end
@@ -330,10 +315,7 @@ Based on [`mapproperties`](@ref).
330
315
331
316
$EXPERIMENTAL
332
317
"""
333
- struct Properties{H}
334
- handler:: H
335
- end
336
- Properties () = Properties (Construct ())
318
+ struct Properties end
337
319
OpticStyle (:: Type{<:Properties} ) = ModifyBased ()
338
320
modify (f, o, :: Properties ) = mapproperties (f, o)
339
321
@@ -353,7 +335,7 @@ julia> Accessors.mapfields(x -> x+1, obj)
353
335
```
354
336
$EXPERIMENTAL
355
337
"""
356
- @generated function mapfields (f, obj:: O , optic , itr:: I = nothing ) where {O,H,I}
338
+ @generated function mapfields (f, obj:: O , handler :: H = Construct () , itr:: I = nothing ) where {O,H,I}
357
339
# TODO : This is how Flatten.jl works, but it's not really
358
340
# correct use of ConstructionBase as it assumers properties=fields
359
341
fnames = fieldnames (O)
@@ -424,12 +406,9 @@ Based on [`mapfields`](@ref).
424
406
425
407
$EXPERIMENTAL
426
408
"""
427
- struct Fields{H}
428
- handler:: H
429
- end
430
- Fields () = Fields (Construct ())
409
+ struct Fields end
431
410
OpticStyle (:: Type{<:Fields} ) = ModifyBased ()
432
- modify (f, o, optic :: Fields ) = mapfields (f, o, optic )
411
+ modify (f, o, :: Fields ) = mapfields (f, o)
433
412
434
413
"""
435
414
Recursive(descent_condition, optic)
@@ -528,18 +507,22 @@ julia> modify(x -> 100x, obj, Recursive(x -> (x isa Tuple), Elements()))
528
507
```
529
508
$EXPERIMENTAL
530
509
"""
531
- struct Query{Select,Descend,Optic}
510
+ struct Query{Select,Descend,Optic<: Union{ComposedOptic,Fields,Properties} }
532
511
select_condition:: Select
533
512
descent_condition:: Descend
534
513
optic:: Optic
535
514
end
536
515
Query (select, descend = x -> true ) = Query (select, descend, Fields ())
537
516
Query (; select= Any, descend= x -> true , optic= Fields ()) = Query (select, descend, optic)
538
517
518
+ _inner (optic:: ComposedOptic ) = optic. inner
519
+ _inner (optic:: Fields ) = optic
520
+ _inner (optic:: Properties ) = optic
521
+
539
522
function (q:: Query )(obj)
540
- _query (obj, q. optic, Splat (), nothing ) do o
523
+ _query (obj, _inner ( q. optic) , Splat (), nothing ) do o
541
524
if q. select_condition (o)
542
- (o ,)
525
+ (_getouter (o, q . optic) ,)
543
526
elseif q. descent_condition (o)
544
527
q (o)
545
528
else
@@ -548,12 +531,15 @@ function (q::Query)(obj)
548
531
end
549
532
end
550
533
534
+ _getouter (o, optic:: ComposedOptic ) = optic. outer (o)
535
+ _getouter (o, optic) = o
536
+
551
537
_set (obj, q:: Query , val, :: SetBased ) = _setquery (obj, q:: Query , (val, 1 ))[1 ]
552
538
553
539
function _setquery (obj, q:: Query , (val, itr))
554
- _query (obj, q. optic, Construct (), itr) do o, itr
540
+ _query (obj, _inner ( q. optic) , Construct (), itr) do o, itr
555
541
if q. select_condition (o)
556
- val[itr], itr + 1
542
+ _setouter (o, q . optic, val[itr]) , itr + 1
557
543
elseif q. descent_condition (o)
558
544
_setquery (o, q, (val, itr))
559
545
else
@@ -562,8 +548,10 @@ function _setquery(obj, q::Query, (val, itr))
562
548
end
563
549
end
564
550
551
+ _setouter (o, optic:: ComposedOptic , v) = set (o, optic. outer, v)
552
+ _setouter (o, optic, v) = v
553
+
565
554
modify (f, obj, q:: Query ) = set (obj, q, map (f, q (obj)))
566
555
567
- _query (f, o, :: Elements , itr) = map (f, o)
568
- _query (f, o, :: Fields , itr) = mapfields (f, o, itr)
569
- _query (f, o, :: Properties , itr) = mapproperties (f, o, itr)
556
+ _query (f, o, :: Fields , handler, itr) = mapfields (f, o, handler, itr)
557
+ _query (f, o, :: Properties , handler, itr) = mapproperties (f, o, handler, itr)
0 commit comments