Closed TimWhiting closed 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),
...
});
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).
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.
Done
Still haven't made sure not to override user's custom type converters, but does this look like the right direction @schultek?