google / built_value.dart

Immutable value types, enum classes, and serialization.
https://pub.dev/packages/built_value
BSD 3-Clause "New" or "Revised" License
867 stars 184 forks source link

Generate field names as strings similar to C# nameof operator #1167

Open jan-siroky opened 2 years ago

jan-siroky commented 2 years ago

I would like to ask you to consider adding functionality that would generate field names to strings that would be accessible as static members of a class or an enum.

For example I am using reactive_forms package. When defining formgroup object, I have to use string literals for map keys which creates room for errors.

FormGroup({
  'name': FormControl<String>(),
  'email': FormControl<String>(),
});

I would much rather have something like this (static class members):

FormGroup({
  User.fields.name: FormControl<String>(),
  User.fields.email: FormControl<String>(),
});

or enum style:

FormGroup({
  UserFields.name.name: FormControl<String>(),
  UserFields.email.name: FormControl<String>(),
});

This would be useful for many more use cases, but this is the main for me.

Thank you for developing this wonderful library!

davidmorgan commented 2 years ago

Hmmmmm in general I like the idea of making class metadata more usefully available.

We could create an enum for you. With dart 2.17 it could even be a real enum.

I don't often get to work on built_value features, I'll see if I can take a look at this one next time I do. Thanks.

jan-siroky commented 2 years ago

That would be nice. Thank you for considering!