Skip to content

Commit 8d0626c

Browse files
committed
Fixes
1 parent 6e70b3f commit 8d0626c

4 files changed

Lines changed: 43 additions & 86 deletions

File tree

common/types/duration/duration.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
//
1313
// It embeds time.Duration, so all standard methods (Seconds, String, etc.)
1414
// are directly accessible. The underlying time.Duration value can be
15-
// accessed via the .Duration field.
15+
// accessed via the .AsDuration() method.
1616
//
1717
// Example:
1818
//
@@ -35,12 +35,6 @@ func (x *Duration) AsDuration() time.Duration {
3535
return x.Duration
3636
}
3737

38-
// NewDuration creates a custom Duration from a standard time.Duration.
39-
// Deprecated: Use New instead.
40-
func NewDuration(d time.Duration) Duration {
41-
return Duration{Duration: d}
42-
}
43-
4438
// MarshalJSON implements the json.Marshaler interface by formatting the
4539
// duration as a string according to Google Well Known Type
4640
func (d Duration) MarshalJSON() ([]byte, error) {
@@ -53,8 +47,8 @@ func (d Duration) MarshalJSON() ([]byte, error) {
5347
// the number of seconds, with nanoseconds expressed as fractional seconds.
5448
// https://protobuf.dev/reference/protobuf/google.protobuf/#duration
5549
func (d Duration) ToWireFormat() string {
56-
// We do not use the standard time.Duration.String() method because it
57-
// loses precision when converting to a string.
50+
// We do not use the standard time.Duration.String() and d.Duration.Seconds()
51+
// method because they use float64 which loses precision.
5852

5953
// Get the total nanoseconds as a precise integer.
6054
nanos := d.Duration.Nanoseconds()

common/types/fieldmask/fieldmask_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,6 @@ func TestFieldMask_UnmarshalJSON(t *testing.T) {
107107
want: FieldMask{Paths: []string{"name", "age", ""}},
108108
wantErr: false,
109109
},
110-
{
111-
name: "leading comma",
112-
input: `",name,age"`,
113-
want: FieldMask{Paths: []string{"", "name", "age"}},
114-
wantErr: false,
115-
},
116-
{
117-
name: "consecutive commas",
118-
input: `"name,,age"`,
119-
want: FieldMask{Paths: []string{"name", "", "age"}},
120-
wantErr: false,
121-
},
122110
{
123111
name: "invalid JSON",
124112
input: `invalid`,

common/types/time/time.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
stdtime "time"
88
)
99

10-
// Time is a wrapper for stdtime.Time to provide custom marshaling
10+
// Time is a wrapper for time.Time to provide custom marshaling
1111
// for JSON and URL query strings.
1212
//
13-
// It embeds stdtime.Time, so all standard methods (Format, Parse, etc.)
14-
// are directly accessible. The underlying stdtime.Time value can be
15-
// accessed via the .Time field.
13+
// It embeds time.Time, so all standard methods (Format, Parse, etc.)
14+
// are directly accessible. The underlying time.Time value can be
15+
// accessed via the .AsTime() method.
1616
//
1717
// Example:
1818
//

common/types/time/time_test.go

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -256,69 +256,44 @@ func TestTime_JSONRoundTrip(t *testing.T) {
256256
}
257257

258258
func TestTime_EdgeCases(t *testing.T) {
259-
t.Run("very old time", func(t *testing.T) {
260-
oldTime := stdtime.Date(1000, 1, 1, 0, 0, 0, 0, stdtime.UTC)
261-
timeVal := *New(oldTime)
262-
263-
jsonData, err := json.Marshal(timeVal)
264-
if err != nil {
265-
t.Errorf("json.Marshal() error = %v", err)
266-
return
267-
}
268-
269-
var result Time
270-
err = json.Unmarshal(jsonData, &result)
271-
if err != nil {
272-
t.Errorf("json.Unmarshal() error = %v", err)
273-
return
274-
}
275-
276-
if !result.Time.Equal(oldTime) {
277-
t.Errorf("Time round trip failed: original = %v, result = %v", oldTime, result)
278-
}
279-
})
280-
281-
t.Run("very future time", func(t *testing.T) {
282-
futureTime := stdtime.Date(9999, 12, 31, 23, 59, 59, 999999999, stdtime.UTC)
283-
timeVal := *New(futureTime)
284-
285-
jsonData, err := json.Marshal(timeVal)
286-
if err != nil {
287-
t.Errorf("json.Marshal() error = %v", err)
288-
return
289-
}
290-
291-
var result Time
292-
err = json.Unmarshal(jsonData, &result)
293-
if err != nil {
294-
t.Errorf("json.Unmarshal() error = %v", err)
295-
return
296-
}
297-
298-
if !result.Time.Equal(futureTime) {
299-
t.Errorf("Time round trip failed: original = %v, result = %v", futureTime, result)
300-
}
301-
})
259+
tests := []struct {
260+
name string
261+
time stdtime.Time
262+
}{
263+
{
264+
name: "very old time",
265+
time: stdtime.Date(1000, 1, 1, 0, 0, 0, 0, stdtime.UTC),
266+
},
267+
{
268+
name: "very future time",
269+
time: stdtime.Date(9999, 12, 31, 23, 59, 59, 999999999, stdtime.UTC),
270+
},
271+
{
272+
name: "leap year time",
273+
time: stdtime.Date(2024, 2, 29, 12, 0, 0, 0, stdtime.UTC),
274+
},
275+
}
302276

303-
t.Run("leap year time", func(t *testing.T) {
304-
leapTime := stdtime.Date(2024, 2, 29, 12, 0, 0, 0, stdtime.UTC)
305-
timeVal := *New(leapTime)
277+
for _, tt := range tests {
278+
t.Run(tt.name, func(t *testing.T) {
279+
timeVal := *New(tt.time)
306280

307-
jsonData, err := json.Marshal(timeVal)
308-
if err != nil {
309-
t.Errorf("json.Marshal() error = %v", err)
310-
return
311-
}
281+
jsonData, err := json.Marshal(timeVal)
282+
if err != nil {
283+
t.Errorf("json.Marshal() error = %v", err)
284+
return
285+
}
312286

313-
var result Time
314-
err = json.Unmarshal(jsonData, &result)
315-
if err != nil {
316-
t.Errorf("json.Unmarshal() error = %v", err)
317-
return
318-
}
287+
var result Time
288+
err = json.Unmarshal(jsonData, &result)
289+
if err != nil {
290+
t.Errorf("json.Unmarshal() error = %v", err)
291+
return
292+
}
319293

320-
if !result.Time.Equal(leapTime) {
321-
t.Errorf("Time round trip failed: original = %v, result = %v", leapTime, result)
322-
}
323-
})
294+
if !result.Time.Equal(tt.time) {
295+
t.Errorf("Time round trip failed: original = %v, result = %v", tt.time, result)
296+
}
297+
})
298+
}
324299
}

0 commit comments

Comments
 (0)