Skip to content

Add serializer plugin that does renames #144

@vadimtsushko

Description

@vadimtsushko

That is one of requirements, that probably appears only when one works with external API.
In my case models have many String fields that can have values only from some predefined list:
For example, the field chartType can have values

  • table
  • barchart
  • linechart
  • pivot-table

and so on. Ideally, I would like to use Enums for such fields, but some of the possible values (pivot-table in above example) are not valid dart identifiers.

It would be great If we could change serialized value of Enum by some annotation.

For example, given enum

class SecondTestEnum extends EnumClass {
  static const SecondTestEnum yes = _$ys;
  static const SecondTestEnum no = _$n;
  static const SecondTestEnum definitely = _$definitely;
  @SerializedAs('no-thanks')
  static const SecondTestEnum noThanks = _$noThanks;
  const SecondTestEnum._(String name) : super(name);
  static Serializer<SecondTestEnum> get serializer => _$secondTestEnumSerializer;
  static BuiltSet<SecondTestEnum> get values => _$vls;
  static SecondTestEnum valueOf(String name) => _$vlOf(name);
}

Its implementation would be:

const SecondTestEnum _$ys = const SecondTestEnum._('yes');
const SecondTestEnum _$n = const SecondTestEnum._('no');
const SecondTestEnum _$definitely = const SecondTestEnum._('definitely');
const SecondTestEnum _$noThanks = const SecondTestEnum._('no-thanks');

SecondTestEnum _$vlOf(String name) {
  switch (name) {
    case 'yes':
      return _$ys;
    case 'no':
      return _$n;
    case 'definitely':
      return _$definitely;
    case 'no-thanks':
      return _$noThanks;
    default:
      throw new ArgumentError(name);
  }
}

...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions