improbable-eng / ts-protoc-gen

Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.
Apache License 2.0
1.36k stars 173 forks source link

Getting the string name of Protobuf object #307

Open chrisplusplus opened 2 years ago

chrisplusplus commented 2 years ago

I cannot seem to find a way to dynamically get the string name (used in the any.pack function) of an object. The only way I know of is to explicitly pass in a string to the .pack function.

Here is how I currently get the "any" version of my Protobuf objects

const payload = new SysTypes.PbRepeatedUint32Struct();
payload.setValuesList(items);
const any_pb = new GoogleProtobuf.Any;
any_pb.pack(payload.serializeBinary(), 'MpaInterface.PbRepeatedUint32Struct');

Is there a different way to do this without having to know the string ahead of time?

It looks like the generated .js file actually contains a reference to the string I want to use:

proto.MpaInterface.PbRepeatedUint32Struct.displayName = 'proto.MpaInterface.PbRepeatedUint32Struct';

but this property isn't accessible in the generated Typescript files.

Does anyone know how to access the "string name" of the Protobuf object?