为Go中导入的结构设置标签

huangapple go评论131阅读模式
英文:

Set tags for imported struct in Go

问题

我正在使用gormgo orm)从数据库中检索数据,并将其编码为JSON。Gorm为主键和时间跟踪提供了一个默认的结构体,其中的DeletedAt属性不应该被编码为JSON。

我编写了一个小例子,不会输出密码,但DeletedAt属性仍然可见。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/jinzhu/gorm"
  7. _ "github.com/lib/pq"
  8. _ "github.com/mattn/go-sqlite3"
  9. )
  10. // gorm包中的结构体:
  11. //
  12. // type Model struct {
  13. // ID uint `gorm:"primary_key"`
  14. // CreatedAt time.Time
  15. // UpdatedAt time.Time
  16. // DeletedAt *time.Time
  17. // }
  18. // 定义gorm的数据库模型
  19. type User struct {
  20. gorm.Model
  21. Username string `json:"username" sql:"size:32; not null; unique"`
  22. Password string `json:"password" sql:"not null"`
  23. Locale string `json:"locale" sql:"not null"`
  24. }
  25. // 数据库模型的公共JSON版本
  26. type PublicUser struct {
  27. *User
  28. DeletedAt bool `json:"deletedAt,omitempty"`
  29. Password bool `json:"password,omitempty"`
  30. }
  31. func main() {
  32. db, err := gorm.Open("sqlite3", "storage.db")
  33. if err != nil {
  34. fmt.Println(err)
  35. }
  36. u := &User{}
  37. db.Where("id = ?", 3).Find(&u)
  38. json.NewEncoder(os.Stdout).Encode(PublicUser{
  39. User: u,
  40. })
  41. }

如果我运行我的脚本,我得到以下输出:

  1. {
  2. "ID":3,
  3. "CreatedAt":"2015-05-13T14:54:23.5577227Z",
  4. "UpdatedAt":"2015-05-13T14:54:23.5577227Z",
  5. "DeletedAt":null,
  6. "username":"dan",
  7. "locale":"en_US"
  8. }

我修改了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 -->

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;os&quot;
  6. &quot;github.com/jinzhu/gorm&quot;
  7. _ &quot;github.com/lib/pq&quot;
  8. _ &quot;github.com/mattn/go-sqlite3&quot;
  9. )
  10. // Struct from the gorm package:
  11. //
  12. // type Model struct {
  13. // ID uint `gorm:&quot;primary_key&quot;`
  14. // CreatedAt time.Time
  15. // UpdatedAt time.Time
  16. // DeletedAt *time.Time
  17. // }
  18. // Defines the database model for gorn
  19. type User struct {
  20. gorm.Model
  21. Username string `json:&quot;username&quot; sql:&quot;size:32; not null; unique&quot;`
  22. Password string `json:&quot;password&quot; sql:&quot;not null&quot;`
  23. Locale string `json:&quot;locale&quot; sql:&quot;not null&quot;`
  24. }
  25. // Public JSON version of database model
  26. type PublicUser struct {
  27. *User
  28. DeletedAt bool `json:&quot;deletedAt,omitempty&quot;`
  29. Password bool `json:&quot;password,omitempty&quot;`
  30. }
  31. func main() {
  32. db, err := gorm.Open(&quot;sqlite3&quot;, &quot;storage.db&quot;)
  33. if err != nil {
  34. fmt.Println(err)
  35. }
  36. u := &amp;User{}
  37. db.Where(&quot;id = ?&quot;, 3).Find(&amp;u)
  38. json.NewEncoder(os.Stdout).Encode(PublicUser{
  39. User: u,
  40. })
  41. }

This is the ourput I get if I run my script:

  1. {
  2. &quot;ID&quot;:3,
  3. &quot;CreatedAt&quot;:&quot;2015-05-13T14:54:23.5577227Z&quot;,
  4. &quot;UpdatedAt&quot;:&quot;2015-05-13T14:54:23.5577227Z&quot;,
  5. &quot;DeletedAt&quot;:null,
  6. &quot;username&quot;:&quot;dan&quot;,
  7. &quot;locale&quot;:&quot;en_US&quot;
  8. }

I modified the example of Alfred Rossi to imitate the behavior and I got the same result.

答案1

得分: 2

你可以使用一个布尔值将字段设置为false,并使用omitempty进行标记。

例如:

  1. type User struct {
  2. Username string `json:"username"`
  3. DeletedAt int `json:"deleted_at"`
  4. }
  5. type PublicUser struct {
  6. *User
  7. DeletedAt bool `json:"deleted_at,omitempty"`
  8. }

你可以在这里进行尝试。同时,你也可以查看Attila Oláh的博客文章

英文:

You can just shadow the field with a bool set to false and tag it with omitempty

For example

  1. type User struct {
  2. Username string `json:&quot;username&quot;`
  3. DeletedAt int `json:&quot;deleted_at&quot;`
  4. }
  5. type PublicUser struct {
  6. *User
  7. DeletedAt bool `json:&quot;deleted_at,omitempty&quot;`
  8. }

Feel free to play with it here. Also see this blog post of Attila Oláh.

huangapple
  • 本文由 发表于 2015年5月10日 20:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/30151645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定