jsontag and moretags

5,312 views
Skip to first unread message

Walter Schulze

unread,
Aug 5, 2014, 7:58:18 AM8/5/14
to gogopr...@googlegroups.com
I have added two new features jsontag and moretags to allow you to modify the generated tags so as to better integrate with other serializers that use reflect.

I have added a new feature jsontag, which is in alpha since I only have one test.
This allows you to change the value of the json tag.

See this example
https://code.google.com/p/gogoprotobuf/source/browse/test/tags/tags.proto

Typically the jsontag is generated as the fieldname.
Lets show another example:

message A {
  optional string Field1 = 1;
}

will generate

type A struct {
    Field1      string                                   `protobuf:"bytes,1,opt" json:"Field1"`
}

Lets see what happens if we use the jsontag extension

message A {
  optional string Field1 = 1  [(gogoproto.jsontag) = "MyField1"];
}

Now our generated code looks like this

type A struct {
    Field1      string                                   `protobuf:"bytes,1,opt" json:"MyField1"`
}

The other new feature moretags is experimental, since I only have one test and I am not sure what developers will want the behaviour of XXX_recognized and XXX_extensions to be.
Basically moretags allows you to add more tags to the end of the tag.

message A {
  optional string Field1 = 1  [(gogoproto.moretags) = "xml:\",comment\""];
}

This generates extra tags like so

type A struct {
    Field1      string                                   `protobuf:"bytes,1,opt" json:"Field1" xml:",comment"`
}

Enjoy.
I hope you find these useful.

ste...@stipl.net

unread,
Aug 29, 2019, 9:35:05 AM8/29/19
to gogoprotobuf
Hi,
this is super useful and works perfectly for declared fields, thanks! However, I was wondering is there currently any way to specify tags for XXX_* fields?

I'm trying to add a `datastore:"-"` tag (same way as `json:"-"` tag is added by default).

Thanks

cam...@nb.io

unread,
Sep 4, 2019, 5:11:19 PM9/4/19
to gogoprotobuf
I'm also very interested in the ability to add a `firestore:"-"` tag to the XXX_ fields. Perhaps moretags_all sets the default and declared field options may override it?
Reply all
Reply to author
Forward
0 new messages