Skip to content
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

add plugin annotation #688

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ 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
40 changes: 40 additions & 0 deletions plugin/annotation/annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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
551 changes: 276 additions & 275 deletions test/casttype/combos/both/casttype.pb.go

Large diffs are not rendered by default.

551 changes: 276 additions & 275 deletions test/casttype/combos/marshaler/casttype.pb.go

Large diffs are not rendered by default.

543 changes: 272 additions & 271 deletions test/casttype/combos/neither/casttype.pb.go

Large diffs are not rendered by default.

549 changes: 275 additions & 274 deletions test/casttype/combos/unmarshaler/casttype.pb.go

Large diffs are not rendered by default.

501 changes: 251 additions & 250 deletions test/castvalue/castvalue.pb.go

Large diffs are not rendered by default.

485 changes: 243 additions & 242 deletions test/castvalue/combos/both/castvalue.pb.go

Large diffs are not rendered by default.

499 changes: 250 additions & 249 deletions test/castvalue/combos/marshaler/castvalue.pb.go

Large diffs are not rendered by default.

483 changes: 242 additions & 241 deletions test/castvalue/combos/unmarshaler/castvalue.pb.go

Large diffs are not rendered by default.

847 changes: 424 additions & 423 deletions test/combos/both/thetest.pb.go

Large diffs are not rendered by default.

849 changes: 425 additions & 424 deletions test/combos/marshaler/thetest.pb.go

Large diffs are not rendered by default.

849 changes: 425 additions & 424 deletions test/combos/unmarshaler/thetest.pb.go

Large diffs are not rendered by default.

509 changes: 255 additions & 254 deletions test/example/example.pb.go

Large diffs are not rendered by default.

493 changes: 247 additions & 246 deletions test/filedotname/file.dot.pb.go

Large diffs are not rendered by default.

487 changes: 244 additions & 243 deletions test/group/group.pb.go

Large diffs are not rendered by default.

469 changes: 235 additions & 234 deletions test/mapdefaults/combos/both/map.pb.go

Large diffs are not rendered by default.

505 changes: 253 additions & 252 deletions test/mapdefaults/combos/marshaler/map.pb.go

Large diffs are not rendered by default.

505 changes: 253 additions & 252 deletions test/mapdefaults/combos/neither/map.pb.go

Large diffs are not rendered by default.

495 changes: 248 additions & 247 deletions test/mapdefaults/combos/unmarshaler/map.pb.go

Large diffs are not rendered by default.

607 changes: 304 additions & 303 deletions test/mapsproto2/combos/both/mapsproto2.pb.go

Large diffs are not rendered by default.

605 changes: 303 additions & 302 deletions test/mapsproto2/combos/marshaler/mapsproto2.pb.go

Large diffs are not rendered by default.

605 changes: 303 additions & 302 deletions test/mapsproto2/combos/neither/mapsproto2.pb.go

Large diffs are not rendered by default.

605 changes: 303 additions & 302 deletions test/mapsproto2/combos/unmarshaler/mapsproto2.pb.go

Large diffs are not rendered by default.

541 changes: 271 additions & 270 deletions test/oneof/combos/both/one.pb.go

Large diffs are not rendered by default.

533 changes: 267 additions & 266 deletions test/oneof/combos/marshaler/one.pb.go

Large diffs are not rendered by default.

541 changes: 271 additions & 270 deletions test/oneof/combos/neither/one.pb.go

Large diffs are not rendered by default.

539 changes: 270 additions & 269 deletions test/oneof/combos/unmarshaler/one.pb.go

Large diffs are not rendered by default.

511 changes: 256 additions & 255 deletions test/oneof3/combos/both/one.pb.go

Large diffs are not rendered by default.

479 changes: 240 additions & 239 deletions test/oneof3/combos/marshaler/one.pb.go

Large diffs are not rendered by default.

483 changes: 242 additions & 241 deletions test/oneof3/combos/neither/one.pb.go

Large diffs are not rendered by default.

509 changes: 255 additions & 254 deletions test/oneof3/combos/unmarshaler/one.pb.go

Large diffs are not rendered by default.

1,003 changes: 502 additions & 501 deletions test/theproto3/combos/both/theproto3.pb.go

Large diffs are not rendered by default.

1,006 changes: 503 additions & 503 deletions test/theproto3/combos/marshaler/theproto3.pb.go

Large diffs are not rendered by default.

1,007 changes: 504 additions & 503 deletions test/theproto3/combos/neither/theproto3.pb.go

Large diffs are not rendered by default.

1,006 changes: 503 additions & 503 deletions test/theproto3/combos/unmarshaler/theproto3.pb.go

Large diffs are not rendered by default.

848 changes: 424 additions & 424 deletions test/thetest.pb.go

Large diffs are not rendered by default.

509 changes: 255 additions & 254 deletions test/unrecognized/unrecognized.pb.go

Large diffs are not rendered by default.

499 changes: 250 additions & 249 deletions test/unrecognizedgroup/unrecognizedgroup.pb.go

Large diffs are not rendered by default.

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