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 support for useDate=string #221

Merged
merged 6 commits into from
Apr 2, 2021

Conversation

eqyiel
Copy link
Contributor

@eqyiel eqyiel commented Feb 18, 2021

This PR builds on the best parts of #211 and #152 and makes the useDate flag have three states, like suggested here.

My use case is a bit unusual because I use this library to generate types only for use with a gRPC ↔ ️JSON gateway (no client implementation!) that follows the canonical JSON mapping, and what I really want is the option to have timestamps be strings in interfaces.

Because of that, I don't know if these encode and decode methods for useDate=string make sense. I'd love to get some feedback from @actuosus and @jessebutterfield about this (because of their involvement in #152)!

Why absorb the changes in #211?

There are a few reasons:

  1. I couldn't touch this "tri-state" thing without worrying about breaking other use cases.
  2. I really want this library to support useDate=string 😁

If #211 gets merged first, I'll update this. 🤞

Otherwise, I put appropriate @co-authored-by attributions.

* 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
* 9999-12-31T23:59:59Z inclusive.
*/
seconds: number;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this value wrong? According to this, seconds can be int64 which can be string or Long (depending on other options). I think it's not related to this change though 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

forceLong defaults to number so I think this is correct according to https://github.com/stephenh/ts-proto#number-types

@eqyiel eqyiel marked this pull request as ready for review February 18, 2021 02:14
@stephenh
Copy link
Owner

Ah shoot, sorry, I forgot about this PR and already merged #211; can you rebase on top of master?

no client implementation!

Fwiw just checking that for this to work, you can't use primitives w/o default values (like 0 / empty string) being "lost" and showing up as undefined, per my comment here:

#152 (comment)

Are you able to handle/work around that?

Copy link
Contributor

@jessebutterfield jessebutterfield left a comment

Choose a reason for hiding this comment

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

@eqyiel Thanks so much for working on this. Looks good. I had a couple of suggestions for fixing the encode/decode methods and the fromJson method. Let me know if you have any questions about my suggestions.

integration/use-date-string/use-date-string.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
integration/use-date-string/use-date-string.ts Outdated Show resolved Hide resolved
src/main.ts Outdated Show resolved Hide resolved
src/main.ts Outdated Show resolved Hide resolved
@eqyiel eqyiel force-pushed the feat/support-date-as-string branch from 9163a68 to 293e7c1 Compare March 4, 2021 09:30
@eqyiel eqyiel marked this pull request as draft March 4, 2021 09:31
Co-authored-by: @actuosus <actuosus@users.noreply.github.com>
Co-authored-by: @jessebutterfield <jessebutterfield@users.noreply.github.com>
@eqyiel eqyiel force-pushed the feat/support-date-as-string branch from 293e7c1 to c4977a2 Compare March 4, 2021 23:25
Copy link
Contributor Author

@eqyiel eqyiel left a comment

Choose a reason for hiding this comment

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

@stephenh @jessebutterfield thanks for reviewing, please take another look when you get time 😁

*
*
* Example 6: Compute Timestamp from current time in Python.
* Example 5: Compute Timestamp from current time in Python.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess this file (and observable.bin and metadata.bin) changed because I used a different version of protoc than when they were compiled. I checked in these changes because after ./update-bins.sh && ./codegen.sh && npm run test -- -u there are no other unstaged files. It seems that this makes it consistent 😇

~/git/ts-proto/integration feat/support-date-as-string*protoc --version
libprotoc 3.13.0

Comment on lines +385 to +386
function toTimestamp(dateStr: string): ${Timestamp} {
const date = new Date(dateStr);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I felt this was not smart because the variable named date can't be changed due to stuff outside of this function: https://github.com/eqyiel/ts-proto/blob/c4977a2a19ad565766d581c4e3d733d1667de150/src/main.ts#L371-L379

I don't think it's that much worse though, because that was already the case 😇

@eqyiel eqyiel marked this pull request as ready for review March 4, 2021 23:34
Base automatically changed from master to main March 14, 2021 15:29
Copy link
Contributor

@jessebutterfield jessebutterfield left a 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. Suggested one possible simplification but overall looks great.

const obj: any = {};
message.id !== undefined && (obj.id = message.id);
message.timestamp !== undefined &&
(obj.timestamp = message.timestamp !== undefined ? message.timestamp.toISOString() : null);
Copy link
Contributor

Choose a reason for hiding this comment

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

See my comment on the generateToJson code. I think this is unnecessarily checking for undefined twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I updated it to avoid this 997eba0 😊

@stephenh is this OK? I avoided changing this at first because I thought it might be a change in behaviour, but now I think it's not possible for lines like this to evaluate to null (since there is already a check for undefined).

src/main.ts Outdated Show resolved Hide resolved
src/main.ts Show resolved Hide resolved
@stephenh stephenh changed the title Add support for useDate=string feat: Add support for useDate=string Apr 2, 2021
@stephenh stephenh merged commit d967a9a into stephenh:main Apr 2, 2021
stephenh pushed a commit that referenced this pull request Apr 2, 2021
# [1.79.0](v1.78.1...v1.79.0) (2021-04-02)

### Features

* Add support for useDate=string ([#221](#221)) ([d967a9a](d967a9a))
@stephenh
Copy link
Owner

stephenh commented Apr 2, 2021

🎉 This PR is included in version 1.79.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@eqyiel eqyiel deleted the feat/support-date-as-string branch April 3, 2021 04:32
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.

None yet

3 participants