如何忽略 Google Golang 数据存储中结构中的零值?
How to ignore zero value in struct in Google Datastore for Golang?
我正在尝试使用 Google Datastore 通过 Go 存储数据。由于 EndDate
是可选字段,并且不想在该字段中存储零值。如果我为时间字段创建指针,Google Datastore 将发送一条错误消息 - datastore: unsupported struct field type: *time.Time
如何忽略结构中的零值字段?
type Event struct {
StartDate time.Time `datastore:"start_date,noindex" json:"startDate"`
EndDate time.Time `datastore:"end_date,noindex" json:"endDate"`
}
默认保存机制不处理可选字段。字段要么一直保存,要么永远不保存。没有"only save if it's value does not equal to something".
这样的东西
"optionally saved property"被认为是一种自定义行为,一种自定义保存机制,因此必须手动实现。 Go 的方法是在您的结构上实现 PropertyLoadSaver
接口。在这里,我提出了两种不同的方法来实现这一点:
手动保存字段
这是一个如何通过手动保存字段(如果是零值则排除 EndDate
)的示例:
type Event struct {
StartDate time.Time `datastore:"start_date,noindex" json:"startDate"`
EndDate time.Time `datastore:"end_date,noindex" json:"endDate"`
}
func (e *Event) Save(c chan<- datastore.Property) error {
defer close(c)
// Always save StartDate:
c <- datastore.Property{Name:"start_date", Value:e.StartDate, NoIndex: true}
// Only save EndDate if not zero value:
if !e.EndDate.IsZero() {
c <- datastore.Property{Name:"end_date", Value:e.EndDate, NoIndex: true}
}
return nil
}
func (e *Event) Load(c chan<- datastore.Property) error {
// No change required in loading, call default implementation:
return datastore.LoadStruct(e, c)
}
使用另一个结构
这是使用另一个结构的另一种方法。 Load()
实现始终相同,只有 Save()
不同:
func (e *Event) Save(c chan<- datastore.Property) error {
if !e.EndDate.IsZero() {
// If EndDate is not zero, save as usual:
return datastore.SaveStruct(e, c)
}
// Else we need a struct without the EndDate field:
s := struct{ StartDate time.Time `datastore:"start_date,noindex"` }{e.StartDate}
// Which now can be saved using the default saving mechanism:
return datastore.SaveStruct(&s, c)
}
在字段标签中使用 omitempty。
来自文档:https://golang.org/pkg/encoding/json/
Struct values encode as JSON objects. Each exported struct field
becomes a member of the object unless
- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty" option.
Field int json:"myName,omitempty"
我正在尝试使用 Google Datastore 通过 Go 存储数据。由于 EndDate
是可选字段,并且不想在该字段中存储零值。如果我为时间字段创建指针,Google Datastore 将发送一条错误消息 - datastore: unsupported struct field type: *time.Time
如何忽略结构中的零值字段?
type Event struct {
StartDate time.Time `datastore:"start_date,noindex" json:"startDate"`
EndDate time.Time `datastore:"end_date,noindex" json:"endDate"`
}
默认保存机制不处理可选字段。字段要么一直保存,要么永远不保存。没有"only save if it's value does not equal to something".
这样的东西"optionally saved property"被认为是一种自定义行为,一种自定义保存机制,因此必须手动实现。 Go 的方法是在您的结构上实现 PropertyLoadSaver
接口。在这里,我提出了两种不同的方法来实现这一点:
手动保存字段
这是一个如何通过手动保存字段(如果是零值则排除 EndDate
)的示例:
type Event struct {
StartDate time.Time `datastore:"start_date,noindex" json:"startDate"`
EndDate time.Time `datastore:"end_date,noindex" json:"endDate"`
}
func (e *Event) Save(c chan<- datastore.Property) error {
defer close(c)
// Always save StartDate:
c <- datastore.Property{Name:"start_date", Value:e.StartDate, NoIndex: true}
// Only save EndDate if not zero value:
if !e.EndDate.IsZero() {
c <- datastore.Property{Name:"end_date", Value:e.EndDate, NoIndex: true}
}
return nil
}
func (e *Event) Load(c chan<- datastore.Property) error {
// No change required in loading, call default implementation:
return datastore.LoadStruct(e, c)
}
使用另一个结构
这是使用另一个结构的另一种方法。 Load()
实现始终相同,只有 Save()
不同:
func (e *Event) Save(c chan<- datastore.Property) error {
if !e.EndDate.IsZero() {
// If EndDate is not zero, save as usual:
return datastore.SaveStruct(e, c)
}
// Else we need a struct without the EndDate field:
s := struct{ StartDate time.Time `datastore:"start_date,noindex"` }{e.StartDate}
// Which now can be saved using the default saving mechanism:
return datastore.SaveStruct(&s, c)
}
在字段标签中使用 omitempty。 来自文档:https://golang.org/pkg/encoding/json/
Struct values encode as JSON objects. Each exported struct field becomes a member of the object unless
- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty" option.
Field int
json:"myName,omitempty"