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: added nestJsTimestampTypeWrapper #567

Merged
merged 13 commits into from
Oct 13, 2022
Merged

Conversation

PhilipMantrov
Copy link
Contributor

related issue #69

@stephenh
Copy link
Owner

@PhilipMantrov nice! Would be amazing to get this fixed.

I think as-is, the added code isn't going to actually get output? Or will it? Usually code within that makeUtils function is used for things we want conditionally generated, so I just assumed that would be the case here.

Can we nudge one of the existing integration-test examples around to show this code in it's output, and maybe even have a unit test that shows it works? Fwiw I'm fine w/o the unit test if it means just getting this landed, given a lot of users have been asking for it.

Thanks!

@PhilipMantrov
Copy link
Contributor Author

PhilipMantrov commented May 19, 2022

Added integration test and change
const wrappers = imp('wrappers@protobufjs/minimal');
to
const wrappers = imp('wrapper@protobufjs');
because protobuf.js has custom wrapper only for Any (wrappers.js, and issue), and if we dont have Any in our proto, wrappers will be undefined

@PhilipMantrov
Copy link
Contributor Author

PhilipMantrov commented May 19, 2022

And I commit changes in genfiles, if it isn't needed just revert this commit.

merge original master into fork
@PhilipMantrov
Copy link
Contributor Author

@stephenh can we merge this pr asap? Or just tell me what we need this for accepting this pr, thx. :)

@@ -1,6 +1,6 @@
/* eslint-disable */
import { util, configure, Writer, Reader } from 'protobufjs/minimal';
Copy link
Owner

@stephenh stephenh May 26, 2022

Choose a reason for hiding this comment

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

@PhilipMantrov do you know why all of these other files changed?

Copy link
Contributor Author

@PhilipMantrov PhilipMantrov May 26, 2022

Choose a reason for hiding this comment

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

@stephenh newest version of protobufjs has deep types, so compiler dont need abstraction as _m0 anymore.
In package.json we have '^', so we install newest version 6.11.3 (on 6.8.8 i see old behavior and compiler use _m0 for abstraction).

Copy link
Owner

Choose a reason for hiding this comment

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

Huh! I'd love to get rid of this, but I'm not following why this would change, b/c:

  1. Isn't yarn.lock pinned to 6.8.8?
  2. What controls the _m0 output is this line:

https://github.com/stephenh/ts-proto/blob/main/src/options.ts#L183

I.e. the * as _m0 is generated by ts-poet because we explicitly tell it to, and I'm like 99% sure nothing in node_modules/protobuf/... would affect ts-poet's behavior as to whether it does/does not use the _m0 style import...

So, dunno, I'm still not following how this changed?

Copy link
Owner

Choose a reason for hiding this comment

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

@PhilipMantrov I checked out this PR locally, and when I run yarn bin2ts, AFAICT it puts all of the _m0 back... I'm wondering if maybe when you ran codegen, you weren't on the latest main and/or had an outdated version of ts-poet installed?

Fwiw I'd be really happy to bump protobufjs and clean up these imports, but seems like something to do in a separate PR.

Can you try a new yarn bin2ts and see what happens? Thanks!

Copy link
Owner

Choose a reason for hiding this comment

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

@PhilipMantrov I got your ping about asking for a re-review, see this comment ^.

If we just can't figure this out, I can always create a new PR with your core code changes, and w/o all of the _m0 changes, and get that merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

protobufjs now fixed on 6.8.8

Copy link
Contributor Author

@PhilipMantrov PhilipMantrov Jul 19, 2022

Choose a reason for hiding this comment

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

I'd be really happy to bump protobufjs and clean up these imports, but seems like something to do in a separate PR

I create another PR for this soon.

@stephenh
Copy link
Owner

@PhilipMantrov thanks for adding the test! I'm good to merge this once the build is passing.

@PhilipMantrov
Copy link
Contributor Author

PhilipMantrov commented May 26, 2022

