I'm using CouchDB to store my models. CouchDB uses JSON and HTTP for 
communication. Here is a simple user model

type User struct {
  Name string `json:"name"`
  PasswordHash string `json:"passwordHash"`
}

On the other side I'm also using JSON over HTTP to communicate with my 
frontend code. Obviously I do not want to send the whole user model, 
including private fields like PasswordHash, down the line.

Therefore my question: How can I hide private fields from marshaling them 
to JSON when talking to my frontend. When talking to my backend the private 
fields must be included.

Currently I'm using two models. A PublicUser and a PrivateUser

type PublicUser struct {
  Name string `json:"name"`
}

type PrivateUser struct {
  PublicUser
  PasswordHash string `json:"passwordHash"`
}

So whenever I talk to my database I use the PrivateUser and whenever I talk 
to the frontend I use the PublicUser. It works fine but it doesn't feel 
good. I have to duplicate every single model. Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to