From d3c7b0518ed85185e9ec4bfb65a899034eec95aa Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Sat, 21 Aug 2021 10:38:48 +0200
Subject: [PATCH] DRAFT: Check for correct role when setting a value

---
 src/Entity.jl | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/Entity.jl b/src/Entity.jl
index 90caff7..c41711b 100644
--- a/src/Entity.jl
+++ b/src/Entity.jl
@@ -251,6 +251,8 @@ end
         name::AbstractString = "",
         datatype::AbstractString = "",
         unit::AbstractString = "",
+        is_reference::Bool = false,
+        is_list::Bool = false,
     )
 
 Return a new entity object with role Record. If `name`, `datatype`, or
@@ -284,12 +286,17 @@ end
     function create_property(;
         name::AbstractString = "",
         id::AbstractString = "",
-        value::AbstractString = "",
+        value::Union{AbstractString, Number, Bool} = "",
+        datatype::AbstractString = "",
+        is_reference::Bool = false,
+        is_list::Bool = false,
      )
 
 Create a property object that can be appended to another `_Entity`. If
 `name`, `id`, or `value` are given, the property's name, id, or value
-are set accordingly.
+are set accordingly. The datatype and whether it is a list or a
+reference can be specified with `datatype`, `is_reference`, and
+`is_list`, respectively.
 
 !!! info
 
@@ -299,7 +306,7 @@ are set accordingly.
 function create_property(;
     name::AbstractString = "",
     id::AbstractString = "",
-    value::AbstractString = "",
+    value::Union{AbstractString,Number,Bool} = "",
     datatype::AbstractString = "",
     is_reference::Bool = false,
     is_list::Bool = false,
@@ -1518,13 +1525,23 @@ function set_unit(property::Ref{_Property}, unit::AbstractString)
 end
 
 # TODO(henrik, daniel) overload to choose the correct set
-# function. Should throw an error for entities with role != PROPERTY.
+# function.
 """
     function set_value(entity::Ref{_Entity}, value::AbstractString)
 
-Set the value of the given `entity` object.
+Set the value of the given `entity` object. Throw an error if it
+doesn't have the correct role.
 """
 function set_value(entity::Ref{_Entity}, value::AbstractString)
+
+    if get_role(entity) != "PROPERTY"
+        throw(
+            CaosDB.Exceptions.ClientException(
+                "Only entites with role PROPERTY may be assigned a value.",
+            ),
+        )
+    end
+
     err_code = ccall(
         (:caosdb_entity_entity_set_value, CaosDB.library_name),
         Cint,
-- 
GitLab