英文:
Set tags for imported struct in Go
问题
我正在使用gorm(go orm)从数据库中检索数据,并将其编码为JSON。Gorm为主键和时间跟踪提供了一个默认的结构体,其中的DeletedAt属性不应该被编码为JSON。
我编写了一个小例子,不会输出密码,但DeletedAt属性仍然可见。
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
// gorm包中的结构体:
//
// type Model struct {
// ID uint `gorm:"primary_key"`
// CreatedAt time.Time
// UpdatedAt time.Time
// DeletedAt *time.Time
// }
// 定义gorm的数据库模型
type User struct {
gorm.Model
Username string `json:"username" sql:"size:32; not null; unique"`
Password string `json:"password" sql:"not null"`
Locale string `json:"locale" sql:"not null"`
}
// 数据库模型的公共JSON版本
type PublicUser struct {
*User
DeletedAt bool `json:"deletedAt,omitempty"`
Password bool `json:"password,omitempty"`
}
func main() {
db, err := gorm.Open("sqlite3", "storage.db")
if err != nil {
fmt.Println(err)
}
u := &User{}
db.Where("id = ?", 3).Find(&u)
json.NewEncoder(os.Stdout).Encode(PublicUser{
User: u,
})
}
如果我运行我的脚本,我得到以下输出:
{
"ID":3,
"CreatedAt":"2015-05-13T14:54:23.5577227Z",
"UpdatedAt":"2015-05-13T14:54:23.5577227Z",
"DeletedAt":null,
"username":"dan",
"locale":"en_US"
}
我修改了Alfred Rossi的示例以模拟这种行为,并得到了相同的结果。
英文:
I'm using gorm (go orm) to retrieve data from my database that will be encoded in JSON. Gorm provides a default struct for primary keys and time tracking, whose DeletedAt attribute should not be encoded in JSON.
I've written a small example, that doesn't output the password but the DeletedAt attribute is still visible.
<!-- language: lang-go -->
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
// Struct from the gorm package:
//
// type Model struct {
// ID uint `gorm:"primary_key"`
// CreatedAt time.Time
// UpdatedAt time.Time
// DeletedAt *time.Time
// }
// Defines the database model for gorn
type User struct {
gorm.Model
Username string `json:"username" sql:"size:32; not null; unique"`
Password string `json:"password" sql:"not null"`
Locale string `json:"locale" sql:"not null"`
}
// Public JSON version of database model
type PublicUser struct {
*User
DeletedAt bool `json:"deletedAt,omitempty"`
Password bool `json:"password,omitempty"`
}
func main() {
db, err := gorm.Open("sqlite3", "storage.db")
if err != nil {
fmt.Println(err)
}
u := &User{}
db.Where("id = ?", 3).Find(&u)
json.NewEncoder(os.Stdout).Encode(PublicUser{
User: u,
})
}
This is the ourput I get if I run my script:
{
"ID":3,
"CreatedAt":"2015-05-13T14:54:23.5577227Z",
"UpdatedAt":"2015-05-13T14:54:23.5577227Z",
"DeletedAt":null,
"username":"dan",
"locale":"en_US"
}
I modified the example of Alfred Rossi to imitate the behavior and I got the same result.
答案1
得分: 2
你可以使用一个布尔值将字段设置为false,并使用omitempty
进行标记。
例如:
type User struct {
Username string `json:"username"`
DeletedAt int `json:"deleted_at"`
}
type PublicUser struct {
*User
DeletedAt bool `json:"deleted_at,omitempty"`
}
你可以在这里进行尝试。同时,你也可以查看Attila Oláh的博客文章。
英文:
You can just shadow the field with a bool set to false and tag it with omitempty
For example
type User struct {
Username string `json:"username"`
DeletedAt int `json:"deleted_at"`
}
type PublicUser struct {
*User
DeletedAt bool `json:"deleted_at,omitempty"`
}
Feel free to play with it here. Also see this blog post of Attila Oláh.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论