@@ -754,118 +754,6 @@ function vsym(expr::Expr)
754
754
end
755
755
end
756
756
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
-
869
757
index_to_dict (i:: Integer ) = Dict (" type" => " integer" , " value" => i)
870
758
index_to_dict (v:: AbstractVector{Int} ) = Dict (" type" => " vector" , " values" => v)
871
759
index_to_dict (r:: UnitRange ) = Dict (" type" => " unitrange" , " start" => r. start, " stop" => r. stop)
@@ -918,6 +806,33 @@ vn_to_dict(vn::VarName) = Dict("sym" => getsym(vn), "optic" => optic_to_dict(get
918
806
919
807
dict_to_vn (dict:: Dict{<:AbstractString, Any} ) = VarName {Symbol(dict["sym"])} (dict_to_optic (dict[" optic" ]))
920
808
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
+ ```jldoctest
817
+ julia> vn_to_string(@varname(x))
818
+ "{\\ "optic\\ ":{\\ "type\\ ":\\ "identity\\ "},\\ "sym\\ ":\\ "x\\ "}"
819
+
820
+ julia> vn_to_string(@varname(x.a))
821
+ "{\\ "optic\\ ":{\\ "field\\ ":\\ "a\\ ",\\ "type\\ ":\\ "property\\ "},\\ "sym\\ ":\\ "x\\ "}"
822
+
823
+ julia> y = ones(2); vn_to_string(@varname(y[:]))
824
+ "{\\ "optic\\ ":{\\ "indices\\ ":{\\ "values\\ ":[{\\ "type\\ ":\\ "colon\\ "}],\\ "type\\ ":\\ "tuple\\ "},\\ "type\\ ":\\ "index\\ "},\\ "sym\\ ":\\ "y\\ "}"
825
+
826
+ julia> y = ones(2); vn_to_string(@varname(y[:], true))
827
+ "{\\ "optic\\ ":{\\ "indices\\ ":{\\ "values\\ ":[{\\ "oneto\\ ":2,\\ "type\\ ":\\ "concretized_slice\\ "}],\\ "type\\ ":\\ "tuple\\ "},\\ "type\\ ":\\ "index\\ "},\\ "sym\\ ":\\ "y\\ "}"
828
+ ```
829
+ """
830
+ vn_to_string (vn:: VarName ) = JSON. json (vn_to_dict (vn))
831
+
832
+ """
833
+ vn_from_string(str)
922
834
923
- vn_from_string2 (str) = dict_to_vn (JSON. parse (str))
835
+ Convert a string representation of a `VarName` back to a `VarName`. The string
836
+ should have been generated by `vn_to_string`.
837
+ """
838
+ vn_from_string (str) = dict_to_vn (JSON. parse (str))
0 commit comments