grpc / grpc/grpc-node

Go <-> NodeJS: Serialization question for non primitive types

Open
#2,533 2 comments 0 reactions 0 assignees View on GitHub
package: @grpc/proto-loader
Dominant language
TypeScript
Stars
4.8k
Forks
716
Avg merge
2d 3h
Merged PRs (30d)
10

Description

### Problem description

This is more a question than a problem

We have a GRPC service written in Go, and a client written in Javascript (using Nodejs runtime).

Besides some primitives, we are using some google protobuf structures as follows:
```
message PredicateMatch {
message Entry {
google.protobuf.Timestamp timestamp = 1;
oneof value {
float scalar = 2;
string compound = 3;
}
}
...
```
When generating the types and using the proto loader in nodejs to get the responses
```ts
export interface _api_PredicateMatch_Entry {
'timestamp'?: (_google_protobuf_Timestamp | null); // this is an object with { seconds: string, nanos: number }
'scalar'?: (number | string);
'compound'?: (string);
'value'?: "scalar" | "compound";
}
```

The challenge is that we need to manually convert the Timestamp to an "almost equivalent" with a function like this:
```ts
export function convertProtoTimestampToUnixMs({ seconds, nanos }: { seconds: string; nanos: number }) {
return (parseInt(seconds) + nanos / 1e9) * 1000;
}
```

Is there a better way to do this? Is there a way to have some "hooks" or interceptors at the deserialization layer to make this abstraction execute in a better layer?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.