在gorm中的结构中获取嵌套对象 [英] Get nested object in structure in gorm
问题描述
我有两个结构:
type GoogleAccount struct {
Id uint64
Token string
}
它代表我的自定义PostgreSQL对象类型(我创建了自己):
It represent my custom PostgreSQL object type (i created myself):
CREATE TYPE GOOGLE_ACCOUNT AS
(
id NUMERIC,
token TEXT
);
下一个结构是数据库中的表:
And next structure is table in DB:
type Client struct {
IdClient uint64 `gorm:"primary_key"`
Name string
PhotoUrl string
ApprovalNumber uint16
Phone string
Password string
HoursOfNotice int8
Google GoogleAccount
}
我的自定义对象嵌套在客户端类型中,并命名为 google
。我尝试通过以下方式读取数据:
And my custom object nested in type Client and named as google
. I've tried to read data by the next way:
var users model.Client
db.First(&users)
但不幸的是,我无法阅读 google
(具有默认值)。我不想使用google_account创建单独的表,也不想将此结构作为客户端表中的单独字段或将其打包为json(创建单独的实体,因为此结构不仅在此表中使用,而且我正在寻找新方法,得到相同的结果,但更为优雅)。 任务不是简化表格中数据的显示方式。我需要对对象进行从Postgres到实体的正确映射。
But unfortunately I can't read field google
(have a default value). I don't want to create separate table with google_account, or make this structure as separated fields in client table or packed it as json (created separate entity, because this structure used not only in this table and I'm searching new ways, that get the same result, but more gracefully). The task is not to simplify the presentation of data in the table. I need to make the correct mapping of the object from postgres to the entity.
现在,我找到了一个解决方案-将Scanner实施到GoogleAccount。但是输入法中的值为[] uint8。如我所料,[] uint8可以转换为字符串,然后可以解析此字符串。该字符串(保留在db中)看起来像(x,x)
-其中x-是值。解析字符串并将值设置为对象的正确方法是吗?还是通过ORM来获得此结果的方法?
Right now I found one solution - implement Scanner to GoogleAccount. But value in the input method is []uint8. As I can suppose, []uint8 can cast to string, and after that I can parse this string. This string (that keep in db) look like (x,x)
- where x - is value. Is the right way, to parse string and set value to object? Or is way to get this result by ORM?
是否可能以嵌套结构对象的形式读取此数据?
Is the possible way, to read this data as nested structure object?
推荐答案
现在,我找到了一个解决方案-将 Scanner 实施为 GoogleAccount
。在 Scan
方法的输入处,我得到了 [] uint8
,最后我将其转换为字符串并进行解析。该字符串(保留在db中)看起来像(x,x)
-其中 x
-是值。当然,这不是实现我目标的正确方法。但是我找不到其他解决方案。
Right now I found one solution - implement Scanner to GoogleAccount
. At input of the Scan
method I got []uint8
, that I cast to string and parse in the end. This string (that keeping in db) look like (x,x)
- where x
- is value. Of course, it is not correct way to achieve my goal. But I couldn't found other solution.
我强烈推荐使用按关系的经典绑定,或者简单地将表中的这些字段保留为最简单的值(不作为对象)。
I highly recommended use classical binding by relationship or simple keeping these fields in table as simplest value (not as object).
但是,如果您想在表中尝试嵌套对象,可以看看我的实现,也许对您有用:
But if you would like to experiment with nested object in table, you can look at my realization, maybe it would be useful for you:
type Client struct {
// many others fields
Google GoogleAccount `json:"google"`
}
type GoogleAccount struct {
Id uint64 `json:"id"`
Token string `json:"token"`
}
func (google GoogleAccount) Value() (driver.Value, error) {
return "(" + strconv.FormatUint(google.Id, 10) + "," + google.Token + ")", nil
}
func (google *GoogleAccount) Scan(value interface{}) error {
values := utils.GetValuesFromObject(value)
google.Id, _ = strconv.ParseUint(values[0], 10, 64)
google.Token = values[1]
return nil
}
这篇关于在gorm中的结构中获取嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!