forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(internal/protoveneer): support oneof fields (googleapis#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.
- Loading branch information
Showing
5 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,9 @@ types: | |
fields: | ||
Uri: | ||
name: URI | ||
|
||
Pop: | ||
fields: | ||
Y: | ||
noConvert: true | ||
populateToFrom: popYTo, popYFrom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters