Skip to content

Commit 9ec542c

Browse files
author
Pietro Vertechi
committed
view preservs metadata
1 parent cf140a2 commit 9ec542c

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# MetadataArrays
22

3+
[![Build Status](https://travis-ci.org/piever/MetadataArrays.jl.svg?branch=master)](https://travis-ci.org/piever/MetadataArrays.jl)
4+
[![codecov.io](http://codecov.io/github/piever/MetadataArrays.jl/coverage.svg?branch=master)](http://codecov.io/github/piever/MetadataArrays.jl?branch=master)
5+
36
Implementation of arrays with metadata.
47

58
## Metadata Arrays
@@ -34,6 +37,16 @@ Dict{String,String} with 3 entries:
3437
"Louise" => "Placebo"
3538
```
3639

40+
`metadata` is preserved when taking views:
41+
42+
```julia
43+
julia> metadata(view(s, 1:2))
44+
Dict{String,String} with 3 entries:
45+
"John" => "Treatment"
46+
"Jane" => "Placebo"
47+
"Louise" => "Placebo"
48+
```
49+
3750
`s` can be used as a regular `AbstractArray` (meaning all operations that work on `AbstractArray` should work on a `MetadataArray` out of the box:
3851

3952
```julia

src/metadataarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ Base.parent(s::MetadataArray) = s.parent
5757
Returns metadata for `s`.
5858
"""
5959
metadata(s::MetadataArray) = s.metadata
60+
61+
metadata(s::SubArray) = metadata(parent(s))
62+
63+
metadata(s::T) where {T<:AbstractArray} =
64+
error("Type $T has no method for metadata")

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ end
1111
@test length(sv) == 4
1212
@test collect(sv) == parent(sv) == copy(sv)
1313
@test metadata(sv) == "Numbers"
14+
s_view1 = view(sv, 1:3)
15+
s_view2 = view(s_view1, 1:2)
16+
@test metadata(s_view1) == metadata(s_view2) == "Numbers"
1417
sv[3] = 50
1518
@test all(sv .== [1, 3, 50, 4])
1619
@test eltype(sv) == Int

0 commit comments

Comments
 (0)