I have the following model:
User
...
Group
...
Sharing
objectId (Either UserId GroupId)
In Sharing
entity I want to store either UserId
or GroupId
and differentiate between them. Simply using Either
doesn't work:
- Not in scope: type constructor or class `UserId'
- Not in scope: type constructor or class `GroupId'
Adding a new sum-type also doesn't work:
data SharingIdType = SharingUserId UserId | SharingGroupId GroupId
- Not in scope: type constructor or class `SharingIdType'
Moving SharingIdType
into another module isn't possible, because it uses UserId
and GroupId
types. The only way I see is to create an entity for each sharing type, like UserSharing
/GroupSharing
.
Other than that, how to approach this problem?
After searching for some time and thinking about it I concluded there are two possible solutions:
If number of SharingIdType
s is static or rarely changes (means, it is OK to recompile the source to change it or alter the DB schema), the proper way to handle the problem is to have to entities for each sharing type:
User
...
Group
...
UserSharing
userId UserId
GroupSharing
groupId GroupId
Here the "sumness" of the problem is moved to DB queries. Whenever I need to find out with what something shared, I make two selectList
s and query two tables instead of one.
If number of SharingIdTypes
needs to be altered dynamically, the SharingType
entity is needed:
User
...
Group
...
SharingType
description String
Sharing
objectId SharingTypeId
This table is filled up with values corresponding to SharingIdType
s constructors:
do
insert $ SharingType "user"
insert $ SharingType "group"
Now whenever we share something, we refer SharingTypeId
.