Skip to content

Commit

Permalink
Support similar with nonconcrete types (#237)
Browse files Browse the repository at this point in the history
* support similar() with non-concrete types

* unify map_params implementation

* update tests

* extra unit tests

* slight test tweak

* streamline tests

Co-authored-by: Alexander <alexander@plav.in>

Co-authored-by: Alexander Plavin <alexander@plav.in>
  • Loading branch information
piever and aplavin authored Jun 25, 2022
1 parent b398788 commit 3fe67c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StructArrays"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.10"
version = "0.6.11"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
9 changes: 4 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ julia> StructArrays.map_params(T -> Complex{T}, Tuple{Int32,Float64})
(Complex{Int32}, Complex{Float64})
```
"""
map_params(f::F, ::Type{NamedTuple{names, types}}) where {F, names, types} =
NamedTuple{names}(map_params(f, types))
map_params(f::F, ::Type{T}) where {F, T<:Tup} = strip_params(T)(map_params_as_tuple(f, T))

function map_params(f::F, ::Type{T}) where {F, T<:Tuple}
function map_params_as_tuple(f::F, ::Type{T}) where {F, T<:Tup}
if @generated
types = fieldtypes(T)
args = map(t -> :(f($t)), types)
Expr(:tuple, args...)
else
map_params_fallback(f, T)
map_params_as_tuple_fallback(f, T)
end
end

map_params_fallback(f, ::Type{T}) where {T<:Tuple} = map(f, fieldtypes(T))
map_params_as_tuple_fallback(f, ::Type{T}) where {T<:Tup} = map(f, fieldtypes(T))

buildfromschema(initializer::F, ::Type{T}) where {F, T} = buildfromschema(initializer, T, staticschema(T))

Expand Down
64 changes: 54 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ end
@test size(s) == (3, 5)
@test s isa StructArray

for ET in (
NamedTuple{(:x,)},
NamedTuple{(:x,), Tuple{NamedTuple{(:y,)}}},
NamedTuple{(:x, :y), Tuple{Int, S}} where S
)
s = similar(t, ET, (3, 5))
@test eltype(s) === ET
@test size(s) == (3, 5)
@test s isa StructArray
end

s = similar(t, Any, (3, 5))
@test eltype(s) == Any
@test size(s) == (3, 5)
@test s isa Array

s = similar(t, (0:2, 5))
@test eltype(s) == NamedTuple{(:a, :b), Tuple{Float64, Bool}}
@test axes(s) == (0:2, 1:5)
Expand Down Expand Up @@ -413,6 +429,10 @@ end
@test size(t) == (5,)
@test t == convert(StructVector, v)
@test t == convert(StructVector, t)

t = StructVector([(a=1,), (a=missing,)])::StructVector
@test isequal(t.a, [1, missing])
@test eltype(t) <: NamedTuple{(:a,)}
end

@testset "tuple case" begin
Expand Down Expand Up @@ -1118,6 +1138,12 @@ end
@test t isa Vector
@test t == [1, 2, 3]

t = map(x -> (a=x.a,), StructVector(a=[1, missing]))::StructVector
@test isequal(t.a, [1, missing])
@test eltype(t) <: NamedTuple{(:a,)}
t = map(x -> (a=rand(["", 1, nothing]),), StructVector(a=1:10))::StructVector
@test eltype(t) <: NamedTuple{(:a,)}

t = VERSION >= v"1.7" ? @inferred(map(x -> (a=x.a, b=2), s)) : map(x -> (a=x.a, b=2), s)
@test t isa StructArray
@test map(x -> (a=x.a, b=2), s) == [(a=1, b=2), (a=2, b=2), (a=3, b=2)]
Expand Down Expand Up @@ -1182,19 +1208,37 @@ end
@testset "map_params" begin
v = StructArray(rand(ComplexF64, 2, 2))
f(T) = similar(v, T)

types = Tuple{Int, Float64, ComplexF32, String}
A = @inferred StructArrays.map_params(f, types)
B = StructArrays.map_params_fallback(f, types)
@test typeof(A) === typeof(B)
namedtypes = NamedTuple{(:a, :b, :c, :d), types}
A = @inferred StructArrays.map_params_as_tuple(f, types)
B = StructArrays.map_params_as_tuple_fallback(f, types)
C = @inferred StructArrays.map_params_as_tuple(f, namedtypes)
D = StructArrays.map_params_as_tuple_fallback(f, namedtypes)
@test typeof(A) === typeof(B) === typeof(C) === typeof(D)

types = Tuple{Int, Float64, ComplexF32}
A = @inferred StructArrays.map_params(zero, types)
B = StructArrays.map_params_fallback(zero, types)
C = map(zero, fieldtypes(types))
@test A === B === C
A = map(zero, fieldtypes(types))
B = @inferred StructArrays.map_params(zero, types)
C = StructArrays.map_params_as_tuple(zero, types)
D = StructArrays.map_params_as_tuple_fallback(zero, types)
@test A === B === C === D

namedtypes = NamedTuple{(:a, :b, :c), types}
A = @inferred StructArrays.map_params(zero, namedtypes)
C = map(zero, NamedTuple{(:a, :b, :c)}(map(zero, fieldtypes(types))))
@test A === C
A = map(zero, NamedTuple{(:a, :b, :c)}(map(zero, fieldtypes(types))))
B = @inferred StructArrays.map_params(zero, namedtypes)
C = StructArrays.map_params_as_tuple(zero, types)
D = StructArrays.map_params_as_tuple_fallback(zero, types)
@test A === B
@test Tuple(A) === C === D

nonconcretenamedtypes = NamedTuple{(:a, :b, :c)}
A = map(f, NamedTuple{(:a, :b, :c)}((Any, Any, Any)))
B = @inferred StructArrays.map_params(f, nonconcretenamedtypes)
C = StructArrays.map_params_as_tuple(f, nonconcretenamedtypes)
D = StructArrays.map_params_as_tuple_fallback(f, nonconcretenamedtypes)
@test typeof(A) === typeof(B)
@test typeof(Tuple(A)) === typeof(C) === typeof(D)
end

@testset "OffsetArray zero" begin
Expand Down

2 comments on commit 3fe67c7

@piever
Copy link
Collaborator Author

@piever piever commented on 3fe67c7 Jun 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/63088

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.11 -m "<description of version>" 3fe67c7b28b9a17dfd7aef694cc94a478451cfff
git push origin v0.6.11

Please sign in to comment.