Skip to content

Commit a5a0aa6

Browse files
committed
Simplify implementation even more
1 parent 73a426b commit a5a0aa6

File tree

2 files changed

+39
-62
lines changed

2 files changed

+39
-62
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ version = "0.8.4"
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
1010
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
1111
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
12-
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
12+
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1313
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
14-
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
1514

1615
[compat]
1716
AbstractMCMC = "2, 3, 4, 5"

src/varname.jl

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Accessors
22
using Accessors: ComposedOptic, PropertyLens, IndexLens, DynamicIndexLens
3-
using StructTypes: StructTypes
4-
using JSON3: JSON3
3+
using JSON: JSON
54

65
const ALLOWED_OPTICS = Union{typeof(identity),PropertyLens,IndexLens,ComposedOptic}
76

@@ -867,79 +866,58 @@ end
867866
# Alternate implementation with StructTypes
868867
# -----------------------------------------
869868

870-
index_to_dict(i::Integer) = Dict(:type => "integer", :value => i)
871-
index_to_dict(v::AbstractVector{Int}) = Dict(:type => "vector", :values => v)
872-
index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :start => r.start, :stop => r.stop)
873-
index_to_dict(r::StepRange) = Dict(:type => "steprange", :start => r.start, :stop => r.stop, :step => r.step)
874-
index_to_dict(::Colon) = Dict(:type => "colon")
875-
index_to_dict(s::ConcretizedSlice{T,Base.OneTo{I}}) where {T,I} = Dict(:type => "concretized_slice", :oneto => s.range.stop)
869+
index_to_dict(i::Integer) = Dict("type" => "integer", "value" => i)
870+
index_to_dict(v::AbstractVector{Int}) = Dict("type" => "vector", "values" => v)
871+
index_to_dict(r::UnitRange) = Dict("type" => "unitrange", "start" => r.start, "stop" => r.stop)
872+
index_to_dict(r::StepRange) = Dict("type" => "steprange", "start" => r.start, "stop" => r.stop, "step" => r.step)
873+
index_to_dict(::Colon) = Dict("type" => "colon")
874+
index_to_dict(s::ConcretizedSlice{T,Base.OneTo{I}}) where {T,I} = Dict("type" => "concretized_slice", "oneto" => s.range.stop)
876875
index_to_dict(::ConcretizedSlice{T,R}) where {T,R} = error("ConcretizedSlice with range type $(R) not supported")
877-
index_to_dict(t::Tuple) = Dict(:type => "tuple", :values => [index_to_dict(x) for x in t])
876+
index_to_dict(t::Tuple) = Dict("type" => "tuple", "values" => map(index_to_dict, t))
878877

879878
function dict_to_index(dict)
880-
# conversion needed because of the same reason as in dict_to_optic
881-
dict = Dict(Symbol(k) => v for (k, v) in dict)
882-
if dict[:type] == "integer"
883-
return dict[:value]
884-
elseif dict[:type] == "vector"
885-
return collect(Int, dict[:values])
886-
elseif dict[:type] == "unitrange"
887-
return dict[:start]:dict[:stop]
888-
elseif dict[:type] == "steprange"
889-
return dict[:start]:dict[:step]:dict[:stop]
890-
elseif dict[:type] == "colon"
879+
if dict["type"] == "integer"
880+
return dict["value"]
881+
elseif dict["type"] == "vector"
882+
return collect(Int, dict["values"])
883+
elseif dict["type"] == "unitrange"
884+
return dict["start"]:dict["stop"]
885+
elseif dict["type"] == "steprange"
886+
return dict["start"]:dict["step"]:dict["stop"]
887+
elseif dict["type"] == "colon"
891888
return Colon()
892-
elseif dict[:type] == "concretized_slice"
893-
return ConcretizedSlice(Base.Slice(Base.OneTo(dict[:oneto])))
894-
elseif dict[:type] == "tuple"
895-
return tuple(map(dict_to_index, dict[:values])...)
889+
elseif dict["type"] == "concretized_slice"
890+
return ConcretizedSlice(Base.Slice(Base.OneTo(dict["oneto"])))
891+
elseif dict["type"] == "tuple"
892+
return tuple(map(dict_to_index, dict["values"])...)
896893
else
897-
error("Unknown index type: $(dict[:type])")
894+
error("Unknown index type: $(dict["type"])")
898895
end
899896
end
900897

