timostamm / protobuf-ts

Protobuf and RPC for TypeScript
Apache License 2.0
1.1k stars 129 forks source link

How can I get the lower snake case when using protoc to generate TypeScript from .proto definition files? #652

Closed ddddwx closed 5 months ago

ddddwx commented 5 months ago

This is my .proto:

message Group {
  uint32 id = 1;  
  uint32 project_id = 2;  
  string name = 3;  
  string descr = 4;  
  string state = 5;  
  int32 interval = 6;  
  google.protobuf.Struct attributes = 7;  
  repeated Page pages = 8; 
}

I run the command: npx protoc --ts_out src/grpc/gen --ts_opt client_generic --proto_path src/grpc/proto src/grpc/proto/configuration_platform.proto

But I get the interface definition:

interface Group {
     ...
    projectId: number; 
    ...
}

It should be"project_id"as same as definition in .proto How could I fix my protoc command? Many thanks.

timostamm commented 5 months ago

It should be"project_id"as same as definition in .proto

Can you elaborate why it should be? It isn't with Protobuf for Go, Java, C#, PHP, or Dart.

ddddwx commented 5 months ago

It should be"project_id"as same as definition in .proto

Can you elaborate why it should be? It isn't with Protobuf for Go, Java, C#, PHP, or Dart.

This is how proto is defined. I need to unify to have proper front-end and back-end communication. Is "lower snake case" configurable? I didn't see the relevant configuration in the instruction manual.

If I use projectId, there is a server-error, projectId cannot be identified by server.

timostamm commented 5 months ago

This is not how Protobuf is typically implemented - see the list of implementations above. Instead, the goal is to provide an API that is idiomatic to the target language. Peers can properly communicate regardless of property casing.

There is no plugin option to change the behavior.

jcready commented 5 months ago

I think @timostamm is getting his repos mixed up :) There is an option for this, see use_proto_field_name:

By default interface fields use lowerCamelCase names by transforming proto field names to follow common style convention for TypeScript. Set this option to preserve original proto field names in generated interfaces.

timostamm commented 5 months ago

Oof, I completely forgot about it 😅 Thanks for the shout, @jcready.

ddddwx commented 5 months ago

I think @timostamm is getting his repos mixed up :) There is an option for this, see use_proto_field_name:

By default interface fields use lowerCamelCase names by transforming proto field names to follow common style convention for TypeScript. Set this option to preserve original proto field names in generated interfaces.

wow!!! It works!! Thank you very much for your help and detailed explanation. @jcready

ddddwx commented 5 months ago

Oof, I completely forgot about it 😅 Thanks for the shout, @jcready.

Thank you for providing this protoc tool. It's incredibly concise and user-friendly. I'm extremely grateful for it! Thank you once again! @timostamm