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

Support for google.protobuf.Timestamp as string #1015

Closed
netanel-utila opened this issue Nov 7, 2024 · 3 comments
Closed

Support for google.protobuf.Timestamp as string #1015

netanel-utila opened this issue Nov 7, 2024 · 3 comments

Comments

@netanel-utila
Copy link

Currently it returns:

{
  $typeName: "google.protobuf.Timestamp",
  seconds: 1727445165n,
  nanos: 865585000,
}

Migrating from ts-proto that supports useDate string:

version: v1
plugins:
  - name: ts_proto
    out: ../genproto/ts_web
    opt:
      - useDate=string

How can we do this using this library?

@timostamm
Copy link
Member

Fields that use a Timestamp message always use a Timestamp message in generated code. There are a few functions for conversion, see /~https://github.com/bufbuild/protobuf-es/blob/v2.2.2/MANUAL.md#googleprotobuftimestamp

You can also use toJson and fromJson to convert to and from the JSON representation, which is a string - likely the same format that ts-proto uses:

import { fromJson, toJson } from "@bufbuild/protobuf";
import { timestampNow, TimestampSchema } from "@bufbuild/protobuf/wkt";

const str = toJson(TimestampSchema, timestampNow());
const ts = fromJson(TimestampSchema, str);
console.log(str, ts);

@netanel-utila
Copy link
Author

Yes, I know, but it will not be comfortable for our clients that use our SDK.

@timostamm
Copy link
Member

Yes, I know, but it will not be comfortable for our clients that use our SDK.

Is that because of backwards compatibility? I'm not sure that a string is a great representation for end users. Won't they have to parse the strings to make use of them?

We'd love to use Temporal.Instant to represent google.protobuf.Timestamp fields. It's part of the proposed Temporal API that aims to solve many problems with the ECMAScript Date object. I hope that it reaches the final stage soon.

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

2 participants