Permalink
Browse files

fs: Omit zero time.Time values.

The omitempty option doesn't work well with time.Time values, since
zero time.Time values are not considered empty and get marshaled.
Use time.Time pointer instead.
  • Loading branch information...
dmitshur committed Nov 30, 2017
1 parent 486d915 commit 039126946836b51807c8a049271140b3803acc19
Showing with 22 additions and 6 deletions.
  1. +22 −6 fs/schema.go
View
@@ -465,6 +465,8 @@ func (p page) Page() event.Page {
}
// user is an on-disk representation of users.User.
// TODO: Consider storing user spec only, fetching user from users.Service,
// or better yet, using provided users.User?
type user struct {
ID uint64
Domain string `json:",omitempty"`
@@ -476,8 +478,8 @@ type user struct {
AvatarURL string `json:",omitempty"`
HTMLURL string `json:",omitempty"`
CreatedAt time.Time `json:",omitempty"`
UpdatedAt time.Time `json:",omitempty"`
CreatedAt *time.Time `json:",omitempty"`
UpdatedAt *time.Time `json:",omitempty"`
SiteAdmin bool `json:",omitempty"`
}
@@ -498,8 +500,8 @@ func fromUser(u users.User) user {
AvatarURL: u.AvatarURL,
HTMLURL: u.HTMLURL,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
CreatedAt: timePointer(u.CreatedAt),
UpdatedAt: timePointer(u.UpdatedAt),
SiteAdmin: u.SiteAdmin,
}
@@ -523,13 +525,27 @@ func (u user) User() users.User {
AvatarURL: u.AvatarURL,
HTMLURL: u.HTMLURL,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
CreatedAt: timeValue(u.CreatedAt),
UpdatedAt: timeValue(u.UpdatedAt),
SiteAdmin: u.SiteAdmin,
}
}
func timePointer(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}
func timeValue(t *time.Time) time.Time {
if t == nil {
return time.Time{}
}
return *t
}
// userSpec is an on-disk representation of users.UserSpec.
type userSpec struct {
ID uint64

0 comments on commit 0391269

Please sign in to comment.