From 9f0d66f96388f96c0978c80fe0b4775ccfc5b630 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Wed, 16 Oct 2024 22:21:46 +0800 Subject: [PATCH] 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 --- src/cmd/go/internal/cache/prog.go | 12 ++++++------ src/cmd/go/internal/modinfo/info.go | 2 +- src/cmd/go/internal/modload/build.go | 4 ++-- src/cmd/go/internal/modload/query.go | 4 ++-- src/cmd/go/internal/work/action.go | 6 +++--- src/cmd/internal/test2json/test2json.go | 5 ++--- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/cmd/go/internal/cache/prog.go b/src/cmd/go/internal/cache/prog.go index 8d826f0b99b6f..f13a8669b1e62 100644 --- a/src/cmd/go/internal/cache/prog.go +++ b/src/cmd/go/internal/cache/prog.go @@ -129,10 +129,10 @@ type ProgResponse struct { // For Get requests. - Miss bool `json:",omitempty"` // cache miss - OutputID []byte `json:",omitempty"` - Size int64 `json:",omitempty"` // in bytes - Time *time.Time `json:",omitempty"` // an Entry.Time; when the object was added to the docs + Miss bool `json:",omitempty"` // cache miss + OutputID []byte `json:",omitempty"` + Size int64 `json:",omitempty"` // in bytes + Time time.Time `json:",omitzero"` // an Entry.Time; when the object was added to the docs // DiskPath is the absolute path on disk of the ObjectID corresponding // 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) { e := Entry{ Size: res.Size, } - if res.Time != nil { - e.Time = *res.Time + if !res.Time.IsZero() { + e.Time = res.Time } else { e.Time = time.Now() } diff --git a/src/cmd/go/internal/modinfo/info.go b/src/cmd/go/internal/modinfo/info.go index ee73c5e07b7fa..4582e7bbda3c0 100644 --- a/src/cmd/go/internal/modinfo/info.go +++ b/src/cmd/go/internal/modinfo/info.go @@ -19,7 +19,7 @@ type ModulePublic struct { Query string `json:",omitempty"` // version query corresponding to this version Versions []string `json:",omitempty"` // available module versions Replace *ModulePublic `json:",omitempty"` // replaced by this module - Time *time.Time `json:",omitempty"` // time version was created + Time time.Time `json:",omitzero"` // time version was created Update *ModulePublic `json:",omitempty"` // available update (with -u) Main bool `json:",omitempty"` // is this the main module? Indirect bool `json:",omitempty"` // module is only indirectly needed by main module diff --git a/src/cmd/go/internal/modload/build.go b/src/cmd/go/internal/modload/build.go index 6e30afd5247b3..628dcf176ac4d 100644 --- a/src/cmd/go/internal/modload/build.go +++ b/src/cmd/go/internal/modload/build.go @@ -156,7 +156,7 @@ func addUpdate(ctx context.Context, m *modinfo.ModulePublic) { m.Update = &modinfo.ModulePublic{ Path: m.Path, Version: info.Version, - Time: &info.Time, + Time: info.Time, } } } @@ -344,7 +344,7 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode Li m.Error = &modinfo.ModuleError{Err: err.Error()} } else { m.Version = q.Version - m.Time = &q.Time + m.Time = q.Time } } diff --git a/src/cmd/go/internal/modload/query.go b/src/cmd/go/internal/modload/query.go index c4cf55442ba69..663ff3f8dc17a 100644 --- a/src/cmd/go/internal/modload/query.go +++ b/src/cmd/go/internal/modload/query.go @@ -234,8 +234,8 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed Version: old.Version, Origin: old.Origin, } - if old.Time != nil { - info.Time = *old.Time + if !old.Time.IsZero() { + info.Time = old.Time } return info, nil } diff --git a/src/cmd/go/internal/work/action.go b/src/cmd/go/internal/work/action.go index ec384b6d9b09d..e879cf3e20650 100644 --- a/src/cmd/go/internal/work/action.go +++ b/src/cmd/go/internal/work/action.go @@ -173,9 +173,9 @@ type actionJSON struct { NeedBuild bool `json:",omitempty"` ActionID string `json:",omitempty"` BuildID string `json:",omitempty"` - TimeReady time.Time `json:",omitempty"` - TimeStart time.Time `json:",omitempty"` - TimeDone time.Time `json:",omitempty"` + TimeReady time.Time `json:",omitzero"` + TimeStart time.Time `json:",omitzero"` + TimeDone time.Time `json:",omitzero"` Cmd []string // `json:",omitempty"` CmdReal time.Duration `json:",omitempty"` diff --git a/src/cmd/internal/test2json/test2json.go b/src/cmd/internal/test2json/test2json.go index f7dfbe69d7725..cdbb38bd53f4a 100644 --- a/src/cmd/internal/test2json/test2json.go +++ b/src/cmd/internal/test2json/test2json.go @@ -29,7 +29,7 @@ const ( // event is the JSON struct we emit. type event struct { - Time *time.Time `json:",omitempty"` + Time time.Time `json:",omitzero"` Action string Package string `json:",omitempty"` Test string `json:",omitempty"` @@ -387,8 +387,7 @@ func (c *Converter) writeOutputEvent(out []byte) { func (c *Converter) writeEvent(e *event) { e.Package = c.pkg if c.mode&Timestamp != 0 { - t := time.Now() - e.Time = &t + e.Time = time.Now() } if e.Test == "" { e.Test = c.testName