Skip to content

Commit 8671da9

Browse files
Be agnostic to table/struct field order when reinterpreting (#63)
1 parent 291cbdf commit 8671da9

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/LegendHDF5IO.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ include("types.jl")
3434
const _datatype_dict = Dict{String,Type}()
3535

3636
function __init__()
37-
_datatype_dict[datatype_to_string(EventType)] = EventType
38-
_datatype_dict["table{t0,dt,values}"] = Vector{<:RDWaveform}
39-
_datatype_dict["struct{binning,weights,isdensity}"] = Histogram
37+
_datatype_dict[_sort_datatype_fields(datatype_to_string(EventType))] = EventType
38+
_datatype_dict[_sort_datatype_fields("table{t0,dt,values}")] = Vector{<:RDWaveform}
39+
_datatype_dict[_sort_datatype_fields("struct{binning,weights,isdensity}")] = Histogram
4040
end
4141

4242
end # module

src/generic_io.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ function _eldatatype_from_string(s::Union{Nothing,AbstractString})
1919
end
2020
end
2121

22+
function _sort_datatype_fields(s::AbstractString)
23+
m = match(datatype_regexp, s)
24+
m isa Nothing && throw(ArgumentError("Invalid datatype string \"$s\""))
25+
kind = m[1]
26+
field_string = m[6]
27+
if kind != "struct" && kind != "table"
28+
return s
29+
else
30+
fields = split(field_string, ",")
31+
sorted_fields = sort(fields)
32+
return "$kind{"*join(sorted_fields, ",")*"}"
33+
end
34+
end
35+
2236
_ndims(x) = ndims(x)
2337
_ndims(::Type{<:AbstractArray{<:Any,N}}) where {N} = N
2438

@@ -29,6 +43,7 @@ _namedtuple_type(members::AbstractVector{<:AbstractString}) = NamedTuple{(Symbol
2943

3044

3145
function datatype_from_string(s::AbstractString)
46+
s_sorted_fields = _sort_datatype_fields(s)
3247
if s == "real"
3348
RealQuantity
3449
elseif s == "bool"
@@ -37,8 +52,8 @@ function datatype_from_string(s::AbstractString)
3752
String
3853
elseif s == "symbol"
3954
Symbol
40-
elseif haskey(_datatype_dict, s)
41-
_datatype_dict[s]
55+
elseif haskey(_datatype_dict, s_sorted_fields)
56+
_datatype_dict[s_sorted_fields]
4257
else
4358
m = match(datatype_regexp, s)
4459
m isa Nothing && throw(ErrorException("Invalid datatype string \"$s\""))

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using Test
55
Test.@testset verbose=true "Package LegendHDF5IO" begin
66
# include("test_aqua.jl")
77
include("ranges/range_to_namedtuple.jl")
8+
include("test_generic_io.jl")
89
include("histograms/histogram_io.jl")
910
include("test_wrappers.jl")
1011
include("test_docs.jl")

test/test_generic_io.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is a part of LegendHDF5IO.jl, licensed under the MIT License (MIT).
2+
3+
using Test
4+
using LegendHDF5IO
5+
6+
using ArraysOfArrays
7+
using EncodedArrays
8+
using Measurements
9+
using RadiationDetectorSignals
10+
using StatsBase
11+
using TypedTables
12+
using Unitful
13+
14+
@testset verbose=true "test generic IO" begin
15+
@testset verbose=true "data types" begin
16+
@test LegendHDF5IO._sort_datatype_fields("table{values,dt,t0}") == "table{dt,t0,values}"
17+
@test LegendHDF5IO._sort_datatype_fields("struct{weights,isdensity,binning}") == "struct{binning,isdensity,weights}"
18+
@test LegendHDF5IO._sort_datatype_fields("real") == "real"
19+
@test LegendHDF5IO._sort_datatype_fields("array<1>{encoded_array<1>{real}}") == "array<1>{encoded_array<1>{real}}"
20+
end
21+
end

0 commit comments

Comments
 (0)