-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from daniel-thom/serialize-attributes
Support serialization of supplemental attributes
- Loading branch information
Showing
14 changed files
with
235 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This is an abstraction of a Set in order to enable de-serialization of supplemental | ||
# attributes. | ||
|
||
struct ComponentUUIDs <: InfrastructureSystemsType | ||
uuids::Set{Base.UUID} | ||
|
||
function ComponentUUIDs(uuids = Set{Base.UUID}()) | ||
new(uuids) | ||
end | ||
end | ||
|
||
Base.copy(x::ComponentUUIDs) = copy(x.uuids) | ||
Base.delete!(x::ComponentUUIDs, uuid) = delete!(x.uuids, uuid) | ||
Base.empty!(x::ComponentUUIDs) = empty!(x.uuids) | ||
Base.filter!(f, x::ComponentUUIDs) = filter!(f, x.uuids) | ||
Base.in(x, y::ComponentUUIDs) = in(x, y.uuids) | ||
Base.isempty(x::ComponentUUIDs) = isempty(x.uuids) | ||
Base.iterate(x::ComponentUUIDs, args...) = iterate(x.uuids, args...) | ||
Base.length(x::ComponentUUIDs) = length(x.uuids) | ||
Base.pop!(x::ComponentUUIDs) = pop!(x.uuids) | ||
Base.pop!(x::ComponentUUIDs, y) = pop!(x.uuids, y) | ||
Base.pop!(x::ComponentUUIDs, y, default) = pop!(x.uuids, y, default) | ||
Base.push!(x::ComponentUUIDs, y) = push!(x.uuids, y) | ||
Base.setdiff!(x::ComponentUUIDs, y::ComponentUUIDs) = setdiff!(x.uuids, y.uuids) | ||
Base.sizehint!(x::ComponentUUIDs, newsz) = sizehint!(x.uuids, newsz) | ||
|
||
function deserialize(::Type{ComponentUUIDs}, data::Dict) | ||
uuids = Set{Base.UUID}() | ||
for uuid in data["uuids"] | ||
push!(uuids, deserialize(Base.UUID, uuid)) | ||
end | ||
return ComponentUUIDs(uuids) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
All components must include a field of this type in order to store supplemental attributes. | ||
""" | ||
struct SupplementalAttributesContainer | ||
data::SupplementalAttributesByType | ||
end | ||
|
||
function SupplementalAttributesContainer(; data = SupplementalAttributesByType()) | ||
return SupplementalAttributesContainer(data) | ||
end | ||
|
||
Base.getindex(x::SupplementalAttributesContainer, key) = getindex(x.data, key) | ||
Base.haskey(x::SupplementalAttributesContainer, key) = haskey(x.data, key) | ||
Base.isempty(x::SupplementalAttributesContainer) = isempty(x.data) | ||
Base.iterate(x::SupplementalAttributesContainer, args...) = iterate(x.data, args...) | ||
Base.length(x::SupplementalAttributesContainer) = length(x.data) | ||
Base.values(x::SupplementalAttributesContainer) = values(x.data) | ||
Base.delete!(x::SupplementalAttributesContainer, key) = delete!(x.data, key) | ||
Base.empty!(x::SupplementalAttributesContainer) = empty!(x.data) | ||
Base.setindex!(x::SupplementalAttributesContainer, val, key) = setindex!(x.data, val, key) | ||
Base.pop!(x::SupplementalAttributesContainer, key) = pop!(x.data, key) | ||
|
||
function serialize(container::SupplementalAttributesContainer) | ||
return [serialize(uuid) for attrs in values(container) for uuid in keys(attrs)] | ||
end | ||
|
||
function deserialize( | ||
::Type{SupplementalAttributesContainer}, | ||
uuids::Vector, | ||
system_attributes::Dict{Base.UUID, <:InfrastructureSystemsSupplementalAttribute}, | ||
) | ||
container = SupplementalAttributesContainer() | ||
for uuid_dict in uuids | ||
uuid = deserialize(Base.UUID, uuid_dict) | ||
attribute = system_attributes[uuid] | ||
type = typeof(attribute) | ||
if !haskey(container, type) | ||
container[type] = Dict{Base.UUID, InfrastructureSystemsSupplementalAttribute}() | ||
end | ||
if haskey(container[type], uuid) | ||
error( | ||
"Bug: component supplemental attribute container already has uuid = $uuid", | ||
) | ||
end | ||
container[type][uuid] = attribute | ||
end | ||
|
||
return container | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.