Selaa lähdekoodia

ptypes: sync internal development (#374)

Added ptypes.TimestampNow which returns Timestamp proto for the current time.
Added nil check for ptypes.AnyMessageName.
Herbie Ong 8 vuotta sitten
vanhempi
commit
4f95b0d3ea
4 muutettua tiedostoa jossa 59 lisäystä ja 32 poistoa
  1. 3 0
      ptypes/any.go
  2. 19 19
      ptypes/duration_test.go
  3. 9 0
      ptypes/timestamp.go
  4. 28 13
      ptypes/timestamp_test.go

+ 3 - 0
ptypes/any.go

@@ -51,6 +51,9 @@ const googleApis = "type.googleapis.com/"
 // function. AnyMessageName is provided for less common use cases like filtering a
 // sequence of Any messages based on a set of allowed message type names.
 func AnyMessageName(any *any.Any) (string, error) {
+	if any == nil {
+		return "", fmt.Errorf("message is nil")
+	}
 	slash := strings.LastIndex(any.TypeUrl, "/")
 	if slash < 0 {
 		return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl)

+ 19 - 19
ptypes/duration_test.go

@@ -52,37 +52,37 @@ var durationTests = []struct {
 	dur     time.Duration
 }{
 	// The zero duration.
-	{&durpb.Duration{0, 0}, true, true, 0},
+	{&durpb.Duration{Seconds: 0, Nanos: 0}, true, true, 0},
 	// Some ordinary non-zero durations.
-	{&durpb.Duration{100, 0}, true, true, 100 * time.Second},
-	{&durpb.Duration{-100, 0}, true, true, -100 * time.Second},
-	{&durpb.Duration{100, 987}, true, true, 100*time.Second + 987},
-	{&durpb.Duration{-100, -987}, true, true, -(100*time.Second + 987)},
+	{&durpb.Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second},
+	{&durpb.Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second},
+	{&durpb.Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987},
+	{&durpb.Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)},
 	// The largest duration representable in Go.
-	{&durpb.Duration{maxGoSeconds, int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64},
+	{&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64},
 	// The smallest duration representable in Go.
-	{&durpb.Duration{minGoSeconds, int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64},
+	{&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64},
 	{nil, false, false, 0},
-	{&durpb.Duration{-100, 987}, false, false, 0},
-	{&durpb.Duration{100, -987}, false, false, 0},
-	{&durpb.Duration{math.MinInt64, 0}, false, false, 0},
-	{&durpb.Duration{math.MaxInt64, 0}, false, false, 0},
+	{&durpb.Duration{Seconds: -100, Nanos: 987}, false, false, 0},
+	{&durpb.Duration{Seconds: 100, Nanos: -987}, false, false, 0},
+	{&durpb.Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0},
+	{&durpb.Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0},
 	// The largest valid duration.
-	{&durpb.Duration{maxSeconds, 1e9 - 1}, true, false, 0},
+	{&durpb.Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0},
 	// The smallest valid duration.
-	{&durpb.Duration{minSeconds, -(1e9 - 1)}, true, false, 0},
+	{&durpb.Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0},
 	// The smallest invalid duration above the valid range.
-	{&durpb.Duration{maxSeconds + 1, 0}, false, false, 0},
+	{&durpb.Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0},
 	// The largest invalid duration below the valid range.
-	{&durpb.Duration{minSeconds - 1, -(1e9 - 1)}, false, false, 0},
+	{&durpb.Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0},
 	// One nanosecond past the largest duration representable in Go.
-	{&durpb.Duration{maxGoSeconds, int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0},
+	{&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0},
 	// One nanosecond past the smallest duration representable in Go.
-	{&durpb.Duration{minGoSeconds, int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0},
+	{&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0},
 	// One second past the largest duration representable in Go.
-	{&durpb.Duration{maxGoSeconds + 1, int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0},
+	{&durpb.Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0},
 	// One second past the smallest duration representable in Go.
-	{&durpb.Duration{minGoSeconds - 1, int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0},
+	{&durpb.Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0},
 }
 
 func TestValidateDuration(t *testing.T) {

+ 9 - 0
ptypes/timestamp.go

@@ -99,6 +99,15 @@ func Timestamp(ts *tspb.Timestamp) (time.Time, error) {
 	return t, validateTimestamp(ts)
 }
 
+// TimestampNow returns a google.protobuf.Timestamp for the current time.
+func TimestampNow() *tspb.Timestamp {
+	ts, err := TimestampProto(time.Now())
+	if err != nil {
+		panic("ptypes: time.Now() out of Timestamp range")
+	}
+	return ts
+}
+
 // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
 // It returns an error if the resulting Timestamp is invalid.
 func TimestampProto(t time.Time) (*tspb.Timestamp, error) {

+ 28 - 13
ptypes/timestamp_test.go

@@ -46,32 +46,32 @@ var tests = []struct {
 	t     time.Time
 }{
 	// The timestamp representing the Unix epoch date.
-	{&tspb.Timestamp{0, 0}, true, utcDate(1970, 1, 1)},
+	{&tspb.Timestamp{Seconds: 0, Nanos: 0}, true, utcDate(1970, 1, 1)},
 	// The smallest representable timestamp.
-	{&tspb.Timestamp{math.MinInt64, math.MinInt32}, false,
+	{&tspb.Timestamp{Seconds: math.MinInt64, Nanos: math.MinInt32}, false,
 		time.Unix(math.MinInt64, math.MinInt32).UTC()},
 	// The smallest representable timestamp with non-negative nanos.
-	{&tspb.Timestamp{math.MinInt64, 0}, false, time.Unix(math.MinInt64, 0).UTC()},
+	{&tspb.Timestamp{Seconds: math.MinInt64, Nanos: 0}, false, time.Unix(math.MinInt64, 0).UTC()},
 	// The earliest valid timestamp.
-	{&tspb.Timestamp{minValidSeconds, 0}, true, utcDate(1, 1, 1)},
+	{&tspb.Timestamp{Seconds: minValidSeconds, Nanos: 0}, true, utcDate(1, 1, 1)},
 	//"0001-01-01T00:00:00Z"},
 	// The largest representable timestamp.
-	{&tspb.Timestamp{math.MaxInt64, math.MaxInt32}, false,
+	{&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: math.MaxInt32}, false,
 		time.Unix(math.MaxInt64, math.MaxInt32).UTC()},
 	// The largest representable timestamp with nanos in range.
-	{&tspb.Timestamp{math.MaxInt64, 1e9 - 1}, false,
+	{&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: 1e9 - 1}, false,
 		time.Unix(math.MaxInt64, 1e9-1).UTC()},
 	// The largest valid timestamp.
-	{&tspb.Timestamp{maxValidSeconds - 1, 1e9 - 1}, true,
+	{&tspb.Timestamp{Seconds: maxValidSeconds - 1, Nanos: 1e9 - 1}, true,
 		time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)},
 	// The smallest invalid timestamp that is larger than the valid range.
-	{&tspb.Timestamp{maxValidSeconds, 0}, false, time.Unix(maxValidSeconds, 0).UTC()},
+	{&tspb.Timestamp{Seconds: maxValidSeconds, Nanos: 0}, false, time.Unix(maxValidSeconds, 0).UTC()},
 	// A date before the epoch.
-	{&tspb.Timestamp{-281836800, 0}, true, utcDate(1961, 1, 26)},
+	{&tspb.Timestamp{Seconds: -281836800, Nanos: 0}, true, utcDate(1961, 1, 26)},
 	// A date after the epoch.
-	{&tspb.Timestamp{1296000000, 0}, true, utcDate(2011, 1, 26)},
+	{&tspb.Timestamp{Seconds: 1296000000, Nanos: 0}, true, utcDate(2011, 1, 26)},
 	// A date after the epoch, in the middle of the day.
-	{&tspb.Timestamp{1296012345, 940483}, true,
+	{&tspb.Timestamp{Seconds: 1296012345, Nanos: 940483}, true,
 		time.Date(2011, 1, 26, 3, 25, 45, 940483, time.UTC)},
 }
 
@@ -123,8 +123,8 @@ func TestTimestampString(t *testing.T) {
 	}{
 		// Not much testing needed because presumably time.Format is
 		// well-tested.
-		{&tspb.Timestamp{0, 0}, "1970-01-01T00:00:00Z"},
-		{&tspb.Timestamp{minValidSeconds - 1, 0}, "(timestamp: seconds:-62135596801  before 0001-01-01)"},
+		{&tspb.Timestamp{Seconds: 0, Nanos: 0}, "1970-01-01T00:00:00Z"},
+		{&tspb.Timestamp{Seconds: minValidSeconds - 1, Nanos: 0}, "(timestamp: seconds:-62135596801  before 0001-01-01)"},
 	} {
 		got := TimestampString(test.ts)
 		if got != test.want {
@@ -136,3 +136,18 @@ func TestTimestampString(t *testing.T) {
 func utcDate(year, month, day int) time.Time {
 	return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
 }
+
+func TestTimestampNow(t *testing.T) {
+	// Bracket the expected time.
+	before := time.Now()
+	ts := TimestampNow()
+	after := time.Now()
+
+	tm, err := Timestamp(ts)
+	if err != nil {
+		t.Errorf("between %v and %v\nTimestampNow() = %v\nwhich is invalid (%v)", before, after, ts, err)
+	}
+	if tm.Before(before) || tm.After(after) {
+		t.Errorf("between %v and %v\nTimestamp(TimestampNow()) = %v", before, after, tm)
+	}
+}