microsoft / microsoft/typespec

@field on Enum members

Open
#8,166 0 comments 0 reactions 1 assignee Claimed by @witemple-msft View on GitHub
compiler:core design:needed triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Clear and concise description of the problem

Right now when you define an enum that for `@typespec/protobuf` you have to give it a numeric value. For example:

```proto
enum DiagnosticLevel {
debug: 0,
info: 1,
warning: 2,
error: 3,
}
```

This works fine, but if I want to use the same enum with another emitter like `@typespec/http` it is currently impossible to have it serialize as a string. What you would normally do would be to change the enum to be

```proto
enum DiagnosticLevel {
debug: "debug",
info: "info",
warning: "warning",
error: "error",
}
```
However this is a compiler error for proto since they have to be numbers. I attempted to apply the `@field(1)` attribute but this is not valid for enum members. My suggestion would be to enable this so I could define my enum like this and make everyone happy.

```proto
enum DiagnosticLevel {
@field(1) debug: "debug",
@field(2) info: "info",
@field(3) warning: "warning",
@field(4) error: "error",
}
```

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

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.