It's no secret I've been working with golang for a few weeks now to get a JSON based web API to backend a project.
My initial approach had me defining objects in golang that may go into or out of the DB and then using gorilla to handle serialization of posted data into & out of those objects and finally using a controller to handle persistence to a mongo store.
After a few weeks of this approach I've come to realize I have problem that I hadn't considered before and I'm not really sure how to address it correctly within Go.
Certain objects are strict models for instance a User comprises a username, password, creation time and a boolean that tells us whether or not this user is activated or not.
But then we get to something that's not so clear cut such as permissions. My point might become more clear if I give you some background on the project.
I work with a healthcare network that is nationwide. They have acquired several smaller organizations over time and the end result is a patchwork of various databases and systems in play, many of which duplicate functionality from eachother, but each has it's own "spin".
My task is to pilot a program to unify the information systems for the entire organization. We are only speaking to documents, workflows and non-email based communications such as an in house chat client.
Of particular interest and concern is the documents area. Because it's healthcare and most documents are either patient medical records or references to such we have to be very, very granular about who gets access to what documents and what privilege level they have within those documents.
For example, Becky in Nebraska works in compliance and should only have read level access to medical records. Her job involves verifying that on the whole a Dr is not just ordering extra tests that are likely to be irrelevant. Without going into how bad that may sound it is still a reality of the task at hand, that I make sure she only has access to documents created by Drs within the groups she is in charge of.
This produces a permissions object that is stored in the database under a collection called permissions looks like
permissions = {
sources:{
midwestclinic: [r],
southvalleyregional: [r],
heartofmercy: [r]
}
}
And this works out ok for Becky's case. However if you consider a clinician, they need read access to every patient record for every patient under their care and read write access for every record created by themselves.
In fact the clinician ends up with a completely different model that looks more like.
permissions = {
allowedtypes :{
medical: {
sources:{
myself: [*],
mylocation:[r],
other: [a] //(a is a flag meaning that we need to record the fact he accessed it, but otherwise it's open for reading)
}
}
}
Besides being significantly different in structure, the other problem we face is that in Becky's instance, the sources field
describes distinct location labels which are stored within the document
themselves. Whereas in the case of the Dr, the sources field is a
description of the relationship of the Dr to the document source. Remapping them into a single object is probably not realistic.
As you can see, these are completely different objects that in javascript could easily be filtered by an if(object.hasOwnProperty()) statement.
The permissions object can be multi-modal and we can't always be confident about the result of marshalling it into a predefined object.
My solution thus far has been to use the gorilla schema and a bunch of attempts to marshall into what I expect the most common permissions structures to look like.
This is producing a lot of boiler plate objects with very similar names who differ by very little, which is an anti-pattern telling me I'm doing it wrong.
It would be much better if we could just arbitrarily check for any field using the equivalent of javascript's, object.hasOwnProperty and then take action based on the presence or lack thereof of fields.
So far my reading on the subject hasn't lead me to an answer.
For the record I've read both
http://blog.golang.org/json-and-go and
http://golang.org/pkg/encoding/json/ and maybe I'm just not fully understanding them.
My lack of finding something suitable tells me I'm either missing something obvious, approaching the task completely wrong, or possibly both.
I'm open to ideas which is why I'm here asking for help and for bearing with me through this, thank you.