-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shouldn't BuiltMap<String, BuiltSet<int>> work out of the box? #263
Comments
It has generated the builder factory for FYI, there is also |
Ah cool! Seems like an implicit Map<T, Set>? Lemme try that out first. |
Yes, for most purposes it beats the plain |
Thanks, BuiltSetMultimap was the way to go! ❤️ |
@davidmorgan Any way around this?
The data is:
I don't see why that wouldn't serialize. EDIT: |
Looks like it's not supported; to be honest I don't remember why; if you like you could try changing the plugin |
@davidmorgan So it seems that it is actually supported, which can be demonstrated by removing both Maybe it's just an oversight that these weren't removed. I've tested this using the below model and test, but it would be nice if you could just double check my findings! 🙂 The model: library model;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'serializers.dart';
part 'model.g.dart';
abstract class Model implements Built<Model, ModelBuilder> {
BuiltSetMultimap<String, int>? get builtSet;
BuiltListMultimap<String, int>? get builtList;
Model._();
factory Model([Function(ModelBuilder b) updates]) = _$Model;
factory Model.from(Map<String, dynamic> map) => serializers.deserializeWith(serializer, map) as Model;
Map<String, dynamic> /*!*/ toMap() => serializers.serializeWith(serializer, this) as Map<String, dynamic>;
@BuiltValueSerializer(serializeNulls: true)
static Serializer<Model> get serializer => _$modelSerializer;
} The test: import 'package:built_collection/built_collection.dart';
import 'package:dummy/model.dart';
import 'package:flutter_test/flutter_test.dart';
const setData = [1, 2, 3, 3, 3];
const listData = [1, 1, 1];
Model _createModel() {
return Model((b) => b
..builtSet = SetMultimapBuilder(<String, List<int>>{'hello': setData})
..builtList = ListMultimapBuilder(<String, List<int>>{'hello': listData}));
}
Map<String, dynamic> _createMap() {
return <String, dynamic>{
'builtSet': {
'hello': setData,
},
'builtList': {
'hello': listData,
}
};
}
void main() {
test('BuiltSetMultimap should serialize', () {
final instance = _createModel();
expect(instance.toMap()['builtSet']['hello'].length, 3);
});
test('BuiltListMultimap should serialize', () {
final instance = _createModel();
expect(instance.toMap()['builtList']['hello'].length, 3);
});
test('Model should deserialize', () {
expect(Model.from(_createMap()), _createModel());
});
} 00:07 +3: All tests passed! |
Hmmmm it might be relate to needing additional / more complex support if there is a discriminator, meaning that the type is written in the json. For example this would happen if there is a BuiltSetMultimap inside a One way to land this might be to narrow the restriction to only throw if there is a discriminator, i.e. to allow serialization/deserialization when the type is fully specified and to throw if it's not. You could see this in tests by adding that |
Sounds like a pretty good compromise for now. How to best proceed? It's not really urgent for us, as we have our workaround, but would obvously be nice to have something merged for official support. |
So this throws a DeserializationError.
Using this raw data:
[wv8P7YM21ryHwcvtCoGR, [688, 689, 690, 691, 692, 693, 694, 695, 696, 697]]
Thought it was supported.
Also tried making a custom builder factory for it, like the error message suggests - same error.
Any hints?
EDIT:
This is auto-generated in serializers.g.dart:
Looks correct? Doesn't work
The exception message:
The text was updated successfully, but these errors were encountered: