-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proto3 compatibility: use uvarint encoding in time instead of fixed sized #224
Conversation
- internal seconds and nanos were encoded fixed size which is different from proto's "well known type" TimeStamp
This is actually a bit confusing: message Timestamp {
int64 seconds = 1; // this is proto3's int64
int32 nanos = 2;
} which translate into this go code: type Timestamp struct {
Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
} In protobuf I've updated amino's time encoding to match with proto's behaviour (will push shortly). |
8336134
to
ca36768
Compare
- add tests against "google/protobuf/timestamp.proto and clean up tests slightly - add validation of seconds while encoding - add error type InvalidTimeErr - remove unnecessary var - fix typo - add changelog entry
ca36768
to
dbdbbcd
Compare
b1924f9
to
9d7df11
Compare
9d7df11
to
e2d4648
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just a few minor comments!
// seconds of 01-01-0001 | ||
minSeconds = -62135596800 | ||
// seconds of 10000-01-01 | ||
maxSeconds = 253402300800 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the standard upperbound? Just seems kinda weird since its not near a power of two. Doesn't block the PR though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This constant is straight from the code that converts between time.Time
and the wellknown type Timestamp
: https://github.com/golang/protobuf/blob/ddf22928ea3c56eb4292a0adbbf5001b1e8e7d0d/ptypes/timestamp.go#L48-L50
decoder.go
Outdated
@@ -261,15 +262,20 @@ func decodeNanos(bz *[]byte, n *int) (int32, error) { | |||
if err != nil { | |||
return 0, err | |||
} | |||
if fieldNum == 2 && typ == Typ3_4Byte { | |||
if fieldNum == 2 && typ == Typ3_Varint { | |||
slide(bz, n, _n) | |||
_n = 0 | |||
// Actually read the Int32. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outdated comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, good catch! Removed.
assert.Equal(t, p3.ProtoGotTime{T: nil}, pbRes) | ||
} | ||
|
||
func TestProto3CompatTimestampNow(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be table driven?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be nice. But it's basically one test-case that documents what works how.
Merged! Thanks for the review @ValarDragon |
// the original signed value: | ||
res := int64(sec) | ||
if res < minSeconds || res >= maxSeconds { | ||
return 0, n, InvalidTimeErr(fmt.Sprintf("seconds have to be > %d and < %d, got: %d", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seconds have to be >= %d
if ns < 0 || ns > maxNanos { | ||
// we could as well panic here: | ||
// time.Time.Nanosecond() guarantees nanos to be in [0, 999,999,999] | ||
return InvalidTimeErr(fmt.Sprintf("nanosecons have to be >= 0 and <= %v, got: %d", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nanoseconds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I'll address this and above comment in #233
WIP: fuzzer found a crasher, still need to investigate if this is related!The fuzzer just timed out, no real crashers.
resolves #208
resolves #223