Skip to content

Commit 121d1b0

Browse files
committed
Clean up old code, add docs
1 parent c1613f9 commit 121d1b0

File tree

2 files changed

+33
-116
lines changed

2 files changed

+33
-116
lines changed

src/varname.jl

Lines changed: 31 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -754,118 +754,6 @@ function vsym(expr::Expr)
754754
end
755755
end
756756

757-
"""
758-
index_to_str(i)
759-
760-
Generates a string representation of the index `i`, or a tuple thereof.
761-
762-
## Examples
763-
764-
```jldoctest
765-
julia> AbstractPPL.index_to_str(2)
766-
"2"
767-
768-
julia> AbstractPPL.index_to_str((1, 2:5))
769-
"(1, 2:5,)"
770-
771-
julia> AbstractPPL.index_to_str(:)
772-
":"
773-
774-
julia> AbstractPPL.index_to_str(AbstractPPL.ConcretizedSlice(Base.Slice(Base.OneTo(10))))
775-
"ConcretizedSlice(Base.OneTo(10))"
776-
```
777-
"""
778-
index_to_str(i::Integer) = string(i)
779-
index_to_str(r::UnitRange) = "$(first(r)):$(last(r))"
780-
index_to_str(::Colon) = ":"
781-
index_to_str(s::ConcretizedSlice{T,R}) where {T,R} = "ConcretizedSlice(" * repr(s.range) * ")"
782-
index_to_str(t::Tuple) = "(" * join(map(index_to_str, t), ", ") * ",)"
783-
784-
"""
785-
optic_to_nt(optic)
786-
787-
Convert an optic to a named tuple representation.
788-
789-
## Examples
790-
```jldoctest; setup=:(using Accessors)
791-
julia> AbstractPPL.optic_to_nt(identity)
792-
(type = "identity",)
793-
794-
julia> AbstractPPL.optic_to_nt(@optic _.a)
795-
(type = "property", field = "a")
796-
797-
julia> AbstractPPL.optic_to_nt(@optic _.a.b)
798-
(type = "composed", outer = (type = "property", field = "b"), inner = (type = "property", field = "a"))
799-
800-
julia> AbstractPPL.optic_to_nt(@optic _[1]) # uses index_to_str()
801-
(type = "index", indices = "(1,)")
802-
```
803-
"""
804-
optic_to_nt(::typeof(identity)) = (type = "identity",)
805-
optic_to_nt(::PropertyLens{sym}) where {sym} = (type = "property", field = String(sym))
806-
optic_to_nt(i::IndexLens) = (type = "index", indices = index_to_str(i.indices))
807-
optic_to_nt(c::ComposedOptic) = (type = "composed", outer = optic_to_nt(c.outer), inner = optic_to_nt(c.inner))
808-
809-
810-
"""
811-
nt_to_optic(nt)
812-
813-
Convert a named tuple representation back to an optic.
814-
"""
815-
function nt_to_optic(nt)
816-
if nt.type == "identity"
817-
return identity
818-
elseif nt.type == "index"
819-
return IndexLens(eval(Meta.parse(nt.indices)))
820-
elseif nt.type == "property"
821-
return PropertyLens{Symbol(nt.field)}()
822-
elseif nt.type == "composed"
823-
return nt_to_optic(nt.outer) nt_to_optic(nt.inner)
824-
end
825-
end
826-
827-
"""
828-
vn_to_string(vn::VarName)
829-
830-
Convert a `VarName` as a string, via an intermediate named tuple. This differs
831-
from `string(vn)` in that concretised slices are faithfully represented (rather
832-
than being pretty-printed as colons).
833-
834-
```jldoctest
835-
julia> vn_to_string(@varname(x))
836-
"(sym = \\"x\\", optic = (type = \\"identity\\",))"
837-
838-
julia> vn_to_string(@varname(x.a))
839-
"(sym = \\"x\\", optic = (type = \\"property\\", field = \\"a\\"))"
840-
841-
julia> y = ones(2); vn_to_string(@varname(y[:]))
842-
"(sym = \\"y\\", optic = (type = \\"index\\", indices = \\"(:,)\\"))"
843-
844-
julia> y = ones(2); vn_to_string(@varname(y[:], true))
845-
"(sym = \\"y\\", optic = (type = \\"index\\", indices = \\"(ConcretizedSlice(Base.OneTo(2)),)\\"))"
846-
```
847-
"""
848-
vn_to_string(vn::VarName) = repr((sym = String(getsym(vn)), optic = optic_to_nt(getoptic(vn))))
849-
850-
"""
851-
vn_from_string(str)
852-
853-
Convert a string representation of a `VarName` back to a `VarName`. The string
854-
should have been generated by `vn_to_string`.
855-
856-
!!! warning
857-
This function should only be used with trusted input, as it uses `eval`
858-
and `Meta.parse` to parse the string.
859-
"""
860-
function vn_from_string(str)
861-
new_fields = eval(Meta.parse(str))
862-
return VarName{Symbol(new_fields.sym)}(nt_to_optic(new_fields.optic))
863-
end
864-
865-
# -----------------------------------------
866-
# Alternate implementation with StructTypes
867-
# -----------------------------------------
868-
869757
index_to_dict(i::Integer) = Dict("type" => "integer", "value" => i)
870758
index_to_dict(v::AbstractVector{Int}) = Dict("type" => "vector", "values" => v)
871759
index_to_dict(r::UnitRange) = Dict("type" => "unitrange", "start" => r.start, "stop" => r.stop)
@@ -918,6 +806,35 @@ vn_to_dict(vn::VarName) = Dict("sym" => getsym(vn), "optic" => optic_to_dict(get
918806

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

921-
vn_to_string2(vn::VarName) = JSON.json(vn_to_dict(vn))
809+
"""
810+
vn_to_string(vn::VarName)
811+
812+
Convert a `VarName` as a string, via an intermediate dictionary. This differs
813+
from `string(vn)` in that concretised slices are faithfully represented (rather
814+
than being pretty-printed as colons).
815+
816+
Note that this function only supports a subset of AbstractRange
817+
818+
```jldoctest
819+
julia> vn_to_string(@varname(x))
820+
"{\\"optic\\":{\\"type\\":\\"identity\\"},\\"sym\\":\\"x\\"}"
821+
822+
julia> vn_to_string(@varname(x.a))
823+
"{\\"optic\\":{\\"field\\":\\"a\\",\\"type\\":\\"property\\"},\\"sym\\":\\"x\\"}"
824+
825+
julia> y = ones(2); vn_to_string(@varname(y[:]))
826+
"{\\"optic\\":{\\"indices\\":{\\"values\\":[{\\"type\\":\\"colon\\"}],\\"type\\":\\"tuple\\"},\\"type\\":\\"index\\"},\\"sym\\":\\"y\\"}"
922827
923-
vn_from_string2(str) = dict_to_vn(JSON.parse(str))
828+
julia> y = ones(2); vn_to_string(@varname(y[:], true))
829+
"{\\"optic\\":{\\"indices\\":{\\"values\\":[{\\"oneto\\":2,\\"type\\":\\"concretized_slice\\"}],\\"type\\":\\"tuple\\"},\\"type\\":\\"index\\"},\\"sym\\":\\"y\\"}"
830+
```
831+
"""
832+
vn_to_string(vn::VarName) = JSON.json(vn_to_dict(vn))
833+
834+
"""
835+
vn_from_string(str)
836+
837+
Convert a string representation of a `VarName` back to a `VarName`. The string
838+
should have been generated by `vn_to_string`.
839+
"""
840+
vn_from_string(str) = dict_to_vn(JSON.parse(str))

test/varname.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ end
172172
@varname(z[2:5,:], true),
173173
]
174174
for vn in vns
175-
@test vn_from_string2(vn_to_string2(vn)) == vn
175+
@test vn_from_string(vn_to_string(vn)) == vn
176176
end
177177

178178
# For this VarName, the {de,}serialisation works correctly but we must
@@ -181,7 +181,7 @@ end
181181
# addresses rather than the contents (thus vn_vec == vn_vec2 returns
182182
# false).
183183
vn_vec = @varname(x[[1, 2, 5, 6]])
184-
vn_vec2 = vn_from_string2(vn_to_string2(vn_vec))
184+
vn_vec2 = vn_from_string(vn_to_string(vn_vec))
185185
@test hash(vn_vec) == hash(vn_vec2)
186186
end
187187
end

0 commit comments

Comments
 (0)