V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
jss
V2EX  ›  Go 编程语言

gorm 结构体中定义了关联关系,某些接口不需要时如何取消

  •  
  •   jss · 2019-07-11 20:00:47 +08:00 via iPhone · 2950 次点击
    这是一个创建于 1722 天前的主题,其中的信息可能已经有所发展或是发生改变。
    某些接口不需要关联数据,在查询数据时如何取消,即不显示空的关联结构体?
    11 条回复    2019-07-12 10:47:43 +08:00
    janxin
        1
    janxin  
       2019-07-11 20:25:49 +08:00
    看看文档? 印象中有的
    jss
        2
    jss  
    OP
       2019-07-11 20:35:09 +08:00
    @janxin 文档看过了,帮我研究下
    pubby
        3
    pubby  
       2019-07-11 21:11:02 +08:00
    是指接口返回数据中隐藏一些信息吗 ?


    func handlerApiXXX(w httpResponseWriter,r *http.Request) {

    var user model.User
    //.. get User

    type T struct{
    model.User

    // 隐藏 password_hash, salt 信息
    OmitPassword bool `json:"password_hash,omitempty"`
    OmitSalt bool `json:"salt,omitempty"`
    }

    var t = T{User: user}
    _ = json.NewEcode(w).Encode(t)
    }
    jss
        4
    jss  
    OP
       2019-07-11 21:27:42 +08:00
    @pubby 是这样的
    type user struct {
    ID int32
    Name string

    //关联账户
    Account Account `gorm:"foreignkey:UserId"`
    }

    正常查询结果是有 Account 关联数据的,但是另一个接口 不需要 Account 的数据 ,我如何在不改变结构体关联关系而取消关联,(即不在查询结果中只显示 ID,Name;不显示 Account 数据结构)
    jss
        5
    jss  
    OP
       2019-07-11 21:30:51 +08:00
    @pubby
    是这样的
    type user struct {

    ID int32
    Name string
    //关联账户
    Account Account `gorm:"foreignkey:UserId"`

    }

    type Account struct {

    ID int32
    UserId int32
    ......

    }

    正常查询结果是有 Account 关联数据的,但是另一个接口 不需要 Account 的数据 ,我如何在不改变结构体关联关系而取消关联,(即在查询结果中只显示 ID,Name;不显示 Account 数据结构)
    Aoang
        6
    Aoang  
       2019-07-11 21:37:26 +08:00 via Android
    type T struct {
    Username string `json:"username"`

    Password string `json:"-"`
    }
    pubby
        7
    pubby  
       2019-07-11 21:48:00 +08:00
    type User struct {
    ID int32
    Name string
    //关联账户
    Account Account `gorm:"foreignkey:UserId" json:"-"` // json 结果将不包含 Account 信息
    }

    如果 User 是其他包的无法修改

    就在输出时临时定义一个 struct,利用 omitempty 特性把空值移除
    type T struct {
    User
    OmitAccount bool `json:"Account,omitempty"` // 只要这里是空值 false, json 输出将不会包含 Account
    }
    sunmoon1983
        8
    sunmoon1983  
       2019-07-11 22:16:13 +08:00
    插眼
    eslizn
        9
    eslizn  
       2019-07-11 22:17:37 +08:00
    jss
        10
    jss  
    OP
       2019-07-12 10:08:09 +08:00
    @pubby omitempty 可行,但是对于列表查询如何处理里面的 “ account ” 如:
    var list []User

    数据结构如下:
    {
    "data":[
    {
    "id": 1,
    "name":"name",
    "account":{
    "id":0,
    "mobile":"",
    "password":""
    }
    },{
    "id": 2,
    "name":"names",
    "account":{
    "id":0,
    "mobile":"",
    "password":""
    }
    },
    ]
    }
    pubby
        11
    pubby  
       2019-07-12 10:47:43 +08:00 via Android   ❤️ 1
    @jss 循环一遍转成[]T 啊

    转的地方多就包装一个新类型转

    type Users []User
    func (users Users) Convert() []T

    //.....
    }

    Users(users).Convert()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5460 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:33 · PVG 09:33 · LAX 18:33 · JFK 21:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.