Skip to content

Commit

Permalink
set(): relax NTuple type constraints (#165)
Browse files Browse the repository at this point in the history
* set(endpoints): relax type signature
* set(dates): relax type signature
  • Loading branch information
aplavin authored Aug 26, 2024
1 parent a5166b7 commit 991e92d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions ext/AccessorsDatesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ set(x::Time, ::typeof(Dates.value), y) = @set x.instant.value = y
set(x::Date, ::typeof(year), y) = Date(y, month(x), day(x))
set(x::Date, ::typeof(month), y) = Date(year(x), y, day(x))
set(x::Date, ::typeof(day), y) = Date(year(x), month(x), y)
set(x::Date, ::typeof(yearmonth), y::NTuple{2}) = Date(y..., day(x))
set(x::Date, ::typeof(monthday), y::NTuple{2}) = Date(year(x), y...)
set(x::Date, ::typeof(yearmonthday), y::NTuple{3}) = Date(y...)
set(x::Date, ::typeof(yearmonth), y::NTuple{2, Any}) = Date(y..., day(x))
set(x::Date, ::typeof(monthday), y::NTuple{2, Any}) = Date(year(x), y...)
set(x::Date, ::typeof(yearmonthday), y::NTuple{3, Any}) = Date(y...)
set(x::Date, ::typeof(dayofweek), y) = firstdayofweek(x) + Day(y - 1)

set(x::Time, ::typeof(hour), y) = Time(y, minute(x), second(x), millisecond(x), microsecond(x), nanosecond(x))
Expand Down
2 changes: 1 addition & 1 deletion ext/AccessorsIntervalSetsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module AccessorsIntervalSetsExt
using Accessors
using IntervalSets

Accessors.set(x::Interval, ::typeof(endpoints), v::NTuple{2}) = setproperties(x, left=first(v), right=last(v))
Accessors.set(x::Interval, ::typeof(endpoints), v::NTuple{2, Any}) = setproperties(x, left=first(v), right=last(v))
Accessors.set(x::Interval, ::typeof(leftendpoint), v) = @set x.left = v
Accessors.set(x::Interval, ::typeof(rightendpoint), v) = @set x.right = v
Accessors.set(x::Interval, ::typeof(closedendpoints), v::NTuple{2, Bool}) = Interval{v[1] ? :closed : :open, v[2] ? :closed : :open}(endpoints(x)...)
Expand Down
1 change: 1 addition & 0 deletions test/test_extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ VERSION >= v"1.9-" && @testset "IntervalSets" begin

@test Interval{:open, :closed}(1, 10) === @set int.right = 10
@test Interval{:open, :closed}(10.0, 11.0) === @set endpoints(int) = (10.0, 11.0)
@test Interval{:open, :closed}(10.0, 11.0) === @set endpoints(int) = (10, 11.0)
@test Interval{:open, :closed}(-2, 5) === @set leftendpoint(int) = -2
@test Interval{:open, :closed}(1, 2) === @set rightendpoint(int) = 2
@test Interval{:closed, :closed}(1, 5) === @set first(closedendpoints(int)) = true
Expand Down
6 changes: 3 additions & 3 deletions test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ end
test_getset_laws(lens, Time(1, 2, 3, 4, 5, 6), rand(0:23), rand(0:23))
end
@testset for x in [DateTime(2020, 1, 2, 3, 4, 5, 6), Date(2020, 1, 2)]
test_getset_laws(yearmonth, x, (rand(1:5000), rand(1:12)), (rand(1:5000), rand(1:12)))
test_getset_laws(monthday, x, (rand(1:12), rand(1:28)), (rand(1:12), rand(1:28)))
test_getset_laws(yearmonthday, x, (rand(1:5000), rand(1:12), rand(1:28)), (rand(1:5000), rand(1:12), rand(1:28)))
test_getset_laws(yearmonth, x, (rand(1:5000), rand(1:12) |> Int8), (rand(1:5000), rand(1:12)))
test_getset_laws(monthday, x, (rand(1:12), rand(1:28) |> Int8), (rand(1:12), rand(1:28)))
test_getset_laws(yearmonthday, x, (rand(1:5000), rand(1:12) |> Int8, rand(1:28)), (rand(1:5000), rand(1:12), rand(1:28)))
end

@testset for x in [DateTime(2020, 1, 2, 3, 4, 5, 6), Date(2020, 1, 2), Time(1, 2, 3, 4, 5, 6)]
Expand Down

0 comments on commit 991e92d

Please sign in to comment.