Skip to content

Commit bc507ef

Browse files
authored
Merge pull request #138 from piever/pv/structarrays
fuller StructArrays integration
2 parents 43d1026 + 1c6115a commit bc507ef

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1414
[compat]
1515
IterTools = "1.3.0"
1616
StaticArrays = "0.12, 1.0"
17-
StructArrays = "0.3,0.4, 0.5"
17+
StructArrays = "0.5"
1818
Tables = "0.2, 1"
1919
julia = "1.3"
2020

src/metadata.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ macro meta_type(name, mainfield, supertype, params...)
158158
return ($field, Names...)
159159
end
160160

161+
function StructArrays.component(x::$MetaName{$(params_sym...),Typ,Names,Types},
162+
field::Symbol) where {$(params...),Typ,Names,Types}
163+
return getproperty(x, field)
164+
end
165+
161166
function StructArrays.staticschema(::Type{$MetaName{$(params_sym...),Typ,Names,
162167
Types}}) where {$(params...),
163168
Typ,Names,Types}

test/runtests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,29 @@ end
684684
@test Base.size(feat[1]) == (2,)
685685
end
686686

687+
@testset "StructArrays integration" begin
688+
pt = meta(Point(0.0, 0.0), color="red", alpha=0.1)
689+
@test StructArrays.component(pt, :position) == Point(0.0, 0.0)
690+
@test StructArrays.component(pt, :color) == "red"
691+
@test StructArrays.component(pt, :alpha) == 0.1
692+
@test StructArrays.staticschema(typeof(pt)) ==
693+
NamedTuple{(:position, :color, :alpha), Tuple{Point2{Float64}, String, Float64}}
694+
@test StructArrays.createinstance(typeof(pt), Point(0.0, 0.0), "red", 0.1) == pt
695+
696+
s = StructArray([pt, pt])
697+
@test StructArrays.components(s) == (
698+
position = [Point(0.0, 0.0), Point(0.0, 0.0)],
699+
color = ["red", "red"],
700+
alpha = [0.1, 0.1]
701+
)
702+
s[2] = meta(Point(0.1, 0.1), color="blue", alpha=0.3)
703+
@test StructArrays.components(s) == (
704+
position = [Point(0.0, 0.0), Point(0.1, 0.1)],
705+
color = ["red", "blue"],
706+
alpha = [0.1, 0.3]
707+
)
708+
end
709+
687710
@testset "Tests from GeometryTypes" begin
688711
include("geometrytypes.jl")
689712
end

0 commit comments

Comments
 (0)