Closed Norbert515 closed 4 years ago
and is used in another package
You should register your enum from external package manually via adapter as stated in the readme. https://github.com/k-paxian/dart-json-mapper#enum-types
As you have already discovered, enums are automatically registered for local package only.
The readme states
Enums from third party packages, they can not be annotated.
but I certainly can (and have) annotated the enums from said package. Is there any reason for this limitation?
It is pretty common to split up an app into different modules (some being purely dart) at some point. Registering them in the main adapter works, but in that case, it would be easier to have the enumValues
property back so the responsibility isn't scattered around the code.
There is a slightly better way to chain adapters from libraries you own. Let's imagine you have two libraries A,B and third party library C:
libraryA.dart
@jsonSerializable
enum Test { one, two }
libraryA.mapper.g.dart
final libraryAAdapter = JsonMapperAdapter(
title: 'libraryAAdapter',
enumValues: {
Test: Test.values
});
libraryB.dart
@jsonSerializable
enum TestB { one, two, three }
libraryB.mapper.g.dart
final libraryBAdapter = JsonMapperAdapter(
title: 'libraryBAdapter',
enumValues: {
TestB: TestB.values
});
libraryC.dart
enum ThirdPartyEnum { Fizz, Buzz }
And on the target App side you would:
main.dart
void main() async {
initializeJsonMapper(adapters: [
libraryAAdapter,
libraryBAdapter,
JsonMapperAdapter(
enumValues: {
ThirdPartyEnum: ThirdPartyEnum.values
})
]);
var it = JsonMapper.toMap(SomeOtherClass(Test.one, TestB.three, ThirdPartyEnum.Fizz));
print(it);
}
I don't see any responsibility scattering
in this example, could you please illustrate what exactly made you think so?
Registering particular enum with adapter you are doing it once per application, whereas using enumValues
property on annotations, you are forced to repeat yourself as many times as you refer to this particular enum, which is not aligned with the DRY principle
Gotcha. Yeah, the adapters for different packages definitely work. I was just wondering what the reason for it not automatically picking it up is - but I suppose it could lead to weird cases where it's not clear who is responsible for the initialization.
Thanks for the detailed response!
When an enum is declared in a local package
and is used in another package
enumValues are not captured in main.mapper.g.dart: