Merge pull request #47975 from deads2k/api-14-proto

Automatic merge from submit-queue (batch tested with PRs 46425, 47975)

make proto time precision match json

json readers/writers see second precision, but protobuf readers/writers seen nanosecond precision.  This means that a json client can read and write and accidentally mutate fields as seen by protobuf clients.

This makes the precision consistent.

@kubernetes/sig-api-machinery-misc @smarterclayton 

```release-note
Update protobuf time serialization for a one second granularity
```
This commit is contained in:
Kubernetes Submit Queue 2017-06-26 05:49:40 -07:00 committed by GitHub
commit df7f4b3526
2 changed files with 10 additions and 3 deletions

View File

@ -42,7 +42,10 @@ func (m *Time) ProtoTime() *Timestamp {
}
return &Timestamp{
Seconds: m.Time.Unix(),
Nanos: int32(m.Time.Nanosecond()),
// leaving this here for the record. our JSON only handled seconds, so this results in writes by
// protobuf clients storing values that aren't read by json clients, which results in unexpected
// field mutation, which fails various validation and equality code.
// Nanos: int32(m.Time.Nanosecond()),
}
}
@ -64,7 +67,11 @@ func (m *Time) Unmarshal(data []byte) error {
if err := p.Unmarshal(data); err != nil {
return err
}
m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local()
// leaving this here for the record. our JSON only handled seconds, so this results in writes by
// protobuf clients storing values that aren't read by json clients, which results in unexpected
// field mutation, which fails various validation and equality code.
// m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local()
m.Time = time.Unix(p.Seconds, int64(0)).Local()
return nil
}

View File

@ -152,7 +152,7 @@ func TestTimeProto(t *testing.T) {
input Time
}{
{Time{}},
{Date(1998, time.May, 5, 1, 5, 5, 50, time.Local)},
{Date(1998, time.May, 5, 1, 5, 5, 0, time.Local)},
{Date(1998, time.May, 5, 5, 5, 5, 0, time.Local)},
}