@@ -450,6 +450,7 @@ struct ConcretizedSlice{T,R} <: AbstractVector{T}
450
450
end
451
451
452
452
ConcretizedSlice (s:: Base.Slice{R} ) where {R} = ConcretizedSlice {eltype(s.indices),R} (s. indices)
453
+ ConcretizedSlice (s:: Base.OneTo{R} ) where {R} = ConcretizedSlice (Base. Slice (s))
453
454
Base. show (io:: IO , s:: ConcretizedSlice ) = print (io, " :" )
454
455
Base. show (io:: IO , :: MIME"text/plain" , s:: ConcretizedSlice ) =
455
456
print (io, " ConcretizedSlice(" , s. range, " )" )
@@ -663,8 +664,6 @@ function drop_escape(expr::Expr)
663
664
return Expr (expr. head, map (x -> drop_escape (x), expr. args)... )
664
665
end
665
666
666
- varname_from_str (str:: AbstractString ) = eval (drop_escape (varname (Meta. parse (str))))
667
-
668
667
function _parse_obj_optic (ex)
669
668
obj, optics = _parse_obj_optics (ex)
670
669
optic = Expr (:call , Accessors. opticcompose, optics... )
@@ -749,3 +748,62 @@ function vsym(expr::Expr)
749
748
error (" Malformed variable name `$(expr) `!" )
750
749
end
751
750
end
751
+
752
+ """
753
+ index_to_str(i)
754
+
755
+ Generates a string representation of the index `i`, or a tuple thereof.
756
+ """
757
+ index_to_str (i:: Integer ) = string (i)
758
+ index_to_str (r:: UnitRange ) = " $(first (r)) :$(last (r)) "
759
+ index_to_str (:: Colon ) = " :"
760
+ index_to_str (s:: ConcretizedSlice{T,R} ) where {T,R} = " ConcretizedSlice(" * repr (s. range) * " )"
761
+ index_to_str (t:: Tuple ) = " (" * join (map (index_to_str, t), " , " ) * " ,)"
762
+
763
+ """
764
+ optic_to_nt(optic)
765
+
766
+ Convert an optic to a named tuple representation.
767
+ """
768
+ optic_to_nt (:: typeof (identity)) = (type = " identity" ,)
769
+ optic_to_nt (:: PropertyLens{sym} ) where {sym} = (type = " property" , field = String (sym))
770
+ optic_to_nt (i:: IndexLens ) = (type = " index" , indices = index_to_str (i. indices))
771
+ optic_to_nt (c:: ComposedOptic ) = (type = " composed" , outer = optic_to_nt (c. outer), inner = optic_to_nt (c. inner))
772
+
773
+
774
+ """
775
+ nt_to_optic(nt)
776
+
777
+ Convert a named tuple representation back to an optic.
778
+ """
779
+ function nt_to_optic (nt)
780
+ if nt. type == " identity"
781
+ return identity
782
+ elseif nt. type == " index"
783
+ return IndexLens (eval (Meta. parse (nt. indices)))
784
+ elseif nt. type == " property"
785
+ return PropertyLens {Symbol(nt.field)} ()
786
+ elseif nt. type == " composed"
787
+ return nt_to_optic (nt. outer) ∘ nt_to_optic (nt. inner)
788
+ end
789
+ end
790
+
791
+ """
792
+ vn_to_string(vn::VarName)
793
+
794
+ Convert a `VarName` as a string (via an intermediate named tuple).
795
+ """
796
+ vn_to_string (vn:: VarName ) = repr ((sym = String (getsym (vn)), optic = optic_to_nt (getoptic (vn))))
797
+
798
+ """
799
+ vn_from_string(str)
800
+
801
+ Convert a string representation of a `VarName` back to a `VarName`.
802
+
803
+ NOTE: This function should only be used with trusted input, as it uses `eval`
804
+ and `Meta.parse` to parse the string.
805
+ """
806
+ function vn_from_string (str)
807
+ new_fields = eval (Meta. parse (str))
808
+ return VarName {Symbol(new_fields.sym)} (nt_to_optic (new_fields. optic))
809
+ end
0 commit comments