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

ts-proto 2.x ideas #647

Open
stephenh opened this issue Aug 6, 2022 · 9 comments
Open

ts-proto 2.x ideas #647

stephenh opened this issue Aug 6, 2022 · 9 comments

Comments

@stephenh
Copy link
Owner

stephenh commented Aug 6, 2022

  • ESM output by default
  • Migrate off long.js
  • Migrate off protobufjs
  • Fix env to be node_browser instead of both
  • Fix useOptionals all to be scalar_messages
@wenerme
Copy link

wenerme commented Aug 16, 2022

allowed import common from a common ?

generate a common for

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
  ? T
  : T extends Long
  ? string | number | Long
  : T extends Array<infer U>
  ? Array<DeepPartial<U>>
  : T extends ReadonlyArray<infer U>
  ? ReadonlyArray<DeepPartial<U>>
  : T extends {}
  ? { [K in Exclude<keyof T, '$type'>]?: DeepPartial<T[K]> }
  : Partial<T>;

function toTimestamp(date: Date): Timestamp {
  const seconds = numberToLong(date.getTime() / 1_000);
  const nanos = (date.getTime() % 1_000) * 1_000_000;
  return { $type: 'google.protobuf.Timestamp', seconds, nanos };
}

function fromTimestamp(t: Timestamp): Date {
  let millis = t.seconds.toNumber() * 1_000;
  millis += t.nanos / 1_000_000;
  return new Date(millis);
}

function fromJsonTimestamp(o: any): Date {
  if (o instanceof Date) {
    return o;
  } else if (typeof o === 'string') {
    return new Date(o);
  } else {
    return fromTimestamp(Timestamp.fromJSON(o));
  }
}

function numberToLong(number: number) {
  return Long.fromNumber(number);
}

if (_m0.util.Long !== Long) {
  _m0.util.Long = Long as any;
  _m0.configure();
}

function isSet(value: any): boolean {
  return value !== null && value !== undefined;
}

@paralin
Copy link
Collaborator

paralin commented Aug 16, 2022

@wenerme
Copy link

wenerme commented Aug 16, 2022

Allowed toJSON to omit empty ?

// sequelize
const model = await ResourceModel.create(User.toJSON(item), {
        returning: true,
});

without toJSON to omit default value, the create may fail, e.g. int64 profile_id, profile_id=0 is not exists.

@wenerme
Copy link

wenerme commented Aug 16, 2022

Generate with a generated header comment

// Code generated by protoc-gen-ts-proto v1.2.3. DO NOT EDIT.

this can hint some other program, e.g. ignore count code lines

@wenerme
Copy link

wenerme commented Aug 16, 2022

I'm using the common import here: https://github.com/aperturerobotics/protobuf-project/blob/main/example/other/other.pb.ts#L2

But still has the common functions, only a wkt used an external import.

@Sermelyan
Copy link

Sermelyan commented Oct 17, 2022

Migrate off long.js

Are you planing to migrate to BigInt`s?

Migrate off protobufjs

Do you want your own encoder/decoder?

Great lib, thanks for your work!

@stephenh
Copy link
Owner Author

Hey @Sermelyan ; yeah, I think BigInts would make sense.

For the encoder/decoder, no I think that's probably out-of-scope for ts-proto and I'd poke around at using like the grpc encoders or something like that. Buf ended up writing their own ESM-based encoders, so that would likely be a good candidate.

Full disclaimer I'm not actively planning on working on these personally, as I don't need them for anything I'm working on at the moment, but if others wanted to pick them up, that'd be great.

@lgrahl
Copy link

lgrahl commented Dec 9, 2022

Moving from the custom UTF8 encoding/decoding done in protobufjs to the standardised TextEncoder/TextDecoder APIs would be great as well. But this is likely covered by migrating away from protobufjs.

@azizghuloum
Copy link
Contributor

Hopefully 2023 would be the year that bigints make it to protobufjs.

protobufjs/protobuf.js#1557

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants