Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/StructArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ function refvalue(s::StructArray{T}, v::Tup) where {T}
createinstance(T, map(refvalue, components(s), v)...)
end

# implement colmetadata for StructArray based on metadata of individual columns
import DataAPI: metadata, metadatasupport, colmetadata, colmetadatasupport

colmetadatasupport(::Type{T}) where {T<:StructArray} = (
read=any(col -> metadatasupport(col).read, array_types(T).parameters),
write=false, # not implemented
)
colmetadata(sa::StructArray, col::Symbol) = metadata(getproperty(sa, col))
colmetadata(sa::StructArray) =
map(Tables.columns(sa)) do col
metadatasupport(typeof(col)).read ? metadata(col) : nothing
end

@static if !isdefined(Base, :get_extension)
include("../ext/StructArraysAdaptExt.jl")
include("../ext/StructArraysGPUArraysCoreExt.jl")
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,24 @@ end
end
end

@testset "metadata" begin
using DataAPI: colmetadatasupport, colmetadata, metadata, DataAPI

struct MyArray <: AbstractVector{Int} end
Base.size(::MyArray) = (2,)
DataAPI.metadatasupport(::Type{<:MyArray}) = (read=true, write=false)
DataAPI.metadata(::MyArray) = (x=1, y="2")

sa = StructArray(a=[1,2], b=[1,2])
@test colmetadatasupport(typeof(sa)) == (read=false, write=false)

sa = StructArray(a=MyArray(), b=[1,2])
@test metadata(sa.a) == (x=1, y="2")
@test colmetadatasupport(typeof(sa)) == (read=true, write=false)
@test colmetadata(sa, :a) == (x=1, y="2")
@test colmetadata(sa) == (a=(x=1, y="2"), b=nothing)
end

struct ArrayConverter end

Adapt.adapt_storage(::ArrayConverter, xs::AbstractArray) = convert(Array, xs)
Expand Down