Commit 2260f47
authored
chore(internal/protoveneer): support oneof fields (#10271)
Proto oneofs translate into Go as an unexported type.
Here is one example:
type CachedContent struct {
// Types that are assignable to Expiration:
// *CachedContent_ExpireTime
// *CachedContent_Ttl
Expiration isCachedContent_Expiration `protobuf_oneof:"expiration"`
...
}
As the comment says, the `Expiration` field can be populated by one
of two exported types, but the type of the field itself is not exported
(it is an interface type that the two exported types satisfy).
Oneof fields like this cause a problem for this code generator, which
writes conversion code between veneers and protos that looks like
return &pb.CachedContent{
Expiration: conversionFunction(...),
...
}
We cannot write conversion function because we cannot write
its return type.
This CL addresses the problem by adding two features. First,
the user can configure population functions that are passed the
two structs, proto and veneer. That makes it possible to set
oneof fields:
func popcc(p *pb.CachedContent, v *CachedContent) {
if [something about v] {
p.Expiration = &pb.CachedContent_ExpireTime{..}
} else {
p.Expiration = &pb.CachedkContent_Ttl{...}
}
}
The second feature is the `noConvert` option, which prevents the field
from being set on the struct literal.
So by setting `noConvert` and specifying populate functions,
A oneof field can be initialized from a corresponding veneer type.1 parent 0dee490 commit 2260f47
5 files changed
Lines changed: 100 additions & 10 deletions
File tree
- internal/protoveneer/cmd/protoveneer
- testdata/basic
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
| |||
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
| 287 | + | |
| 288 | + | |
291 | 289 | | |
292 | 290 | | |
293 | 291 | | |
294 | 292 | | |
295 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
296 | 300 | | |
297 | 301 | | |
298 | 302 | | |
| |||
340 | 344 | | |
341 | 345 | | |
342 | 346 | | |
343 | | - | |
344 | | - | |
345 | | - | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
346 | 352 | | |
347 | 353 | | |
348 | 354 | | |
| |||
351 | 357 | | |
352 | 358 | | |
353 | 359 | | |
| 360 | + | |
354 | 361 | | |
355 | 362 | | |
356 | 363 | | |
| |||
445 | 452 | | |
446 | 453 | | |
447 | 454 | | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
448 | 464 | | |
449 | 465 | | |
450 | 466 | | |
| |||
492 | 508 | | |
493 | 509 | | |
494 | 510 | | |
| 511 | + | |
495 | 512 | | |
496 | 513 | | |
497 | 514 | | |
| |||
756 | 773 | | |
757 | 774 | | |
758 | 775 | | |
759 | | - | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
760 | 781 | | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
761 | 785 | | |
762 | 786 | | |
763 | 787 | | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
764 | 792 | | |
765 | 793 | | |
766 | 794 | | |
767 | 795 | | |
768 | 796 | | |
769 | 797 | | |
770 | | - | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
771 | 803 | | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
772 | 807 | | |
773 | 808 | | |
774 | 809 | | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
775 | 814 | | |
776 | 815 | | |
777 | 816 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
Lines changed: 30 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
0 commit comments