Skip to content

Commit 60c7190

Browse files
committed
add plugin annotation, which add a annotation of string for message type, which can be used like JAVA's runtime annotation.
1 parent 5628607 commit 60c7190

File tree

8 files changed

+186
-85
lines changed

8 files changed

+186
-85
lines changed

gogoproto/gogo.pb.go

Lines changed: 96 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gogoproto/gogo.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ extend google.protobuf.MessageOptions {
124124

125125
optional bool goproto_sizecache = 64034;
126126
optional bool goproto_unkeyed = 64035;
127+
128+
optional string annotation = 64036;
127129
}
128130

129131
extend google.protobuf.FieldOptions {

gogoproto/helper.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ func IsUnion(file *google_protobuf.FileDescriptorProto, message *google_protobuf
306306
return proto.GetBoolExtension(message.Options, E_Onlyone, proto.GetBoolExtension(file.Options, E_OnlyoneAll, false))
307307
}
308308

309+
310+
func GetStringAnnotation(message *google_protobuf.DescriptorProto) string {
311+
return proto.GetStringExtension(message.Options, E_Annotation)
312+
}
313+
309314
func HasGoString(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool {
310315
return proto.GetBoolExtension(message.Options, E_Gostring, proto.GetBoolExtension(file.Options, E_GostringAll, false))
311316
}

plugin/annotation/annotation.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package annotation
2+
3+
import (
4+
"github.com/gogo/protobuf/gogoproto"
5+
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
6+
)
7+
8+
9+
type annotation struct {
10+
*generator.Generator
11+
generator.PluginImports
12+
}
13+
14+
func NewAnnotation() *annotation {
15+
return &annotation{}
16+
}
17+
18+
func (p *annotation) Name() string {
19+
return "annotation"
20+
}
21+
22+
func (p *annotation) Init(g *generator.Generator) {
23+
p.Generator = g
24+
}
25+
26+
func (p *annotation) Generate(file *generator.FileDescriptor) {
27+
p.PluginImports = generator.NewPluginImports(p.Generator)
28+
for _, message := range file.Messages() {
29+
if gogoproto.GetStringAnnotation(message.DescriptorProto) == "" {
30+
continue
31+
}
32+
33+
ccTypeName := generator.CamelCaseSlice(message.TypeName())
34+
p.P(`func init(){ proto.RegisterAnnotation(`,
35+
`(*`, ccTypeName, ")(nil), `",gogoproto.GetStringAnnotation(message.DescriptorProto) ,"`)}")
36+
}
37+
}
38+
39+
func init() {
40+
generator.RegisterPlugin(NewAnnotation())
41+
}

proto/extensions_gogo.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ func GetBoolExtension(pb Message, extension *ExtensionDesc, ifnotset bool) bool
7979
return *(value.(*bool))
8080
}
8181

82+
func GetStringExtension(pb Message, extension *ExtensionDesc) string {
83+
if reflect.ValueOf(pb).IsNil() {
84+
return ""
85+
}
86+
value, err := GetExtension(pb, extension)
87+
if err != nil {
88+
return ""
89+
}
90+
91+
if value.(*string) == nil {
92+
return ""
93+
}
94+
return *(value.(*string))
95+
}
96+
8297
func (this *Extension) Equal(that *Extension) bool {
8398
if err := this.Encode(); err != nil {
8499
return false

proto/properties.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,31 @@ func MessageType(name string) reflect.Type {
595595
return protoMapTypes[name]
596596
}
597597

598+
// A registry of message type's annotations.
599+
var (
600+
annotations = make(map[reflect.Type]string) // message type => annotation
601+
)
602+
603+
// RegisterAnnotation is called from generated code and maps from the
604+
// message type to its annotation.
605+
func RegisterAnnotation(x Message, annotation string) {
606+
annotations[reflect.TypeOf(x)] = annotation
607+
}
608+
609+
// GetAnnotation returns the annotation for a message.
610+
func GetAnnotation(x Message) string {
611+
return annotations[reflect.TypeOf(x)]
612+
}
613+
614+
// GetAnnotations returns all annotations registered.
615+
func GetAnnotations() []string {
616+
var ret []string
617+
for _, v := range annotations {
618+
ret = append(ret, v)
619+
}
620+
return ret
621+
}
622+
598623
// A registry of all linked proto files.
599624
var (
600625
protoFiles = make(map[string][]byte) // file name => fileDescriptor

protoc-gen-gogo/generator/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func (g *Generator) CommandLineParameters(parameter string) {
548548
if pluginList == "none" {
549549
pluginList = ""
550550
}
551-
gogoPluginNames := []string{"unmarshal", "unsafeunmarshaler", "union", "stringer", "size", "protosizer", "populate", "marshalto", "unsafemarshaler", "gostring", "face", "equal", "enumstringer", "embedcheck", "description", "defaultcheck", "oneofcheck", "compare"}
551+
gogoPluginNames := []string{"annotation", "unmarshal", "unsafeunmarshaler", "union", "stringer", "size", "protosizer", "populate", "marshalto", "unsafemarshaler", "gostring", "face", "equal", "enumstringer", "embedcheck", "description", "defaultcheck", "oneofcheck", "compare"}
552552
pluginList = strings.Join(append(gogoPluginNames, pluginList), "+")
553553
if pluginList != "" {
554554
// Amend the set of plugins.

vanity/command/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"os"
3636
"strings"
3737

38+
_ "github.com/gogo/protobuf/plugin/annotation"
3839
_ "github.com/gogo/protobuf/plugin/compare"
3940
_ "github.com/gogo/protobuf/plugin/defaultcheck"
4041
_ "github.com/gogo/protobuf/plugin/description"

0 commit comments

Comments
 (0)