Hide fields in Golang Gorm
Asked Answered
Z

5

6

I am using Gorm in my Golang project. Exctly I have a Rest-API and I get a request make the process and return an object, so, for example I have a struct User like this:

type User struct {
    gorm.Model
    Password []byte
    Active bool
    Email string
    ActivationToken string
    RememberPasswordToken string
}

Now, when I create a User I am encoding it to JSON with:

json.NewEncoder(w).Encode(user)

But in the client side I am receiving some fields that I don't really want to send/receive, for example: Created_At, Deleted_At, Updated_At, Password. So, what is the best way to ignore or hide that fields in the response? I saw that I can use a library called Reflect, but it seems to be a lot of work for a simple thing and I want to know if there's another way. Thank you very much

Zaffer answered 16/5, 2017 at 13:39 Comment(5)
Either have separate models for what you're going to be returning and storing in the database, don't export the field, or you can use a json attribute to ignore the field i.e. Password []byte `json:'-'`Fabiano
@Gavin, is it even possible to change those attributes on an external struct?Myopic
@Myopic When was the struct mentioned to be external?Fabiano
gorm.Model contains anonymous fields within the User struct. I'm assuming OP wants to restrict visibility to those fields.Myopic
@Myopic Ah, yeah you're right. I would probably make a separate model for the response then. That sounds like the easiest solution and also separates the database specific information from what the end user sees.Fabiano
C
6

As Gavin said, I would suggest having two separate models and giving the model the ability to convert to the correct return type.

models/user.go

package models

type User struct {
    gorm.Model
    Password []byte
    Active bool
    Email string
    ActivationToken string
    RememberPasswordToken string
}

func (u *User) UserToUser() app.User {
    return app.User{
        Email: u.Email
    }
}

app/user.go

package app

type User struct {
    Email string
}
Chromatics answered 16/5, 2017 at 16:41 Comment(1)
if we use preload/joins do we have to loop every object to convert it to other struct?Graduated
W
14

If you want to have a fixed object to return, you can change the tags with json:"-" to decide the elements to send with json. For the elements in the gorm.Model:

type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

You can substitute them for your own struct:

type OwnModel struct {
    ID        uint       `gorm:"primary_key"`
    CreatedAt time.Time  `json:"-"`
    UpdatedAt time.Time  `json:"-"`
    DeletedAt *time.Time `json:"-";sql:"index"`
}

So, your User struct would be like that:

type User struct {
    OwnModel
    Password []byte
    Active bool
    Email string
    ActivationToken string
    RememberPasswordToken string
}

The other User fields is your decision to add or not `json:"-"` tag.
Wildfowl answered 9/5, 2018 at 5:36 Comment(0)
C
13

For me helped add json:"-" to gorm.Model

For example:

type User struct {
    gorm.Model `json:"-"`
    Password []byte
    Active bool
    Email string
    ActivationToken string
    RememberPasswordToken string
}
Cavin answered 13/3, 2019 at 12:8 Comment(0)
C
6

As Gavin said, I would suggest having two separate models and giving the model the ability to convert to the correct return type.

models/user.go

package models

type User struct {
    gorm.Model
    Password []byte
    Active bool
    Email string
    ActivationToken string
    RememberPasswordToken string
}

func (u *User) UserToUser() app.User {
    return app.User{
        Email: u.Email
    }
}

app/user.go

package app

type User struct {
    Email string
}
Chromatics answered 16/5, 2017 at 16:41 Comment(1)
if we use preload/joins do we have to loop every object to convert it to other struct?Graduated
K
0

I found a way to hide the relation to my User model from my Sessions model by creating an inherited struct with an omitempty

// Trick to hide the User assocation from the results
type Session struct {
    models.Session
    User *models.User `json:"User,omitempty"`
}

func Token(c *fiber.Ctx) error {
    user := auth.User(c)
    var sessions []Session
    database.Db.Find(&sessions, "user_id = ?", user.ID)
    return render.Render(c, sessions)
}

I hope this helps - i also added a feature request - i think this would be great built-in to gorm

https://github.com/go-gorm/gorm/issues/5746

Kukri answered 9/10, 2022 at 1:17 Comment(0)
F
-2
type OrmModel struct {
    gorm.Model `json:"-"`
    ID        uint  `gorm:"primarykey" json:"id,omitempty"`
}
type User struct {
    OrmModel
    Password []byte
    Active bool
    Email string
    ActivationToken string
    RememberPasswordToken string
}
Frenchify answered 20/7, 2023 at 16:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.