-
Notifications
You must be signed in to change notification settings - Fork 810
add bson tag for bson serialization, used for mongodb. #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
When we need go struct, which protobuf generated, to support the bson serialization, within the gogoprotobuf, we can use `moretags`. However in general, the value of `bson` tag is consistent with the `json` tag, so we only need to control whether the generator automatically adds `bson` tag by a switch, which defaults to false. Currently, three switches have been added, 1. The generator CMD parameter ``` Protoc - gofast_out = bsontag:. My. Proto ``` 2. The file option ``` Option (gogoproto bsontag_all) = true; ``` 3. The message option ``` Option (gogoproto bsontag) = true; ``` The bson tag will check from the command line parameter to the message option, and the settings will override the previous one. The specific value of the bson tag can be specified by `moretags`, eg. `moretags = "bson:\"fieldName\ "`, if `moretags` is not specified, then will automatically generate value for bson tag: 1. First get the `json` attribute in `protobuf` tag, and if it exists, use the value; 2. If the above does not exist, the value of the `json` tag is used.
Adding the command generator is a bit non conformant, and then it would suggest that we need to add a command generator for all tags. This is what the vanity binaries are for. For example https://github.com/gogo/protobuf/blob/master/protoc-gen-gogoslick/main.go This will allow you to not need to create your own tag and simply use moretags to create bson tags for all fields, without the user having to specify them. What do you think? |
See #368 |
like this: message Building { optional string id = 1; optional string name = 2; } message Floor { optional string id = 1; optional string name = 2; } message Place { option (gogoproto.union) = "type,typeField"; oneof union { Floor floor = 1; Building building = 2; } } there is no change for the protobuffer, only changed for the json format. so the Place will format like this: { "@type" : "Building", "id": "id", "name": "name" }
convenient for bson or other serializer
When we need go struct, which protobuf generated, to support the bson serialization, within the gogoprotobuf, we can use
moretags
.However in general, the value of
bson
tag is consistent with thejson
tag, so we only need to control whether the generator automatically addsbson
tag by a switch, which defaults to false.Currently, three switches have been added,
The bson tag will check from the command line parameter to the message option, and the settings will override the previous one.
The specific value of the bson tag can be specified by
moretags
, eg.moretags = "bson:\"fieldName\ "
, ifmoretags
is not specified, then will automatically generate value for bson tag:json
attribute inprotobuf
tag, and if it exists, use the value;json
tag is used.