@PhilipMantrov thanks for adding the test! I'm good to merge this once the build is passing.

@stephenh you need to disable 'Diff check' for once because protobufjs was updated.

@n0isy
Copy link

n0isy commented Jul 14, 2022

Any updates?

@PhilipMantrov
Copy link
Contributor Author

Something strange with tests, working well in local env, but failed in CI.
@stephenh can you take a look?

@gusinacio
Copy link

I tried to run the assignment of ".google.protobuf.Timestamp" to wrappers with protobufjs 7.0.0 and it isn't working, I had to downgrade to 6.11.3.

It's worth to check if it's running in version 7 and what has changed.

@PhilipMantrov
Copy link
Contributor Author

PhilipMantrov commented Aug 30, 2022

@flametuner yea, its not working on protobufjs: 7.0.0 and above.
ts-proto was pinned protobufjs to 6.8.8 for some reason, and for protobufjs 7.0.0 need some work in ts-proto.

Just use for now

Add in main.ts:

// ToDo: Remove when https://github.com/stephenh/ts-proto/pull/567 has merged
registerWrapperTypes();

function:

import { wrappers } from 'protobufjs';

export function registerWrapperTypes() {
  wrappers['.google.protobuf.Timestamp'] = {
    fromObject(value) {
      return {
        seconds: value.getTime() / 1000,
        nanos: (value.getTime() % 1000) * 1e6,
      };
    },
    toObject(message: { seconds: number; nanos: number }) {
      return new Date(message.seconds * 1000 + message.nanos / 1e6);
    },
  } as any;
}

@TakeOver
Copy link

Whatsup guys when this will be merged? I need this feature ASAP

@TakeOver
Copy link

Is this repo alive? Seems like dead perhaps I should fork it

@stephenh stephenh merged commit 59d451e into stephenh:main Oct 13, 2022
@stephenh
Copy link
Owner

@TakeOver happy to have you fork the repo; if you want to contribute here I'm happy to merge PRs as well.

For this one, it took a few iterations and I didn't realize it'd been rewritten quite a bit and the build was green. Thanks @PhilipMantrov !

Fwiw technically it is green-ish b/c as @PhilipMantrov mentioned the tests were failing in CI so they're currently being skipped:

  xit('should findOneHero', async () => {
    const hero = await heroService.findOneHero({ id: 1 }).toPromise();
    expect(hero).toEqual({ id: 1, name: 'Stephenh', birthDate: new Date("2000/01/01") });
  });

I didn't have time to debug those, so just fyi if you try using this feature, and it doesn't work, it would be interesting see if the failures are for the same issue as the xit-d tests. Or maybe it really is CI specific.

stephenh pushed a commit that referenced this pull request Oct 13, 2022
# [1.128.0](v1.127.0...v1.128.0) (2022-10-13)

### Features

* added nestJsTimestampTypeWrapper ([#567](#567)) ([59d451e](59d451e))
@stephenh
Copy link
Owner

🎉 This PR is included in version 1.128.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@TannerPerrien
Copy link

TannerPerrien commented Dec 12, 2022

@TakeOver and @stephenh - Thanks for this contribution!

When generating output with --ts_proto_opt=useDate=date, I see the resulting protobufjs wrappers/fromObject/toObject. However, my nestjs application does not end up invoking this wrapper and my resulting Timestamp output is

{
  "seconds": "0",
  "nanos": 0
}

I have carefully reviewed the nestjs-simple-usedate integration test.

UPDATE: Leaving the above comment for reference to others.

I re-read this thread and saw the comment about this not working in protobufjs: 7.0.0. It turns out @grpc/proto-loader 0.7.1 and above uses protobufjs: 7.x.x. I downgraded @grpc/proto-loader to 0.6.13 and everything is now working.

@stephenh
Copy link
Owner

Ah nice! Thanks for both the self-diagnosis and also reporting back the "pin to @grpc/proto-loader 0.6.13" fix @TannerPerrien !

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

6 participants