JuliaIO / JuliaIO/MAT.jl

Preserve order of struct fields?

Open
#159 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
293
Forks
79
PR merge metrics
No merged PRs in 30d

Description

Hi, would you consider using OrderedDict instead of Dict when reading struct, so that the order of struct fields can be preserved? The following patch has worked for my particular application. (Though I have been wishing Julia's default Dict implementation can preserve order)

diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl
index cfacf3e..f0c8d90 100644
--- a/src/MAT_HDF5.jl
+++ b/src/MAT_HDF5.jl
@@ -32,6 +32,7 @@ using HDF5, SparseArrays
 
 import Base: names, read, write, close
 import HDF5: Reference
+import OrderedCollections: OrderedDict
 
 const HDF5Parent = Union{HDF5.File, HDF5.Group}
 const HDF5BitsOrBool = Union{HDF5.BitsType,Bool}
@@ -230,12 +231,8 @@ function m_read(g::HDF5.Group)
             error("Cannot read from a non-struct group, type was $mattype")
         end
     end
-    if haskey(g, "MATLAB_fields")
-        fn = [join(f) for f in read_attribute(g, "MATLAB_fields")]
-    else
-        fn = keys(g)
-    end
-    s = Dict{String, Any}()
+    fn = [join(f) for f in read_attribute(g, "MATLAB_fields")]
+    s = OrderedDict{String, Any}()
     for i = 1:length(fn)
         dset = g[fn[i]]
         try

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/MAT_HDF5.jl at m_read(g::HDF5.Group), where struct field names are collected and the result is built as a Dict. Compare the proposed OrderedDict and MATLAB_fields handling with the existing fallback behavior. Done means MATLAB structs preserve their field order when read without changing other group-reading behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.