Skip to content

Commit

Permalink
broke everything
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydellavecchia committed Nov 21, 2024
1 parent 32bb5ee commit 47e3f3f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 140 deletions.
4 changes: 2 additions & 2 deletions src/Serialization/Groups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function save_object(s::SerializerState, G::FinGenAbGroup)
end

function load_object(s::DeserializerState, ::Type{FinGenAbGroup})
return abelian_group(load_object(s, Matrix, ZZRingElem))
return abelian_group(load_object(s, Matrix{ZZRingElem}))
end

# elems
Expand All @@ -254,5 +254,5 @@ function save_object(s::SerializerState, g::FinGenAbGroupElem)
end

function load_object(s::DeserializerState, ::Type{FinGenAbGroupElem}, G::FinGenAbGroup)
return G(vec(load_object(s, Matrix, ZZRingElem)))
return G(vec(load_object(s, Matrix{ZZRingElem})))
end
133 changes: 123 additions & 10 deletions src/Serialization/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ function type_params(obj::S) where {T, S <:MatVecType{T}}
return S, params[1]
end

function save_type_params(s::SerializerState, T::Type{Vector{S}}, ::Nothing) where S
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_object(s, encode_type(S), :params)
end
end

function save_type_params(s::SerializerState, T::Type, params::Vector)
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_array(s, :params) do
for param in params
save_type_params(s, param...)
end
end
end
end

function load_type_params(s::DeserializerState, T::Type{<: Union{MatVecType, Set}})
subtype, params = load_type_params(s, :params)
return T{subtype}, params
end

function save_object(s::SerializerState, x::Vector)
save_data_array(s) do
for elem in x
Expand Down Expand Up @@ -119,6 +142,16 @@ function type_params(obj::T) where T <: Tuple
for i in 1:n]
end

function load_type_params(s::DeserializerState, T::Type{Tuple})
subtype, params = load_node(s, :params) do _
tuple_params = load_array_node(s) do _
load_type_params(s)
end
return collect(zip(tuple_params...))
end
return T{subtype...}, params
end

function save_object(s::SerializerState, obj::Tuple)
save_data_array(s) do
for entry in obj
Expand All @@ -136,7 +169,7 @@ function load_object(s::DeserializerState, T::Type{<:Tuple}, params::Tuple)
entries = load_array_node(s) do (i, entry)
S = fieldtype(T, i)
if serialize_with_id(S)
return load_ref(s)
return load_ref(s)
else
return load_object(s, S, params[i])
end
Expand All @@ -152,6 +185,38 @@ function type_params(obj::T) where T <: NamedTuple
return T, NamedTuple(map(x -> x.first => type_params(x.second), collect(pairs(obj))))
end


# Named Tuples need to preserve order so they are handled seperate from Dict
function save_type_params(s::SerializerState, T::Type{<:NamedTuple}, params::Vector{<:Pair{Symbol, S}}) where S
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_data_array(s, :names) do
for param in params
save_object(s, param.first)
end
end
save_data_array(s, :tuple_params) do
for param in params
save_type_params(s, param.second...)
end
end
end
end
end

function load_type_params(s::DeserializerState, T::Type{NamedTuple})
subtype, params = load_node(s, :params) do obj
tuple_params = load_array_node(s, :tuple_params) do _
load_type_params(s)
end
tuple_types, named_tuple_params = collect(zip(tuple_params...))
names = load_object(s, Vector{Symbol}, :names)
return (tuple(names...), Tuple{tuple_types...}), named_tuple_params
end
return T{subtype...}, params
end

function save_object(s::SerializerState, obj::NamedTuple)
save_object(s, values(obj))
end
Expand All @@ -167,6 +232,62 @@ function type_params(obj::T) where T <: Dict
return T, Dict(map(x -> x.first => type_params(x.second), collect(pairs(obj))))
end

function save_type_params(s::SerializerState, T::Type{<:Dict{S, U}}, params::Vector{<:Pair{S, V}}) where {U, V, S <: Union{Int, String}}
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_object(s, encode_type(S), :key_type)
isempty(params) && save_object(s, encode_type(U), :value_type)
for param in params
save_type_params(s, param.second..., Symbol(param.first))
end
end
end
end

function save_type_params(s::SerializerState, T::Type{<:Dict{Symbol, U}},
params::Vector{<:Pair{Symbol, V}}) where {U, V}
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_object(s, encode_type(Symbol), :key_type)
isempty(params) && save_object(s, encode_type(U), :value_type)
for param in params
save_type_params(s, param.second..., Symbol(param.first))
end
end
end
end

function load_type_params(s::DeserializerState, T::Type{Dict})
subtype, params = load_node(s, :params) do obj
S = load_node(s, :key_type) do _
decode_type(s)
end
params_dict = Dict{S, Any}()
if S <: Union{String, Symbol, Int}
value_types = Type[]
for (k, _) in obj
k == :key_type && continue
if k == :value_type
load_node(s, k) do _
push!(value_types, decode_type(s))
end
continue
end
key = S == Int ? parse(Int, string(k)) : S(k)
params_dict[key] = load_type_params(s, k)
push!(value_types, params_dict[key][1])
end
return (S, Union{value_types...}), params_dict
else
error{"not implented yet"}
end
end

return Dict{subtype...}, params
end

