google / google/built_value.dart

Support reverse lookup of wireNames

Open
#663 9 comments 2 reactions 1 assignee Claimed by @davidmorgan View on GitHub
p1 / feature request
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

A small feature request to have `wireName` values be part of the reverse lookup that `valueOf` provides.

If I have an enum like this:
```
class CountryCode extends EnumClass {
@BuiltValueEnumConst(wireName: 'DK')
static const CountryCode DENMARK = _$dk;

@BuiltValueEnumConst(wireName: 'CH')
static const CountryCode SWITZERLAND = _$ch;

const CountryCode._(String name) : super(name);

static Serializer get serializer => _$countryCodeSerializer;
static BuiltSet get values => _$values;
static CountryCode valueOf(String name) => _$valueOf(name);
}
```

the generated valueOf function looks like this:
```
CountryCode _$valueOf(String name) {
switch (name) {
case 'DENMARK':
return _$dk;
case 'SWITZERLAND':
return _$ch;
default:
throw new ArgumentError(name);
}
}
```

But there's no way to do `CountryCode.valueOf('CH')`, even though 'CH' might be the only value available in the program flow. So the optimal generated `valueOf` would include this:

```
CountryCode _$valueOf(String name) {
switch (name) {
case 'DENMARK':
return _$dk;
case 'SWITZERLAND':
return _$ch;
case 'DK':
return _$dk;
case 'CH':
return _$ch;
default:
throw new ArgumentError(name);
}
}
```

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.