Skip to content

Commit 9854e42

Browse files
authored
more setters: propertynames & ustrip (#135)
* set(ustrip) * set(propertynames)
1 parent 21d2e02 commit 9854e42

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
2020
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
2121
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2222
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
23+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2324

2425
[extensions]
2526
AccessorsAxisKeysExt = "AxisKeys"
2627
AccessorsIntervalSetsExt = "IntervalSets"
2728
AccessorsStaticArraysExt = "StaticArrays"
2829
AccessorsStructArraysExt = "StructArrays"
30+
AccessorsUnitfulExt = "Unitful"
2931

3032
[compat]
3133
AxisKeys = "0.2"

ext/AccessorsStructArraysExt.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Accessors.set(x::StructArray, o::PropertyLens, v) = set(x, o ∘ StructArrays.co
99
Accessors.insert(x::StructArray{<:Union{Tuple, NamedTuple}}, o::PropertyLens, v) = insert(x, o StructArrays.components, v)
1010
Accessors.delete(x::StructArray{<:Union{Tuple, NamedTuple}}, o::PropertyLens) = delete(x, o StructArrays.components)
1111

12+
Accessors.set(x::StructArray{<:Union{Tuple, NamedTuple}}, ::typeof(propertynames), names) = set(x, propertynames StructArrays.components, names)
13+
1214
# (named)tuple eltypes: only component arrays themselves are needed in the constructor
1315
# can change component number/names
1416
Accessors.set(x::StructArray{<:Union{Tuple, NamedTuple}}, ::typeof(StructArrays.components), v) = StructArray(v)

ext/AccessorsUnitfulExt.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module AccessorsUnitfulExt
2+
3+
import Accessors: set
4+
using Unitful
5+
6+
# ustrip(unit, _) works automatically because Unitful defines inverse() for it
7+
# inverse(ustrip) is impossible, so special set() handling is required
8+
set(obj, ::typeof(ustrip), val) = val * unit(obj)
9+
10+
end

src/functionlenses.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ set(obj, ::typeof(Base.splat(=>)), val::Pair) = @set Tuple(obj) = Tuple(val)
4949

5050
set(obj, ::typeof(getproperties), val::NamedTuple) = setproperties(obj, val)
5151

52+
set(x::Union{Tuple,NamedTuple}, ::typeof(propertynames), names) = set(x, propertynames, Tuple(names))
53+
function set(x::Union{Tuple,NamedTuple}, ::typeof(propertynames), names::Tuple)
54+
length(names) == length(x) || throw(ArgumentError("Got $(length(names)) for $(length(x)) properties"))
55+
if eltype(names) === Symbol
56+
NamedTuple{names}(Tuple(x))
57+
elseif eltype(names) <: Integer && names == ntuple(identity, length(names))
58+
Tuple(x)
59+
else
60+
throw(ArgumentError("invalid property names: $names"))
61+
end
62+
end
63+
5264
################################################################################
5365
##### eltype
5466
################################################################################

test/test_extensions.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using AxisKeys
66
using IntervalSets
77
using StaticArrays, StaticNumbers
88
using StructArrays
9+
using Unitful
910

1011

1112
# most of the tests run on Julia 1.9 and later because of package extensions support
@@ -170,6 +171,16 @@ VERSION >= v"1.9-" && @testset "StructArrays" begin
170171
s = StructArray([S(1, 2), S(3, 4)])
171172
@test @inferred(set(s, PropertyLens(:a), 10:11))::StructArray == StructArray([S(10, 2), S(11, 4)])
172173
@test @inferred(set(s, PropertyLens(:a), [:a, :b]))::StructArray == StructArray([S(:a, 2), S(:b, 4)])
174+
175+
@test_throws "need to overload" set(s, propertynames, (:x, :y))
176+
s = StructArray(x=[1, 2], y=[:a, :b])
177+
test_getset_laws(propertynames, s, (:u, :v), (1, 2))
178+
test_getset_laws(propertynames, s, (1, 2), (:u, :v))
179+
end
180+
181+
VERSION v"1.9-" && @testset "Unitful" begin
182+
test_getset_laws(ustrip, 1u"m", 2., 3)
183+
test_getset_laws(ustrip, 1u"m/mm", 2., 3)
173184
end
174185

175186
end

test/test_functionlenses.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ end
109109
test_getset_laws(NamedTuple{(:x, :y)}, CartesianIndex(1, 2), (y=3, x=4), (x=5, y=6); cmp=cmp)
110110

111111
test_getset_laws(Accessors.getproperties, 1+2im, (im=4., re=3.), (re=5, im=6); cmp=cmp)
112+
113+
@testset for obj in [(10, 20.), (x=10, y=20.)]
114+
@test (@set propertynames(obj) = 1:2) === (10, 20.)
115+
@test (@set propertynames(obj) = (:a, :b)) === (a=10, b=20.)
116+
@test_throws Exception (@set propertynames(obj) = 1:3)
117+
@test_throws Exception (@set propertynames(obj) = (2, 3))
118+
@test_throws Exception (@set propertynames(obj) = (:a, :b, :c))
119+
@test_throws Exception (@set propertynames(obj) = (1, :b))
120+
@test_throws Exception (@set propertynames(obj) = ("a", "b"))
121+
end
122+
test_getset_laws(propertynames, (10, 20.), (:a, :b), (1, 2); cmp=(===))
123+
test_getset_laws(propertynames, (10, 20.), (1, 2), (:a, :b); cmp=(===))
124+
test_getset_laws(propertynames, (a=10, b=20.), (:x, :y), (1, 2); cmp=(===))
125+
test_getset_laws(propertynames, (a=10, b=20.), (1, 2), (:x, :y); cmp=(===))
112126
end
113127

114128
@testset "eltype on Number" begin

0 commit comments

Comments
 (0)