Commit 5213254
authored
proto: store extension values according to protobuf data model (#746)
The current API represents scalar extension fields as *T and
repeated extension fields as []T.
However, this is not an accurate reflection of the protobuf data model.
For scalars, pointers are usually used to represent nullability. However,
in the case of extension scalars, there is no need to do so since presence
information is captured by checking whether the field is in the extension map.
For this reason, presence on extension scalars is not determined by checking
whether the returned pointer is nil, but whether HasExtension reports true.
For repeated fields, using []T means that the returned value is only a partially
mutable value. You can swap out elements, but you cannot change the length of
the original field value. On the other hand, the reflective API provides methods
on repeated field values that permit operations that do change the length.
Thus, using *[]T is a closer match to the protobuf data model.
This CL changes the implementation of extension fields to always store T
for scalars, and *[]T for repeated fields. However, for backwards compatibility,
the API continues to provide *T and []T. In theory, this could break anyone
that relies on memory aliasing for *T. However, this is unlikely since:
* use of extensions themselves are relatively rare
* if extensions are used, it is recommended practice to use a message as the
field extension and not a scalar
* relying on memory aliasing is generally not a good idiom to follow.
The expected pattern is to call SetExtension to make it explicit that a mutation
is happening on the message.
* analysis within Google demonstrates that no one is relying on this behavior.1 parent 951a149 commit 5213254
File tree
6 files changed
+141
-42
lines changed- proto
6 files changed
+141
-42
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
70 | 76 | | |
71 | 77 | | |
72 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
73 | 90 | | |
74 | 91 | | |
75 | 92 | | |
76 | 93 | | |
77 | 94 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
86 | 99 | | |
87 | | - | |
88 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
89 | 103 | | |
90 | | - | |
91 | | - | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
92 | 107 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
98 | 114 | | |
99 | 115 | | |
100 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
| 249 | + | |
| 250 | + | |
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
189 | 203 | | |
190 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
191 | 207 | | |
192 | 208 | | |
193 | 209 | | |
| |||
334 | 350 | | |
335 | 351 | | |
336 | 352 | | |
337 | | - | |
| 353 | + | |
338 | 354 | | |
339 | 355 | | |
340 | 356 | | |
| |||
349 | 365 | | |
350 | 366 | | |
351 | 367 | | |
352 | | - | |
| 368 | + | |
353 | 369 | | |
354 | 370 | | |
355 | 371 | | |
356 | | - | |
| 372 | + | |
357 | 373 | | |
358 | 374 | | |
359 | 375 | | |
| |||
500 | 516 | | |
501 | 517 | | |
502 | 518 | | |
503 | | - | |
| 519 | + | |
504 | 520 | | |
505 | 521 | | |
506 | 522 | | |
| |||
541 | 557 | | |
542 | 558 | | |
543 | 559 | | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
86 | 89 | | |
87 | 90 | | |
88 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
94 | 98 | | |
95 | | - | |
96 | | - | |
97 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
98 | 103 | | |
99 | 104 | | |
100 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| |||
407 | 408 | | |
408 | 409 | | |
409 | 410 | | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
410 | 414 | | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
411 | 420 | | |
412 | 421 | | |
413 | 422 | | |
414 | 423 | | |
415 | 424 | | |
416 | 425 | | |
| 426 | + | |
417 | 427 | | |
418 | 428 | | |
419 | 429 | | |
| |||
2310 | 2320 | | |
2311 | 2321 | | |
2312 | 2322 | | |
2313 | | - | |
2314 | | - | |
| 2323 | + | |
| 2324 | + | |
2315 | 2325 | | |
2316 | 2326 | | |
2317 | 2327 | | |
| |||
2329 | 2339 | | |
2330 | 2340 | | |
2331 | 2341 | | |
2332 | | - | |
2333 | | - | |
| 2342 | + | |
| 2343 | + | |
2334 | 2344 | | |
2335 | 2345 | | |
2336 | 2346 | | |
| |||
2399 | 2409 | | |
2400 | 2410 | | |
2401 | 2411 | | |
2402 | | - | |
| 2412 | + | |
2403 | 2413 | | |
2404 | 2414 | | |
2405 | 2415 | | |
| |||
2434 | 2444 | | |
2435 | 2445 | | |
2436 | 2446 | | |
2437 | | - | |
| 2447 | + | |
2438 | 2448 | | |
2439 | 2449 | | |
2440 | 2450 | | |
| |||
2465 | 2475 | | |
2466 | 2476 | | |
2467 | 2477 | | |
2468 | | - | |
| 2478 | + | |
2469 | 2479 | | |
2470 | 2480 | | |
2471 | 2481 | | |
| |||
2510 | 2520 | | |
2511 | 2521 | | |
2512 | 2522 | | |
2513 | | - | |
| 2523 | + | |
2514 | 2524 | | |
2515 | 2525 | | |
2516 | 2526 | | |
| |||
2553 | 2563 | | |
2554 | 2564 | | |
2555 | 2565 | | |
2556 | | - | |
| 2566 | + | |
2557 | 2567 | | |
2558 | 2568 | | |
2559 | 2569 | | |
| |||
2591 | 2601 | | |
2592 | 2602 | | |
2593 | 2603 | | |
2594 | | - | |
| 2604 | + | |
2595 | 2605 | | |
2596 | 2606 | | |
2597 | 2607 | | |
| |||
2621 | 2631 | | |
2622 | 2632 | | |
2623 | 2633 | | |
2624 | | - | |
| 2634 | + | |
2625 | 2635 | | |
2626 | 2636 | | |
2627 | 2637 | | |
| |||
2656 | 2666 | | |
2657 | 2667 | | |
2658 | 2668 | | |
2659 | | - | |
| 2669 | + | |
2660 | 2670 | | |
2661 | 2671 | | |
2662 | 2672 | | |
| |||
0 commit comments