JuliaData / JuliaData/StructTypes.jl

sometimes dicts are passed to constructors as `Dict{String,Any}`

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

Nobody has claimed this yet.

Dominant language
Julia
Stars
86
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I started out thinking this only happened for nested types, but as I set out to make a MWE, I got even more confused. Here it is

using JSON3, StructTypes

struct Inner
    a::Int
end
Inner(dct) = Inner(dct[:a])

StructTypes.StructType(::Type{Inner}) = StructTypes.DictType()

struct Outer
    I::Inner
end
Outer(dct) = Outer(dct[:I])

StructTypes.StructType(::Type{Outer}) = StructTypes.DictType()


json = JSON3.write(Dict(:I=>Dict(:a=>1)))
JSON3.read(json, Outer)

gives me

ERROR: LoadError: KeyError: key :I not found
Stacktrace:
 [1] getindex at ./dict.jl:467 [inlined]
 [2] Outer(::Dict{String,Any}) at /home/expandingman/src/scrap.jl:13 (repeats 2 times)
 [3] #construct#2 at /home/expandingman/.julia/packages/StructTypes/CpLmq/src/StructTypes.jl:267 [in
lined]
 [4] construct at /home/expandingman/.julia/packages/StructTypes/CpLmq/src/StructTypes.jl:267 [inlin
ed]
 [5] read(::StructTypes.DictType, ::Base.CodeUnits{UInt8,String}, ::Int64, ::Int64, ::UInt8, ::Type{
Outer}, ::Type{Symbol}, ::Type{Any}; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),
Tuple{}}}) at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:342
 [6] read at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:285 [inlined]
 [7] #read#30 at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:279 [inlined]
 [8] read at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:279 [inlined]
 [9] read(::String, ::Type{Outer}; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tu
ple{}}}) at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl:34
 [10] read(::String, ::Type{Outer}) at /home/expandingman/.julia/packages/JSON3/LtYDK/src/structs.jl
:33
 [11] top-level scope at /home/expandingman/src/scrap.jl:19
 [12] include(::String) at ./client.jl:457
 [13] top-level scope at REPL[3]:1
in expression starting at /home/expandingman/src/scrap.jl:19

Usually I would hit this kind of error with Inner where it would try to construct from Dict("a"=>1), I don't know why I suddenly have this problem with the outer one.

As a workaround, it seems that right now one has to define constructors for both Dict{String,Any} and Dict{Symbol,Any} which seems wrong.

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 by running the Julia MWE and tracing the constructor call at StructTypes.jl:267 alongside JSON3's read path at structs.jl:342. Compare how Outer and Inner receive dictionary keys, then verify that the reported nested-construction case no longer requires separate Dict{String,Any} and Dict{Symbol,Any} constructors.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.