-
Notifications
You must be signed in to change notification settings - Fork 183
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
Unable to initialise a null map field by field number #373
Comments
Before this change, repeated fields are initialised in the _getDefault method but maps are not. This change initialises maps. This fixes google#373
I believe this can be fixed with a change to the I've sent a quick PR showing a potential fix. I'm not totally familiar with the internals so it's possible I've missed something, but this fixes my bug and the protobuf tests all pass. Let me know what you think. |
Before this change, repeated fields are initialised in the _getDefault method but maps are not. This change initialises maps. This fixes google#373
Before this change, repeated fields are initialised in the _getDefault method but maps are not. This change initialises maps. This fixes google#373
@sigurdm - Do you mind taking a look at this? This is going back awhile, but it's related to this: #340 (comment) In other words, your suggestion there won't work for the reason @dave has pointed out |
Would be awesome to get dave@f0605ba in, as I also ran into this issue, and there doesn't seem to the a "client side" workaround (apart from switching to Dave's branch). |
Unfortunately dave@f0605ba is not a good fix for this issue, and it breaks a few things. I'm currently working on fixing this. In the meantime here's a hacky workaround: Proto file: syntax = "proto3";
message A {
map<int32, int32> a = 1;
} Dart: var a = A.create();
// Get index of the map field in generated message. 1 here is the tag of
// the field as specified in the proto definition.
var mapFieldInfo = a.info_.fieldInfo[1]!;
var mapFieldIndex = mapFieldInfo.index!;
PbMap<int, int> map = a.$_getMap(mapFieldIndex) as PbMap<int, int>;
map[1] = 2;
print(a); // prints "a : {1 : 2}" |
- `$_getMap` when called on an uninitialized map field initializes the field. Update `getField` to do the same. `getField` already initializes repeated fields, but it used to not initialize map fields. Fixes #373 This is a sync of cl/442686654. - Inline `_getDefaultList` into `_$getList` and `_getDefaultMap` into `_$getMap`. These inlined functions have only one use site, and inlining them makes the `_$getList` and `_$getMap` easier to compare for consistency.
If a map field isn't set (e.g. it's null), then it doesn't seem to be possible to modify it by field number. It looks like
getField
returns the default value, and updates to that value are not reflected in the message... I've prepared a simplified example:Is there any way around this?
The text was updated successfully, but these errors were encountered: