diff --git a/src/Entity.jl b/src/Entity.jl index ef2ea915da9de96539fefbba550254af8f42b418..e1cdafbf38d9a8f2cd2482501ddb02239822a1bb 100644 --- a/src/Entity.jl +++ b/src/Entity.jl @@ -61,7 +61,8 @@ export append_parent, set_description, set_datatype, set_unit, - set_value + set_value, + set_importance # helper functions export has_errors, has_warnings @@ -790,6 +791,29 @@ function get_importance(property::Ref{_Property}) return GC.@preserve out unsafe_string(pointer(out)) 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) @@ -798,6 +822,17 @@ Return the error message of the given `entity` with the provided """ 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()) err_code = ccall( @@ -830,6 +865,29 @@ function get_error(entity::Ref{_Entity}, index::Integer) 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) @@ -838,6 +896,17 @@ Return the warning message of the given `entity` with the provided """ 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()) err_code = ccall( @@ -870,6 +939,29 @@ function get_warning(entity::Ref{_Entity}, index::Integer) 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) @@ -878,6 +970,17 @@ Return the info message of the given `entity` with the provided """ 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()) err_code = ccall( @@ -918,19 +1021,9 @@ Return a Vector of all error messages attached to the given `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 end @@ -942,19 +1035,9 @@ Return a Vector of all warning messages attached to the given `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 end @@ -967,10 +1050,24 @@ Return a Vector of all info messages attached to the given `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) err_code = ccall( - (:caosdb_entity_entity_get_infos_size, CaosDB.library_name), + (:caosdb_entity_entity_get_parents_size, CaosDB.library_name), Cint, (Ref{_Entity}, Ref{Cint}), entity, @@ -979,9 +1076,8 @@ function get_infos(entity::Ref{_Entity}) CaosDB.Exceptions.evaluate_return_code(err_code) - infos = [get_info(entity, Cint(ii)) for ii = 1:size[]] + return size[] - return infos end """ @@ -991,15 +1087,23 @@ Return the parent of the given `entity` at position `index`. """ function get_parent(entity::Ref{_Entity}, index::Cint) - parent = Ref{_Parent}(_Parent()) + size = get_parents_size(entity) + + if index > size + throw( + DomainError( + index, + "You tried to access the parent at position $index but the entity only has $size parents.", + ), + ) + end - println(index - Cint(1)) - println(typeof(index - Cint(1))) + parent = Ref{_Parent}(_Parent()) err_code = ccall( (:caosdb_entity_entity_get_parent, CaosDB.library_name), Cint, - (Ref{_Entity}, Ref{_Parent}, Ref{Cint}), + (Ref{_Entity}, Ref{_Parent}, Cint), entity, parent, index - Cint(1), @@ -1031,10 +1135,24 @@ Return the vector of all parents of the given `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) err_code = ccall( - (:caosdb_entity_entity_get_parents_size, CaosDB.library_name), + (:caosdb_entity_entity_get_properties_size, CaosDB.library_name), Cint, (Ref{_Entity}, Ref{Cint}), entity, @@ -1043,9 +1161,8 @@ function get_parents(entity::Ref{_Entity}) CaosDB.Exceptions.evaluate_return_code(err_code) - parents = [get_parent(entity, Cint(ii)) for ii = 1:size[]] + return size[] - return parents end """ @@ -1055,12 +1172,23 @@ Return the property of the given `entity` at position `index`. """ 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()) err_code = ccall( (:caosdb_entity_entity_get_property, CaosDB.library_name), Cint, - (Ref{_Entity}, Ref{_Property}, Ref{Cint}), + (Ref{_Entity}, Ref{_Property}, Cint), entity, property, index - Cint(1), @@ -1093,19 +1221,9 @@ Return the vector of all properties of the given `entity`. """ function get_properties(entity::Ref{_Entity}) - size = Ref{Cint}(0) - - 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) + size = get_properties_size(entity) - properties = [get_property(entity, Cint(ii)) for ii = 1:size[]] + properties = [get_property(entity, Cint(ii)) for ii = 1:size] return properties end diff --git a/test/runtests.jl b/test/runtests.jl index cbb339ee74ce7b85b7aa744f1631e9d5cfa249f1..d4879f8d0a3b046cbc5ffe2b35e8500798af3e54 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -123,6 +123,7 @@ using CaosDB @test CaosDB.Entity.get_name( CaosDB.Entity.get_parent(rec_with_parent_and_props, 2), ) == "Parent2" + @test_throws DomainError CaosDB.Entity.get_parent(rec_with_parent_and_props, 3) @test CaosDB.Entity.get_id( CaosDB.Entity.get_parent(rec_with_parent_and_props, Cint(2)), ) == "id_of_parent_2"