Git Product home page Git Product logo

Comments (8)

thesayyn avatar thesayyn commented on May 24, 2024 1

also, using oneof fileds could be an option.

message EmploymentStatus {
  oneof name {
      bool unemployed = 0;
      bool employed = 1;
      bool student = 2;
      bool retired = 3;
  }
}


```typescript
const status = new EmploymentStatus({unemployed: true});
status.name // "unemployed" | "employed" | "student" | "retired"

since one of fields can present one at a time, this would act like an enum and we wouldn’t have to worry about wire format or interoperability as this is a normal way of using the language.

from protoc-gen-ts.

tomasweigenast avatar tomasweigenast commented on May 24, 2024 1

@thesayyn Here it says the ENUM specification: https://developers.google.com/protocol-buffers/docs/proto3#json
It must be a string, not an integer. If you use Protobuf with dart, it will serialize enums as strings, and this plugin will not understand that.

from protoc-gen-ts.

thesayyn avatar thesayyn commented on May 24, 2024

This seems like a good idea but this would require the introduction of an option just for this plugin. and that is something I would try to avoid.

besides, I don't know how would this translate to wire format as we have to keep things interoperable with other languages.

from protoc-gen-ts.

maZahaca avatar maZahaca commented on May 24, 2024

Well here is an option if you initialize a server from proto file, which automatically transforms enums to strings correctly:

const exampleProto = `${__dirname}/../../../../../proto/example.proto`;

const options = {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
};

const examplePackageDefinition = protoLoader.loadSync(exampleProto, options);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { examplepackage }: any = loadPackageDefinition(
  examplePackageDefinition,
);

const grpcServer = new Server();

grpcServer.addService(examplepackage.Example.service, {
  verifyMessage: serviceRequest<VerifyMessageInput, VerifyMessageResult>(
    verifyModel,
  ),
});

Basically when you pass an option enums: String while loading it transforms it to string.
Can we have something like this option for protoc-gen-ts ?

from protoc-gen-ts.

thesayyn avatar thesayyn commented on May 24, 2024

As far as I understand, you want to be able to get the name of an enum based on a value. At least that's what I got out of the enums option from proto-loader.

See: https://github.com/protobufjs/protobuf.js/blob/95b56817ef6fb9bdcb14d956c159da49d0889bff/tests/api_converters.js#L97
If you want to do the same, typescript has you covered.

https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings

Perhaps this could be an option for you?

from protoc-gen-ts.

maZahaca avatar maZahaca commented on May 24, 2024

Yes, this is it. The question was is it possible to have this conversion take place automatically? depends on some config as done for protoLoader options

from protoc-gen-ts.

thesayyn avatar thesayyn commented on May 24, 2024

I am not entirely sure that we can take the same approach as we do not compile protos on the runtime. they are compiled ahead of time thus we need a way to describe this behavior within the proto file. perhaps an option but we are against introducing any options that the upstream compiler does not provide.

I am open to any other suggestions apart from introducing a new option and force users to include the proto file that describes this option.

from protoc-gen-ts.

thesayyn avatar thesayyn commented on May 24, 2024

We won’t support this because neither it is in the spec nor could be useful for most users.

from protoc-gen-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.