Skip to content
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

feat: add pbtime util package #11085

Closed
wants to merge 7 commits into from
Closed

feat: add pbtime util package #11085

wants to merge 7 commits into from

Conversation

robert-zaremba
Copy link
Collaborator

Description

Adding pbtime package bring more higher level functionality to protobuf.Timestamp (notably comparison).
With new proto directions we are going away from go stdlib time.Time.

Note, I put it into root, but maybe there is a better place?

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Copy link
Member

@aaronc aaronc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @robert-zaremba. I wonder if this maybe does fit in the scope of cosmos-proto as it is pretty generic. @fdymylja would you feel comfortable including it there?

Comment on lines +9 to +11
func IsZero(t *tspb.Timestamp) bool {
return t == nil || t.Nanos == 0 && t.Seconds == 0
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't generically true. it's sufficient if we assume that the time is in the future, but technically 1970-01-01 is a valid timestamp. generically IsZero is equivalent to t == nil

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't do a validation. The goal is to make a canonical zero value which work with a non nil version of tspb.Timestamp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's not zero. We can't use this to infer that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's zero value of tspb.Timestamp and I thought that yesterday we said to add a support for a non nil zero value.

That being said, my if we can change non nil to nil in proto and handle that in a normal way, then that would be my preference.

pbtime/cmp.go Outdated
return 1
}

func Add(t *tspb.Timestamp, d time.Duration) *tspb.Timestamp {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use durationpb here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,. we can have both: time.Duration and durationpb

pbtime/cmp_test.go Outdated Show resolved Hide resolved
@fdymylja
Copy link
Contributor

fdymylja commented Feb 1, 2022

Thanks @robert-zaremba. I wonder if this maybe does fit in the scope of cosmos-proto as it is pretty generic. @fdymylja would you feel comfortable including it there?

TBH, I think ideally you'd want all the time.Time methods on timestampb.Timestamp which accept timestamppb.Timestamp and durationpb.Duration as arguments (and maybe even variants which accept time.Time and time.Duration for easier modifications).

I don't feel the sdk is the correct place for this package, I don't even think cosmos-proto is, Ideally you'd want these upstreamed to go-protobuf (as methods on those types and not as utility function).

But if I had to choose between sdk or cosmos-proto, then better have those in a types/wellknown/time pkg in cosmos-proto.

@robert-zaremba robert-zaremba mentioned this pull request Feb 2, 2022
56 tasks
Co-authored-by: Aaron Craelius <aaron@regen.network>
@robert-zaremba
Copy link
Collaborator Author

I think ideally you'd want all the time.Time methods

We can add them later, when needed. For the moment I mostly need IsZero and Compare for #11095

@robert-zaremba
Copy link
Collaborator Author

robert-zaremba commented Feb 2, 2022

I've updated the PR to:

  • Add now accepts PB Duration
  • new AddStd - accepts time.Duration
  • Added overflow checks
  • Added case for Compare when either of t1 and t2 is nil -> it will return false. nil is treated here as undefined, although if we want to follow the IsZero then:
    • if both are nil, then either we return true or update the IsZero function. what do you think?

@robert-zaremba
Copy link
Collaborator Author

I think moving it to cosmos-proto is a good idea. Anyone against?

@aaronc
Copy link
Member

aaronc commented Feb 2, 2022

Moving to cosmos-proto makes sense

@robert-zaremba
Copy link
Collaborator Author

OK, will keep this PR open until I will move it (plan: tomorrow) to let other comment on other things.
BTW: the motivation is to have a canonical functions and representation rather than copy-pasting.

@robert-zaremba
Copy link
Collaborator Author

Moved to cosmos/cosmos-proto#60
Please review there.

@tac0turtle tac0turtle deleted the robert/pbtime branch March 25, 2022 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants