From e25d8098937b4ba9728ec6f8fcab731c4e8a84d1 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Mon, 20 Jun 2022 01:27:10 +0300 Subject: [PATCH 1/2] throw if no fields --- src/utils.jl | 1 + test/runtests.jl | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 5f4e9936..04ca24b1 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -33,6 +33,7 @@ end 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)) +buildfromschema(initializer, ::Type, ::Type{NamedTuple{(), Tuple{}}}) = error("Only structs with fields are supported") """ StructArrays.buildfromschema(initializer, T[, S]) diff --git a/test/runtests.jl b/test/runtests.jl index aabf8c90..19d789cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -433,6 +433,9 @@ end t = StructVector([(a=1,), (a=missing,)])::StructVector @test isequal(t.a, [1, missing]) @test eltype(t) <: NamedTuple{(:a,)} + + @test_throws Exception StructArray([nothing]) + @test_throws Exception StructArray([1, 2, 3]) end @testset "tuple case" begin From 78f341c5cfc7c906cbc17e34d0b0bcd073af1585 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 26 Aug 2022 18:31:45 +0300 Subject: [PATCH 2/2] after code review --- src/structarray.jl | 13 +++++-------- src/utils.jl | 1 - 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/structarray.jl b/src/structarray.jl index a4c5acba..128d1e49 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -14,12 +14,11 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N} components::C function StructArray{T, N, C}(c) where {T, N, C<:Tup} - if length(c) > 0 - ax = axes(first(c)) - length(ax) == N || error("wrong number of dimensions") - map(tail(c)) do ci - axes(ci) == ax || error("all field arrays must have same shape") - end + isempty(c) && error("Only eltypes with fields are supported") + ax = axes(first(c)) + length(ax) == N || error("wrong number of dimensions") + map(tail(c)) do ci + axes(ci) == ax || error("all field arrays must have same shape") end new{T, N, C, index_type(c)}(c) end @@ -333,9 +332,7 @@ staticschema(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = staticschema( createinstance(::Type{<:StructArray{T}}, args...) where {T} = StructArray{T}(args) Base.size(s::StructArray) = size(components(s)[1]) -Base.size(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (0,) Base.axes(s::StructArray) = axes(components(s)[1]) -Base.axes(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (1:0,) """ StructArrays.get_ith(cols::Union{Tuple,NamedTuple}, I...) diff --git a/src/utils.jl b/src/utils.jl index 04ca24b1..5f4e9936 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -33,7 +33,6 @@ end 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)) -buildfromschema(initializer, ::Type, ::Type{NamedTuple{(), Tuple{}}}) = error("Only structs with fields are supported") """ StructArrays.buildfromschema(initializer, T[, S])