Skip to content
Snippets Groups Projects
Commit 20939d95 authored by florian's avatar florian
Browse files

ENH: Add size checking to getters

parent 379a133b
Branches
No related tags found
1 merge request!7ENH: Implement queries and entity retrieval
Pipeline #12044 failed
...@@ -61,7 +61,8 @@ export append_parent, ...@@ -61,7 +61,8 @@ export append_parent,
set_description, set_description,
set_datatype, set_datatype,
set_unit, set_unit,
set_value set_value,
set_importance
# helper functions # helper functions
export has_errors, has_warnings export has_errors, has_warnings
...@@ -790,6 +791,29 @@ function get_importance(property::Ref{_Property}) ...@@ -790,6 +791,29 @@ function get_importance(property::Ref{_Property})
return GC.@preserve out unsafe_string(pointer(out)) return GC.@preserve out unsafe_string(pointer(out))
end end
"""
function get_errors_size(entity::Ref{_Entity})
Return the number of error messages attached to the given `entity`.
"""
function get_errors_size(entity::Ref{_Entity})
size = Ref{Cint}(0)
err_code = ccall(
(:caosdb_entity_entity_get_errors_size, CaosDB.library_name),
Cint,
(Ref{_Entity}, Ref{Cint}),
entity,
size,
)
CaosDB.Exceptions.evaluate_return_code(err_code)
return size[]
end
""" """
function get_error(entity::Ref{_Entity}, index::Cint) function get_error(entity::Ref{_Entity}, index::Cint)
...@@ -798,6 +822,17 @@ Return the error message of the given `entity` with the provided ...@@ -798,6 +822,17 @@ Return the error message of the given `entity` with the provided
""" """
function get_error(entity::Ref{_Entity}, index::Cint) function get_error(entity::Ref{_Entity}, index::Cint)
size = get_errors_size(entity)
if index > size
throw(
DomainError(
index,
"You tried to access the error at position $index but the entity only has $size errors.",
),
)
end
error = Ref{_Message}(_Message()) error = Ref{_Message}(_Message())
err_code = ccall( err_code = ccall(
...@@ -830,6 +865,29 @@ function get_error(entity::Ref{_Entity}, index::Integer) ...@@ -830,6 +865,29 @@ function get_error(entity::Ref{_Entity}, index::Integer)
end end
"""
function get_warnings_size(entity::Ref{_Entity})
Return the number of warning messages attached to the given `entity`.
"""
function get_warnings_size(entity::Ref{_Entity})
size = Ref{Cint}(0)
err_code = ccall(
(:caosdb_entity_entity_get_warnings_size, CaosDB.library_name),
Cint,
(Ref{_Entity}, Ref{Cint}),
entity,
size,
)
CaosDB.Exceptions.evaluate_return_code(err_code)
return size[]
end
""" """
function get_warning(entity::Ref{_Entity}, index::Cint) function get_warning(entity::Ref{_Entity}, index::Cint)
...@@ -838,6 +896,17 @@ Return the warning message of the given `entity` with the provided ...@@ -838,6 +896,17 @@ Return the warning message of the given `entity` with the provided
""" """
function get_warning(entity::Ref{_Entity}, index::Cint) function get_warning(entity::Ref{_Entity}, index::Cint)
size = get_warnings_size(entity)
if index > size
throw(
DomainError(
index,
"You tried to access the warning at position $index but the entity only has $size warnings.",
),
)
end
warning = Ref{_Message}(_Message()) warning = Ref{_Message}(_Message())
err_code = ccall( err_code = ccall(
...@@ -870,6 +939,29 @@ function get_warning(entity::Ref{_Entity}, index::Integer) ...@@ -870,6 +939,29 @@ function get_warning(entity::Ref{_Entity}, index::Integer)
end end
"""
function get_infos_size(entity::Ref{_Entity})
Return the number of info messages attached to the given `entity`.
"""
function get_infos_size(entity::Ref{_Entity})
size = Ref{Cint}(0)
err_code = ccall(
(:caosdb_entity_entity_get_infos_size, CaosDB.library_name),
Cint,
(Ref{_Entity}, Ref{Cint}),
entity,
size,
)
CaosDB.Exceptions.evaluate_return_code(err_code)
return size[]
end
""" """
function get_info(entity::Ref{_Entity}, index::Cint) function get_info(entity::Ref{_Entity}, index::Cint)
...@@ -878,6 +970,17 @@ Return the info message of the given `entity` with the provided ...@@ -878,6 +970,17 @@ Return the info message of the given `entity` with the provided
""" """
function get_info(entity::Ref{_Entity}, index::Cint) function get_info(entity::Ref{_Entity}, index::Cint)
size = get_infos_size(entity)
if index > size
throw(
DomainError(
index,
"You tried to access the info at position $index but the entity only has $size infos.",
),
)
end
info = Ref{_Message}(_Message()) info = Ref{_Message}(_Message())
err_code = ccall( err_code = ccall(
...@@ -918,19 +1021,9 @@ Return a Vector of all error messages attached to the given `entity`. ...@@ -918,19 +1021,9 @@ Return a Vector of all error messages attached to the given `entity`.
""" """
function get_errors(entity::Ref{_Entity}) function get_errors(entity::Ref{_Entity})
size = Ref{Cint}(0) size = get_errors_size(entity)
err_code = ccall(
(:caosdb_entity_entity_get_errors_size, CaosDB.library_name),
Cint,
(Ref{_Entity}, Ref{Cint}),
entity,
size,
)
CaosDB.Exceptions.evaluate_return_code(err_code)
errors = [get_error(entity, Cint(ii)) for ii = 1:size[]] errors = [get_error(entity, Cint(ii)) for ii = 1:size]
return errors return errors
end end
...@@ -942,19 +1035,9 @@ Return a Vector of all warning messages attached to the given `entity`. ...@@ -942,19 +1035,9 @@ Return a Vector of all warning messages attached to the given `entity`.
""" """
function get_warnings(entity::Ref{_Entity}) function get_warnings(entity::Ref{_Entity})
size = Ref{Cint}(0) size = get_warnings_size(entity)
err_code = ccall(
(:caosdb_entity_entity_get_warnings_size, CaosDB.library_name),
Cint,
(Ref{_Entity}, Ref{Cint}),
entity,
size,
)
CaosDB.Exceptions.evaluate_return_code(err_code)
warnings = [get_warning(entity, Cint(ii)) for ii = 1:size[]] warnings = [get_warning(entity, Cint(ii)) for ii = 1:size]
return warnings return warnings
end end
...@@ -967,10 +1050,24 @@ Return a Vector of all info messages attached to the given `entity`. ...@@ -967,10 +1050,24 @@ Return a Vector of all info messages attached to the given `entity`.
""" """
function get_infos(entity::Ref{_Entity}) function get_infos(entity::Ref{_Entity})
size = get_infos_size(entity)
infos = [get_info(entity, Cint(ii)) for ii = 1:size]
return infos
end
"""
function get_parents_size(entity::Ref{_Entity})
Return the number of parents attached to the given `entity`.
"""
function get_parents_size(entity::Ref{_Entity})
size = Ref{Cint}(0) size = Ref{Cint}(0)
err_code = ccall( err_code = ccall(
(:caosdb_entity_entity_get_infos_size, CaosDB.library_name), (:caosdb_entity_entity_get_parents_size, CaosDB.library_name),
Cint, Cint,
(Ref{_Entity}, Ref{Cint}), (Ref{_Entity}, Ref{Cint}),
entity, entity,
...@@ -979,9 +1076,8 @@ function get_infos(entity::Ref{_Entity}) ...@@ -979,9 +1076,8 @@ function get_infos(entity::Ref{_Entity})
CaosDB.Exceptions.evaluate_return_code(err_code) CaosDB.Exceptions.evaluate_return_code(err_code)
infos = [get_info(entity, Cint(ii)) for ii = 1:size[]] return size[]
return infos
end end
""" """
...@@ -991,15 +1087,23 @@ Return the parent of the given `entity` at position `index`. ...@@ -991,15 +1087,23 @@ Return the parent of the given `entity` at position `index`.
""" """
function get_parent(entity::Ref{_Entity}, index::Cint) function get_parent(entity::Ref{_Entity}, index::Cint)
parent = Ref{_Parent}(_Parent()) size = get_parents_size(entity)
println(index - Cint(1)) if index > size
println(typeof(index - Cint(1))) throw(
DomainError(
index,
"You tried to access the parent at position $index but the entity only has $size parents.",
),
)
end
parent = Ref{_Parent}(_Parent())
err_code = ccall( err_code = ccall(
(:caosdb_entity_entity_get_parent, CaosDB.library_name), (:caosdb_entity_entity_get_parent, CaosDB.library_name),
Cint, Cint,
(Ref{_Entity}, Ref{_Parent}, Ref{Cint}), (Ref{_Entity}, Ref{_Parent}, Cint),
entity, entity,
parent, parent,
index - Cint(1), index - Cint(1),
...@@ -1031,10 +1135,24 @@ Return the vector of all parents of the given `entity`. ...@@ -1031,10 +1135,24 @@ Return the vector of all parents of the given `entity`.
""" """
function get_parents(entity::Ref{_Entity}) function get_parents(entity::Ref{_Entity})
size = get_parents_size(entity)
parents = [get_parent(entity, Cint(ii)) for ii = 1:size]
return parents
end
"""
function get_properties_size(entity::Ref{_Entity})
Return the number of properties attached to the given `entity`.
"""
function get_properties_size(entity::Ref{_Entity})
size = Ref{Cint}(0) size = Ref{Cint}(0)
err_code = ccall( err_code = ccall(
(:caosdb_entity_entity_get_parents_size, CaosDB.library_name), (:caosdb_entity_entity_get_properties_size, CaosDB.library_name),
Cint, Cint,
(Ref{_Entity}, Ref{Cint}), (Ref{_Entity}, Ref{Cint}),
entity, entity,
...@@ -1043,9 +1161,8 @@ function get_parents(entity::Ref{_Entity}) ...@@ -1043,9 +1161,8 @@ function get_parents(entity::Ref{_Entity})
CaosDB.Exceptions.evaluate_return_code(err_code) CaosDB.Exceptions.evaluate_return_code(err_code)
parents = [get_parent(entity, Cint(ii)) for ii = 1:size[]] return size[]
return parents
end end
""" """
...@@ -1055,12 +1172,23 @@ Return the property of the given `entity` at position `index`. ...@@ -1055,12 +1172,23 @@ Return the property of the given `entity` at position `index`.
""" """
function get_property(entity::Ref{_Entity}, index::Cint) function get_property(entity::Ref{_Entity}, index::Cint)
size = get_properties_size(entity)
if index > size
throw(
DomainError(
index,
"You tried to access the property at position $index but the entity only has $size properties.",
),
)
end
property = Ref{_Property}(_Property()) property = Ref{_Property}(_Property())
err_code = ccall( err_code = ccall(
(:caosdb_entity_entity_get_property, CaosDB.library_name), (:caosdb_entity_entity_get_property, CaosDB.library_name),
Cint, Cint,
(Ref{_Entity}, Ref{_Property}, Ref{Cint}), (Ref{_Entity}, Ref{_Property}, Cint),
entity, entity,
property, property,
index - Cint(1), index - Cint(1),
...@@ -1093,19 +1221,9 @@ Return the vector of all properties of the given `entity`. ...@@ -1093,19 +1221,9 @@ Return the vector of all properties of the given `entity`.
""" """
function get_properties(entity::Ref{_Entity}) function get_properties(entity::Ref{_Entity})
size = Ref{Cint}(0) size = get_properties_size(entity)
err_code = ccall(
(:caosdb_entity_entity_get_properties_size, CaosDB.library_name),
Cint,
(Ref{_Entity}, Ref{Cint}),
entity,
size,
)
CaosDB.Exceptions.evaluate_return_code(err_code)
properties = [get_property(entity, Cint(ii)) for ii = 1:size[]] properties = [get_property(entity, Cint(ii)) for ii = 1:size]
return properties return properties
end end
......
...@@ -123,6 +123,7 @@ using CaosDB ...@@ -123,6 +123,7 @@ using CaosDB
@test CaosDB.Entity.get_name( @test CaosDB.Entity.get_name(
CaosDB.Entity.get_parent(rec_with_parent_and_props, 2), CaosDB.Entity.get_parent(rec_with_parent_and_props, 2),
) == "Parent2" ) == "Parent2"
@test_throws DomainError CaosDB.Entity.get_parent(rec_with_parent_and_props, 3)
@test CaosDB.Entity.get_id( @test CaosDB.Entity.get_id(
CaosDB.Entity.get_parent(rec_with_parent_and_props, Cint(2)), CaosDB.Entity.get_parent(rec_with_parent_and_props, Cint(2)),
) == "id_of_parent_2" ) == "id_of_parent_2"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment