c++ - Overwriting a repeated field in protobuf MergeFrom vs. MergeFromString -
suppose have proto defined as:
myproto { optional mywrapper wrapper = 1; }
where:
mywrapper { repeated int32 data = 1; }
when call mergefromstring
on 2 text specifications of myproto
, 2 versions of repeated field inside wrapper concatenated (one appended other.) want them overwritten instead. docs mergefromstring
say:
when find field in |serialized| present in message:
- if it's "repeated" field, append end of our list.
- else, if it's scalar, overwrite our field.
- else, (it's nonrepeated composite), recursively merge existing composite.
clearly, wrapper, we're talking third case. recursively merge, , on next go-around see repeated field , values appended target. see why happened.
compare specification mergefrom
:
this method merges contents of specified message current message. singular fields set in specified message overwrite corresponding fields in current message. repeated fields appended. singular sub-messages , groups recursively merged.
in case, isn't wrapper field singular field, , wouldn't wrapper overridden?
so question two-fold,
1) inconsistent, or have misunderstood something?
2) how can desired behavior of overwriting instead of merging repeated field when call mergefromstring
?
for first part of question, mergefrom
specification quoted technically correct although worded bit confusingly. says "singular fields" overwritten "singular sub-messages" recursively merged, , wrapper considered singular sub- message.
to behavior want, should able use fieldmaskutil. in particular can call fieldmaskutil::mergemessageto(...)
, pass mergeoptions
configured replace repeated fields instead of concatenating them. first have parse 2 messages text format representation.
Comments
Post a Comment