Skip to content

Commit

Permalink
Name functions more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Sep 30, 2024
1 parent 15ee516 commit e0e5322
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ vsym
```@docs
index_to_dict
dict_to_index
vn_to_string
vn_from_string
varname_to_string
string_to_varname
```

## Abstract model functions
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractPPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export VarName,
@vsym,
index_to_dict,
dict_to_index,
vn_to_string,
vn_from_string
varname_to_string,
string_to_varname


# Abstract model functions
Expand Down
22 changes: 11 additions & 11 deletions src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -857,12 +857,12 @@ function dict_to_optic(dict)
end
end

vn_to_dict(vn::VarName) = Dict("sym" => getsym(vn), "optic" => optic_to_dict(getoptic(vn)))
varname_to_dict(vn::VarName) = Dict("sym" => getsym(vn), "optic" => optic_to_dict(getoptic(vn)))

dict_to_vn(dict::Dict{<:AbstractString, Any}) = VarName{Symbol(dict["sym"])}(dict_to_optic(dict["optic"]))
dict_to_varname(dict::Dict{<:AbstractString, Any}) = VarName{Symbol(dict["sym"])}(dict_to_optic(dict["optic"]))

"""
vn_to_string(vn::VarName)
varname_to_string(vn::VarName)
Convert a `VarName` as a string, via an intermediate dictionary. This differs
from `string(vn)` in that concretised slices are faithfully represented (rather
Expand All @@ -875,25 +875,25 @@ if you are using custom index types, you will need to implement the
documentation of [`dict_to_index`](@ref) for instructions on how to do this.
```jldoctest
julia> vn_to_string(@varname(x))
julia> varname_to_string(@varname(x))
"{\\"optic\\":{\\"type\\":\\"identity\\"},\\"sym\\":\\"x\\"}"
julia> vn_to_string(@varname(x.a))
julia> varname_to_string(@varname(x.a))
"{\\"optic\\":{\\"field\\":\\"a\\",\\"type\\":\\"property\\"},\\"sym\\":\\"x\\"}"
julia> y = ones(2); vn_to_string(@varname(y[:]))
julia> y = ones(2); varname_to_string(@varname(y[:]))
"{\\"optic\\":{\\"indices\\":{\\"values\\":[{\\"type\\":\\"Base.Colon\\"}],\\"type\\":\\"Base.Tuple\\"},\\"type\\":\\"index\\"},\\"sym\\":\\"y\\"}"
julia> y = ones(2); vn_to_string(@varname(y[:], true))
julia> y = ones(2); varname_to_string(@varname(y[:], true))
"{\\"optic\\":{\\"indices\\":{\\"values\\":[{\\"range\\":{\\"stop\\":2,\\"type\\":\\"Base.OneTo\\"},\\"type\\":\\"AbstractPPL.ConcretizedSlice\\"}],\\"type\\":\\"Base.Tuple\\"},\\"type\\":\\"index\\"},\\"sym\\":\\"y\\"}"
```
"""
vn_to_string(vn::VarName) = JSON.json(vn_to_dict(vn))
varname_to_string(vn::VarName) = JSON.json(varname_to_dict(vn))

"""
vn_from_string(str::AbstractString)
string_to_varname(str::AbstractString)
Convert a string representation of a `VarName` back to a `VarName`. The string
should have been generated by `vn_to_string`.
should have been generated by `varname_to_string`.
"""
vn_from_string(str::AbstractString) = dict_to_vn(JSON.parse(str))
string_to_varname(str::AbstractString) = dict_to_varname(JSON.parse(str))
8 changes: 4 additions & 4 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ end
@varname(z[2:5,:], true),
]
for vn in vns
@test vn_from_string(vn_to_string(vn)) == vn
@test string_to_varname(varname_to_string(vn)) == vn
end

# For this VarName, the {de,}serialisation works correctly but we must
Expand All @@ -181,7 +181,7 @@ end
# addresses rather than the contents (thus vn_vec == vn_vec2 returns
# false).
vn_vec = @varname(x[[1, 2, 5, 6]])
vn_vec2 = vn_from_string(vn_to_string(vn_vec))
vn_vec2 = string_to_varname(varname_to_string(vn_vec))
@test hash(vn_vec) == hash(vn_vec2)
end

Expand All @@ -191,13 +191,13 @@ end
vn = @varname(weird[:], true)

# This won't work as we don't yet know how to handle OffsetArray
@test_throws MethodError vn_to_string(vn)
@test_throws MethodError varname_to_string(vn)

# Now define the relevant methods
AbstractPPL.index_to_dict(o::OffsetArrays.IdOffsetRange{I, R}) where {I,R} = Dict("type" => "OffsetArrays.OffsetArray", "parent" => AbstractPPL.index_to_dict(o.parent), "offset" => o.offset)
AbstractPPL.dict_to_index(::Val{Symbol("OffsetArrays.OffsetArray")}, d) = OffsetArrays.IdOffsetRange(AbstractPPL.dict_to_index(d["parent"]), d["offset"])

# Serialisation should now work
@test vn_from_string(vn_to_string(vn)) == vn
@test string_to_varname(varname_to_string(vn)) == vn
end
end

0 comments on commit e0e5322

Please sign in to comment.