Skip to content

Commit

Permalink
add plugin annotation, which add a annotation of string for message t…
Browse files Browse the repository at this point in the history
…ype, which can be used like JAVA's runtime annotation.
  • Loading branch information
u2takey committed May 17, 2020
1 parent 5628607 commit 60c7190
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 85 deletions.
180 changes: 96 additions & 84 deletions gogoproto/gogo.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions gogoproto/gogo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ extend google.protobuf.MessageOptions {

optional bool goproto_sizecache = 64034;
optional bool goproto_unkeyed = 64035;

optional string annotation = 64036;
}

extend google.protobuf.FieldOptions {
Expand Down
5 changes: 5 additions & 0 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ func IsUnion(file *google_protobuf.FileDescriptorProto, message *google_protobuf
return proto.GetBoolExtension(message.Options, E_Onlyone, proto.GetBoolExtension(file.Options, E_OnlyoneAll, false))
}


func GetStringAnnotation(message *google_protobuf.DescriptorProto) string {
return proto.GetStringExtension(message.Options, E_Annotation)
}

func HasGoString(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool {
return proto.GetBoolExtension(message.Options, E_Gostring, proto.GetBoolExtension(file.Options, E_GostringAll, false))
}
Expand Down
41 changes: 41 additions & 0 deletions plugin/annotation/annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package annotation

import (
"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
)


type annotation struct {
*generator.Generator
generator.PluginImports
}

func NewAnnotation() *annotation {
return &annotation{}
}

func (p *annotation) Name() string {
return "annotation"
}

func (p *annotation) Init(g *generator.Generator) {
p.Generator = g
}

func (p *annotation) Generate(file *generator.FileDescriptor) {
p.PluginImports = generator.NewPluginImports(p.Generator)
for _, message := range file.Messages() {
if gogoproto.GetStringAnnotation(message.DescriptorProto) == "" {
continue
}

ccTypeName := generator.CamelCaseSlice(message.TypeName())
p.P(`func init(){ proto.RegisterAnnotation(`,
`(*`, ccTypeName, ")(nil), `",gogoproto.GetStringAnnotation(message.DescriptorProto) ,"`)}")
}
}

func init() {
generator.RegisterPlugin(NewAnnotation())
}
15 changes: 15 additions & 0 deletions proto/extensions_gogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ func GetBoolExtension(pb Message, extension *ExtensionDesc, ifnotset bool) bool
return *(value.(*bool))
}

func GetStringExtension(pb Message, extension *ExtensionDesc) string {
if reflect.ValueOf(pb).IsNil() {
return ""
}
value, err := GetExtension(pb, extension)
if err != nil {
return ""
}

if value.(*string) == nil {
return ""
}
return *(value.(*string))
}

func (this *Extension) Equal(that *Extension) bool {
if err := this.Encode(); err != nil {
return false
Expand Down
25 changes: 25 additions & 0 deletions proto/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,31 @@ func MessageType(name string) reflect.Type {
return protoMapTypes[name]
}

// A registry of message type's annotations.
var (
annotations = make(map[reflect.Type]string) // message type => annotation
)

// RegisterAnnotation is called from generated code and maps from the
// message type to its annotation.
func RegisterAnnotation(x Message, annotation string) {
annotations[reflect.TypeOf(x)] = annotation
}

// GetAnnotation returns the annotation for a message.
func GetAnnotation(x Message) string {
return annotations[reflect.TypeOf(x)]
}

// GetAnnotations returns all annotations registered.
func GetAnnotations() []string {
var ret []string
for _, v := range annotations {
ret = append(ret, v)
}
return ret
}

// A registry of all linked proto files.
var (
protoFiles = make(map[string][]byte) // file name => fileDescriptor
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-gogo/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func (g *Generator) CommandLineParameters(parameter string) {
if pluginList == "none" {
pluginList = ""
}
gogoPluginNames := []string{"unmarshal", "unsafeunmarshaler", "union", "stringer", "size", "protosizer", "populate", "marshalto", "unsafemarshaler", "gostring", "face", "equal", "enumstringer", "embedcheck", "description", "defaultcheck", "oneofcheck", "compare"}
gogoPluginNames := []string{"annotation", "unmarshal", "unsafeunmarshaler", "union", "stringer", "size", "protosizer", "populate", "marshalto", "unsafemarshaler", "gostring", "face", "equal", "enumstringer", "embedcheck", "description", "defaultcheck", "oneofcheck", "compare"}
pluginList = strings.Join(append(gogoPluginNames, pluginList), "+")
if pluginList != "" {
// Amend the set of plugins.
Expand Down
1 change: 1 addition & 0 deletions vanity/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"os"
"strings"

_ "github.com/gogo/protobuf/plugin/annotation"
_ "github.com/gogo/protobuf/plugin/compare"
_ "github.com/gogo/protobuf/plugin/defaultcheck"
_ "github.com/gogo/protobuf/plugin/description"
Expand Down

0 comments on commit 60c7190

Please sign in to comment.