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 01cba79
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 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))

0 comments on commit 01cba79

Please sign in to comment.