901-
optic_to_dict(::typeof(identity)) = Dict(:type => "identity")
902-
optic_to_dict(::PropertyLens{sym}) where {sym} = Dict(:type => "property", :field => String(sym))
903-
optic_to_dict(i::IndexLens) = Dict(:type => "index", :indices => index_to_dict(i.indices))
904-
optic_to_dict(c::ComposedOptic) = Dict(:type => "composed", :outer => optic_to_dict(c.outer), :inner => optic_to_dict(c.inner))
898+
optic_to_dict(::typeof(identity)) = Dict("type" => "identity")
899+
optic_to_dict(::PropertyLens{sym}) where {sym} = Dict("type" => "property", "field" => String(sym))
900+
optic_to_dict(i::IndexLens) = Dict("type" => "index", "indices" => index_to_dict(i.indices))
901+
optic_to_dict(c::ComposedOptic) = Dict("type" => "composed", "outer" => optic_to_dict(c.outer), "inner" => optic_to_dict(c.inner))
905902

906903
function dict_to_optic(dict)
907-
# Nested dicts are deserialised to Dict{String, Any}
908-
# but the top level dict is deserialised to Dict{Symbol, Any}
909-
# so for this recursive function to work we need to first
910-
# convert String keys to Symbols
911-
dict = Dict(Symbol(k) => v for (k, v) in dict)
912-
if dict[:type] == "identity"
904+
if dict["type"] == "identity"
913905
return identity
914-
elseif dict[:type] == "index"
915-
return IndexLens(dict_to_index(dict[:indices]))
916-
elseif dict[:type] == "property"
917-
return PropertyLens{Symbol(dict[:field])}()
918-
elseif dict[:type] == "composed"
919-
return dict_to_optic(dict[:outer]) dict_to_optic(dict[:inner])
906+
elseif dict["type"] == "index"
907+
return IndexLens(dict_to_index(dict["indices"]))
908+
elseif dict["type"] == "property"
909+
return PropertyLens{Symbol(dict["field"])}()
910+
elseif dict["type"] == "composed"
911+
return dict_to_optic(dict["outer"]) dict_to_optic(dict["inner"])
920912
else
921-
error("Unknown optic type: $(dict[:type])")
913+
error("Unknown optic type: $(dict["type"])")
922914
end
923915
end
924916

925-
struct VarNameWithDictOptic
926-
sym::Symbol
927-
optic::Dict{Symbol, Any}
928-
end
929-
930-
function VarNameWithDictOptic(dict::Dict{Symbol, Any})
931-
return VarNameWithDictOptic{dict[:sym]}(dict[:optic])
932-
end
917+
vn_to_dict(vn::VarName) = Dict("sym" => getsym(vn), "optic" => optic_to_dict(getoptic(vn)))
933918

934-
# Serialisation
935-
StructTypes.StructType(::Type{VarNameWithDictOptic}) = StructTypes.UnorderedStruct()
919+
dict_to_vn(dict::Dict{<:AbstractString, Any}) = VarName{Symbol(dict["sym"])}(dict_to_optic(dict["optic"]))
936920

937-
vn_to_string2(vn::VarName) = JSON3.write(VarNameWithDictOptic(getsym(vn), optic_to_dict(getoptic(vn))))
921+
vn_to_string2(vn::VarName) = JSON.json(vn_to_dict(vn))
938922

939-
# Deserialisation
940-
Base.pairs(vn::VarNameWithDictOptic) = Dict(:sym => vn.sym, :optic => vn.optic)
941-
942-
function vn_from_string2(str)
943-
vn_nt = JSON3.read(str, VarNameWithDictOptic)
944-
return VarName{vn_nt.sym}(dict_to_optic(vn_nt.optic))
945-
end
923+
vn_from_string2(str) = dict_to_vn(JSON.parse(str))

0 commit comments

Comments
 (0)