Comments (8)
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.
@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.
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.
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.
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.
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.
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.
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)
- Get `Error: EAGAIN: resource temporarily unavailable, read` when maybe many files are specified as input HOT 1
- Support Promises in Server Definition
- npm package needs publishing to include fix for typescript >= 5 HOT 8
- The namespace 'Google protobuf' does not have an exported member 'BinaryWriter' HOT 1
- `toObject()` inferred type too long when compiled with `declaration: true` HOT 1
- More fleshed out example
- Proto with oneof and other field is generated as union instead of intersect
- `Property 'xy' of type 'z' is not assignable to 'string' index type 'UntypedHandleCall'.` for class properties
- Add `as const` to UnimplementedService definition
- TSC error with compiler option noUnusedLocals HOT 2
- import_style=es6 ? HOT 1
- Consuming JSON For Tests and Converting To Protoc HOT 1
- I need to generate file interface IUser {} HOT 3
- Property 'Buffer' does not exist on type 'typeof globalThis' HOT 2
- [rust] Error starting the proto build with new rust wasm package. Error when executing /usr/bin/protoc-gen-ts HOT 1
- [rust] proto3 optional fields support
- methodName conflict HOT 1
- snake_case to camelCase conversion HOT 7
- [1.0] Moving away from JavaScript implementation HOT 1
- http2 is null when trying to use a web client in the browser
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protoc-gen-ts.