Overwriting a repeated field in protobuf MergeFrom vs. MergeFromString
<p>Suppose I have a proto defined as:</p>

<pre><code>MyProto {
    optional MyWrapper wrapper = 1;
}
</code></pre>

<p>where:</p>

<pre><code>MyWrapper {
    repeated int32 data = 1;
}
</code></pre>

<p>When I call <code>MergeFromString</code> on two text specifications of <code>MyProto</code>, the two versions of the repeated field inside the wrapper are concatenated (one is appended to the other.) I really want them to be overwritten instead. The docs for <code>MergeFromString</code> say:</p>

<blockquote>
  <p>When we find a field in |serialized| that is already present in this
  message:</p>
  
  <ul>
  <li>If it's a "repeated" field, we append to the end of our list.</li>
  <li>Else, if it's a scalar, we overwrite our field.</li>
  <li>Else, (it's a nonrepeated composite), we recursively merge
  into the existing composite.</li>
  </ul>
</blockquote>

<p>Clearly, with the wrapper, we're talking about the third case. So we recursively merge, and on the next go-around we see a repeated field and the values get appended to the target. So I see why this happened.</p>

<p>Compare this to the specification for <code>MergeFrom</code>:</p>

<blockquote>
  <p>This method merges the contents of the specified message into the
  current message. Singular fields that are set in the specified message
  overwrite the corresponding fields in the current message. Repeated
  fields are appended. Singular sub-messages and groups are recursively
  merged.</p>
</blockquote>

<p>In that case, isn't the wrapper field a singular field, and wouldn't the wrapper be overridden? </p>

<p>So my question is two-fold,</p>

<p>1) Is this inconsistent, or have I misunderstood something?</p>

<p>2) How can I get my desired behavior of overwriting instead of merging a repeated field when I call <code>MergeFromString</code>? </p>