function save_object(s::SerializerState, obj::Dict{S, T}) where {S <: Union{Symbol, String, Int}, T}
save_data_dict(s) do
for (k, v) in obj
Expand All @@ -177,17 +298,9 @@ function save_object(s::SerializerState, obj::Dict{S, T}) where {S <: Union{Symb
end
end

#function save_object(s::SerializerState, obj::Dict)
# save_data_array(s) do
# for (k, v) in obj
# save_object(s, (k, v))
# end
# end
#end

function load_object(s::DeserializerState,
T::Type{<:Dict{S, U}},
params::Dict{S, V}) where {S <: BasicKeyTypes, U, V}
params::Dict{S, V}) where {S <: Union{Symbol, String, Int}, U, V}
dict = T()
for k in keys(params)
dict[k] = load_object(s, params[k]..., Symbol(k))
Expand Down
136 changes: 8 additions & 128 deletions src/Serialization/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,74 +201,10 @@ function save_type_params(s::SerializerState, T::Type, params::Tuple{Type, S}) w
end
end

function save_type_params(s::SerializerState, T::Type{Vector{S}}, ::Nothing) where S
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_object(s, encode_type(S), :params)
end
end

function save_type_params(s::SerializerState, T::Type, params::Vector)
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_array(s, :params) do
for param in params
save_type_params(s, param...)
end
end
end
end

function save_type_params(s::SerializerState, T::Type, params::S) where S <: Union{Dict, NamedTuple}
save_type_params(s, T, collect(pairs(params)))
end

# Named Tuples need to preserve order so they are handled seperate from Dict
function save_type_params(s::SerializerState, T::Type{<:NamedTuple}, params::Vector{<:Pair{Symbol, S}}) where S
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_data_array(s, :names) do
for param in params
save_object(s, param.first)
end
end
save_data_array(s, :tuple_params) do
for param in params
save_type_params(s, param.second...)
end
end
end
end
end

function save_type_params(s::SerializerState, T::Type{<:Dict{S, U}}, params::Vector{<:Pair{S, V}}) where {U, V, S <: Union{Int, String}}
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_object(s, encode_type(S), :key_type)
isempty(params) && save_object(s, encode_type(U), :value_type)
for param in params
save_type_params(s, param.second..., Symbol(param.first))
end
end
end
end

# this function is exactly the same as above, it is here to distinguish from the function below
function save_type_params(s::SerializerState, T::Type{<:Dict{Symbol, U}}, params::Vector{<:Pair{Symbol, V}}) where {U, V}
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_object(s, encode_type(Symbol), :key_type)
isempty(params) && save_object(s, encode_type(U), :value_type)
for param in params
save_type_params(s, param.second..., Symbol(param.first))
end
end
end
end

function save_type_params(s::SerializerState, T::Type,
params::Vector{<:Pair{Symbol, S}}) where S
save_data_dict(s) do
Expand Down Expand Up @@ -312,73 +248,17 @@ function load_type_params(s::DeserializerState)
end
return T, nothing
end

!haskey(s, :params) && return T, load_typed_object(s)

return load_type_params(s, T)
end

function load_type_params(s::DeserializerState, T::Type{<: Union{MatVecType, Set}})
subtype, params = load_type_params(s, :params)
return T{subtype}, params
end

function load_type_params(s::DeserializerState, T::Type{Tuple})
subtype, params = load_node(s, :params) do _
tuple_params = load_array_node(s) do _
load_type_params(s)
end
return collect(zip(tuple_params...))
end
return T{subtype...}, params
end

function load_type_params(s::DeserializerState, T::Type{NamedTuple})
subtype, params = load_node(s, :params) do obj
tuple_params = load_array_node(s, :tuple_params) do _
load_type_params(s)
end
tuple_types, named_tuple_params = collect(zip(tuple_params...))
names = load_object(s, Vector{Symbol}, :names)
return (tuple(names...), Tuple{tuple_types...}), named_tuple_params
end
return T{subtype...}, params
end

function load_type_params(s::DeserializerState, T::Type{Dict})
subtype, params = load_node(s, :params) do obj
S = load_node(s, :key_type) do _
decode_type(s)
end
params_dict = Dict{S, Any}()
if S <: Union{String, Symbol, Int}
value_types = Type[]
for (k, _) in obj
k == :key_type && continue
if k == :value_type
load_node(s, k) do _
push!(value_types, decode_type(s))
end
continue
end
key = S == Int ? parse(Int, string(k)) : S(k)
params_dict[key] = load_type_params(s, k)
push!(value_types, params_dict[key][1])
end
return (S, Union{value_types...}), params_dict
else
error{"not implented yet"}
end
if haskey(s, :params)
return T, load_type_params(s, T)
else
return T, load_typed_object
end

return Dict{subtype...}, params
end

function load_type_params(s::DeserializerState, T::Type)
subtype, params = load_node(s, :params) do obj
load_type_params(s)
function load_type_params(s::DeserializerState, T::Type)
load_node(s, :params) do obj
return load_type_params(s)
end
return T, params
end

function load_typed_object(s::DeserializerState, key::Symbol; override_params::Any = nothing)
Expand All @@ -400,7 +280,7 @@ function load_typed_object(s::DeserializerState; override_params::Any = nothing)
params = override_params
end
else
s.obj isa String && !isnothing(tryparse(UUID, node)) && return load_ref(s)
s.obj isa String && !isnothing(tryparse(UUID, s.obj)) && return load_ref(s)
T, params = load_type_params(s, type_key)
end
load_node(s, :data) do _
Expand Down

0 comments on commit 47e3f3f

Please sign in to comment.