schultek / stormberry

Access your postgres database effortlessly from dart code.
https://pub.dev/packages/stormberry
MIT License
68 stars 17 forks source link

add basic enum type converter #28

Closed TimWhiting closed 2 years ago

TimWhiting commented 2 years ago

Still haven't made sure not to override user's custom type converters, but does this look like the right direction @schultek?

schultek commented 2 years ago

Direction is good. I think we could define a general EnumTypeConverter or EnumConverter that is then just used for each enum, without the need to create custom classes for each enum. Something like:

// somewhere inside stormberry package

class EnumTypeConverter<T extends Enum> extends TypeConverter<T> {

  const EnumTypeConverter(this.values);
  final List<T> values;

  @override
  String encode(T value) => value.name;

  @override
  T decode(dynamic value) => values.byName(value as String);
}

// inside the generated code

final registry = ModelRegistry({
  typeOf<EnumValue>():  EnumTypeConverter<EnumValue>(EnumValue.values),
  ...
});
TimWhiting commented 2 years ago

Done, also tested to make sure enum serializers defined by users override those defined by us. (In maps / map literals the last entry with a key overwrites previous entries with the same key, so I just put enum serializers before all other serializers).

schultek commented 2 years ago

It should be doable to check the enums agains the custom type converters in the builder right? I think it would be much cleaner to remove those duplicates if its not too much work.

Thanks anyways.

TimWhiting commented 2 years ago

Done