Make a method that creates a copy of the Player with erasing the relevant
fields:
type Player struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
CustomField string `json:"customField,omitempty"
bson:"customField,omitempty"`
}
func (player Player) Frontend() Player {
player.CustomField = ""
return player
}
On Wednesday, 6 September 2017 00:36:56 UTC+3, Vincent Jouglard wrote:
>
> Hi guys,
>
> I would like to have fields in a custom type that :
> - are not exported when I mashall them (to send to front)
> - are imported when I decode them (from the from)
>
> This is the type :
> type Player struct {
> Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
> CustomField string `json:"-" bson:"customField,omitempty"`
> }
> -
> - The `json:"-"` works like a charm, the field is not mashalled when I
> send the object to the front end but I would like it to be imported when I
> send an object from to front to the back.
> -
> - This is the json I send in the request payload :
> - {id: "123456789", customField: "123456"}
> -
> if err := decoder.Decode(&newPlayer); err != nil {
> http.Error(w, err.Error(), http.StatusInternalServerError)
> return
> }
>
> -
> -
>
> -
> - What I get is (due to the `json:"-"`):
> - Player {
> Id:132456789
> }
> -
> Do you know a way to keep the field not sent to the front, but when the
> front sends one to the back, all fields are decoded ?
> - I could create another type for the decoder, but that would be a code
> dup and some of my types are quite large...
> -
> - Thanks !
> -
> -
>
> 1.
>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.