Hi Maksym!
To define your custom and named "value-type checker" (uuid), either for object parameters, method parameters, or both, you have to define a corresponding method on two built-in objects:
... on ::nx::ObjectParameterSlot for the scope of object parameters; ... on ::nx::methodParameterSlot for the scope of method parameters;
Watch:
::nx::ObjectParameterSlot method type=uuid {name value} { set pattern {^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$} if {![regexp $pattern $value]} { error "Value '$value' of parameter $name is not UUID" } return $value }
nx::Class create myClass { :property -accessor public {user:uuid,required} }
set obj [myClass new -user 5a40e63a-9317-11ed-a1eb-0242ac120002]; # ok puts [$obj user get]
myClass new -user nope-9317-11ed-a1eb-0242ac120002; # not ok
Some more remarks:
- the method names must carry the prefix "type="; - the checker methods must return the validated value (or, a derived one); - To create a method-parameter checker, you have to define an object method "type=uuid" on ::nx::methodParameterSlot, like:
::nx::methodParameterSlot object method type=uuid {name value} { set pattern {^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$} if {![regexp $pattern $value]} { error "Value '$value' of parameter $name is not UUID" } return $value }
(Note the difference: "object" modifier!)
... to avoid a duplicated and use a shared definition, you may create an alias:
set m [::nx::ObjectParameterSlot method type=uuid {name value} { # stripped for clarity }]
::nx::methodParameterSlot object alias type=uuid $m
Then, you can use:
myClass public method foo {user:uuid} { puts "Accepted: $user" }
$obj foo 5a40e63a-9317-11ed-a1eb-0242ac120002; # ok $obj foo nope-9317-11ed-a1eb-0242ac120002; # not ok
This part is kinda blurry for me, what does it means define a name?
"Name" refers to a custom, self-chosen name "uuid", that can be then used to specify value constraints on object and/ or method parameters.
If you have ideas, suggestions for improvement regarding documentation, we will be happy to consider them.
HTH, Stefan