Skip to content

Commit 9f0d66f

Browse files
committed
cmd: use omitzero option for time.Time
The doc of time.Time says: Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time. Since CL 615676 added omitzero option to encoding/json package, we can just replace (*time.Time + omitempty) with (time.Time + omitzero). Change-Id: I5593c4b45af291d015433bd1d164f1c1378db642
1 parent 24cb743 commit 9f0d66f

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

src/cmd/go/internal/cache/prog.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ type ProgResponse struct {
129129

130130
// For Get requests.
131131

132-
Miss bool `json:",omitempty"` // cache miss
133-
OutputID []byte `json:",omitempty"`
134-
Size int64 `json:",omitempty"` // in bytes
135-
Time *time.Time `json:",omitempty"` // an Entry.Time; when the object was added to the docs
132+
Miss bool `json:",omitempty"` // cache miss
133+
OutputID []byte `json:",omitempty"`
134+
Size int64 `json:",omitempty"` // in bytes
135+
Time time.Time `json:",omitzero"` // an Entry.Time; when the object was added to the docs
136136

137137
// DiskPath is the absolute path on disk of the ObjectID corresponding
138138
// a "get" request's ActionID (on cache hit) or a "put" request's
@@ -339,8 +339,8 @@ func (c *ProgCache) Get(a ActionID) (Entry, error) {
339339
e := Entry{
340340
Size: res.Size,
341341
}
342-
if res.Time != nil {
343-
e.Time = *res.Time
342+
if !res.Time.IsZero() {
343+
e.Time = res.Time
344344
} else {
345345
e.Time = time.Now()
346346
}

src/cmd/go/internal/modinfo/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type ModulePublic struct {
1919
Query string `json:",omitempty"` // version query corresponding to this version
2020
Versions []string `json:",omitempty"` // available module versions
2121
Replace *ModulePublic `json:",omitempty"` // replaced by this module
22-
Time *time.Time `json:",omitempty"` // time version was created
22+
Time time.Time `json:",omitzero"` // time version was created
2323
Update *ModulePublic `json:",omitempty"` // available update (with -u)
2424
Main bool `json:",omitempty"` // is this the main module?
2525
Indirect bool `json:",omitempty"` // module is only indirectly needed by main module

src/cmd/go/internal/modload/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func addUpdate(ctx context.Context, m *modinfo.ModulePublic) {
156156
m.Update = &modinfo.ModulePublic{
157157
Path: m.Path,
158158
Version: info.Version,
159-
Time: &info.Time,
159+
Time: info.Time,
160160
}
161161
}
162162
}
@@ -344,7 +344,7 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode Li
344344
m.Error = &modinfo.ModuleError{Err: err.Error()}
345345
} else {
346346
m.Version = q.Version
347-
m.Time = &q.Time
347+
m.Time = q.Time
348348
}
349349
}
350350

src/cmd/go/internal/modload/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed
234234
Version: old.Version,
235235
Origin: old.Origin,
236236
}
237-
if old.Time != nil {
238-
info.Time = *old.Time
237+
if !old.Time.IsZero() {
238+
info.Time = old.Time
239239
}
240240
return info, nil
241241
}

src/cmd/go/internal/work/action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ type actionJSON struct {
173173
NeedBuild bool `json:",omitempty"`
174174
ActionID string `json:",omitempty"`
175175
BuildID string `json:",omitempty"`
176-
TimeReady time.Time `json:",omitempty"`
177-
TimeStart time.Time `json:",omitempty"`
178-
TimeDone time.Time `json:",omitempty"`
176+
TimeReady time.Time `json:",omitzero"`
177+
TimeStart time.Time `json:",omitzero"`
178+
TimeDone time.Time `json:",omitzero"`
179179

180180
Cmd []string // `json:",omitempty"`
181181
CmdReal time.Duration `json:",omitempty"`

src/cmd/internal/test2json/test2json.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
// event is the JSON struct we emit.
3131
type event struct {
32-
Time *time.Time `json:",omitempty"`
32+
Time time.Time `json:",omitzero"`
3333
Action string
3434
Package string `json:",omitempty"`
3535
Test string `json:",omitempty"`
@@ -387,8 +387,7 @@ func (c *Converter) writeOutputEvent(out []byte) {
387387
func (c *Converter) writeEvent(e *event) {
388388
e.Package = c.pkg
389389
if c.mode&Timestamp != 0 {
390-
t := time.Now()
391-
e.Time = &t
390+
e.Time = time.Now()
392391
}
393392
if e.Test == "" {
394393
e.Test = c.testName

0 commit comments

Comments
 (0)