Skip to content

Commit

Permalink
Revert "getall(maybe) returns nothings if not present"
Browse files Browse the repository at this point in the history
This reverts commit 0a70e63.
  • Loading branch information
aplavin committed Dec 18, 2024
1 parent 30c3864 commit 538d80d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/maybe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Accessors.composed_optic_style(ma::MaybeStyle, mb::MaybeStyle) = MaybeStyle(Acce
@inline set(obj, o::MaybeOptic, val::Nothing) = hasoptic(obj, o.o) ? delete(obj, o.o) : obj
@inline set(obj, o::MaybeOptic, val) = hasoptic(obj, o.o) ? set(obj, o.o, val) : insert(obj, o.o, val)

@inline getall(obj, o::MaybeOptic) = (o(obj),)
@inline getall(obj, o::MaybeOptic) = hasoptic(obj, o.o) ? getall(obj, o.o) : ()
@inline setall(obj, o::MaybeOptic, vals) = hasoptic(obj, o.o) ? setall(obj, o.o, vals) : obj

function modify(f, obj, o::MaybeOptic)
Expand Down
6 changes: 3 additions & 3 deletions test/concatoptic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ end
@test flat_concatoptic(Nothing, (@o _[ₚ][ₚ])) === concat()

o = tree_concatoptic(Union{Nothing, @NamedTuple{a::Int64, b::Float64}}, (@o _[ₚ]))
@test getall(nothing, o) === (nothing, nothing)
@test getall(nothing, o) === ()
@test getall((a=1, b=2.0), o) === (1, 2.0)

o = tree_concatoptic(Union{Nothing, @NamedTuple{a::Union{Nothing, @NamedTuple{b::Int64, c::String}}}}, (@o _[ₚ][ₚ]))
@test getall(nothing, o) === (nothing, nothing)
@test getall((a=nothing,), o) === (nothing, nothing)
@test getall(nothing, o) === ()
@test getall((a=nothing,), o) === ()
@test getall((a=(b=1, c="2"),), o) === (1, "2")

os = flat_concatoptic(Union{Nothing, @NamedTuple{a::Int64, b::Float64}}, (@o _[ₚ])) |> AccessorsExtra._optics
Expand Down
24 changes: 12 additions & 12 deletions test/maybe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ end
@test modify(x -> nothing, (;), o) == (;)

@test getall((a=(b=1,),), o) == (1,)
@test getall((a=(;),), o) == (nothing,)
@test getall((;), o) == (nothing,)
@test getall(nothing, o) == (nothing,)
@test getall((a=(;),), o) == ()
@test getall((;), o) == ()
@test getall(nothing, o) == ()
@test setall((a=(b=1,),), o, (5,)) == (a=(b=5,),)
@test setall((a=(;),), o, (123,)) == (a=(;),)
@test setall((;), o, (123,)) == (;)
@test setall(nothing, o, (123,)) == nothing
@test setall((a=(;),), o, ()) == (a=(;),)
@test setall((;), o, ()) == (;)
@test setall(nothing, o, ()) == nothing
end

for obj in ((5,), (a=5,), [5], Dict(1 => 5),)
Expand Down Expand Up @@ -124,16 +124,16 @@ end
@test_broken set("2020-02-03", o, Date(1234, 5, 6)) == "1234/05/06"

o = maybe(@o _.a) Elements()
@test getall(((a=1,), (b=2,)), o) === (1, nothing)
@test getall(((b=2,),), o) === (nothing,)
@test getall(((),), o) === (nothing,)
@test getall(((a=1,), (b=2,)), o) === (1,)
@test getall(((b=2,),), o) === ()
@test getall(((),), o) === ()
@test modify(x -> x+1, ((a=1,), (b=2,)), o) === ((a=2,), (b=2,))
@test modify(x -> nothing, ((a=1,), (b=2,)), o) === ((;), (b=2,))
@test set(((a=1,), (b=2,)), o, 10) === ((a=10,), (b=2,))
@test set(((a=1,), (b=2,)), o, nothing) === ((;), (b=2,))
@test setall(((a=1,), (b=2,)), o, (10, 123)) === ((a=10,), (b=2,))
@test_throws "tried to assign 0 elements to 2 destinations" setall(((a=1,), (b=2,)), o, ()) === ((a=10,), (b=2,))
@test_throws "tried to assign 1 elements to 2 destinations" setall(((a=1,), (b=2,)), o, (10,)) === ((a=10,), (b=2,))
@test setall(((a=1,), (b=2,)), o, (10,)) === ((a=10,), (b=2,))
@test_throws "tried to assign 0 elements to 1 destinations" setall(((a=1,), (b=2,)), o, ()) === ((a=10,), (b=2,))
@test_throws "tried to assign 2 elements to 1 destinations" setall(((a=1,), (b=2,)), o, (10, 20)) === ((a=10,), (b=2,))

# specify default value in maybe() - semantic not totally clear...
# also see "get(...) as optic"
Expand Down

0 comments on commit 538d80d

Please sign in to comment.