868
868
# Alternate implementation with StructTypes
869
869
# -----------------------------------------
870
870
871
+ index_to_dict (i:: Integer ) = Dict (:type => " integer" , :value => i)
872
+ index_to_dict (r:: UnitRange ) = Dict (:type => " unitrange" , :first => first (r), :last => last (r))
873
+ index_to_dict (:: Colon ) = Dict (:type => " colon" )
874
+ index_to_dict (s:: ConcretizedSlice{T,R} ) where {T,R} = Dict (:type => " concretized_slice" , :range => repr (s. range))
875
+ index_to_dict (t:: Tuple ) = Dict (:type => " tuple" , :values => [index_to_dict (x) for x in t])
876
+
877
+ function dict_to_index (dict)
878
+ dict = Dict (Symbol (k) => v for (k, v) in dict)
879
+ if dict[:type ] == " integer"
880
+ return dict[:value ]
881
+ elseif dict[:type ] == " unitrange"
882
+ return dict[:first ]: dict[:last ]
883
+ elseif dict[:type ] == " colon"
884
+ return Colon ()
885
+ elseif dict[:type ] == " concretized_slice"
886
+ return ConcretizedSlice (eval (Meta. parse (dict[:range ])))
887
+ elseif dict[:type ] == " tuple"
888
+ return tuple (map (dict_to_index, dict[:values ])... )
889
+ end
890
+ end
891
+
892
+
871
893
optic_to_dict (:: typeof (identity)) = Dict (:type => " identity" )
872
894
optic_to_dict (:: PropertyLens{sym} ) where {sym} = Dict (:type => " property" , :field => String (sym))
873
- optic_to_dict (i:: IndexLens ) = Dict (:type => " index" , :indices => index_to_str (i. indices))
895
+ optic_to_dict (i:: IndexLens ) = Dict (:type => " index" , :indices => index_to_dict (i. indices))
874
896
optic_to_dict (c:: ComposedOptic ) = Dict (:type => " composed" , :outer => optic_to_dict (c. outer), :inner => optic_to_dict (c. inner))
875
897
876
898
function dict_to_optic (dict)
@@ -882,7 +904,7 @@ function dict_to_optic(dict)
882
904
if dict[:type ] == " identity"
883
905
return identity
884
906
elseif dict[:type ] == " index"
885
- return IndexLens (eval (Meta . parse ( dict[:indices ]) ))
907
+ return IndexLens (dict_to_index ( dict[:indices ]))
886
908
elseif dict[:type ] == " property"
887
909
return PropertyLens {Symbol(dict[:field])} ()
888
910
elseif dict[:type ] == " composed"
0 commit comments