Skip to content

Conversation

frankee
Copy link

@frankee frankee commented Dec 21, 2017

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
  1. The file option
Option (gogoproto bsontag_all) = true;
  1. 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.

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.
@awalterschulze
Copy link
Member

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.
If allows you to edit the tags before you send it to the generator.

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?

@bufdev bufdev mentioned this pull request Jan 11, 2018
@bufdev
Copy link
Contributor

bufdev commented Jan 11, 2018

See #368

@bufdev
Copy link
Contributor

bufdev commented Jan 12, 2018

@frankee can you take a look at #368 and give your opinion? Especially regarding the protobuf vs json tag.

frankee and others added 8 commits June 6, 2018 08:19
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants