Last active
September 19, 2018 14:34
-
-
Save yugui/e179eee28268e85c5036859987f8a15e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by protoc-gen-go. DO NOT EDIT. | |
// source: custom-options.proto | |
/* | |
Package main is a generated protocol buffer package. | |
It is generated from these files: | |
custom-options.proto | |
It has these top-level messages: | |
MessageListOptions | |
*/ | |
package main | |
import proto "github.com/golang/protobuf/proto" | |
import fmt "fmt" | |
import math "math" | |
import google_protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor" | |
// Reference imports to suppress errors if they are not otherwise used. | |
var _ = proto.Marshal | |
var _ = fmt.Errorf | |
var _ = math.Inf | |
// This is a compile-time assertion to ensure that this generated file | |
// is compatible with the proto package it is being compiled against. | |
// A compilation error at this line likely means your copy of the | |
// proto package needs to be updated. | |
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package | |
type MessageListOptions struct { | |
Target bool `protobuf:"varint,10,opt,name=target" json:"target,omitempty"` | |
} | |
func (m *MessageListOptions) Reset() { *m = MessageListOptions{} } | |
func (m *MessageListOptions) String() string { return proto.CompactTextString(m) } | |
func (*MessageListOptions) ProtoMessage() {} | |
func (*MessageListOptions) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } | |
func (m *MessageListOptions) GetTarget() bool { | |
if m != nil { | |
return m.Target | |
} | |
return false | |
} | |
var E_MessageList = &proto.ExtensionDesc{ | |
ExtendedType: (*google_protobuf.MessageOptions)(nil), | |
ExtensionType: (*MessageListOptions)(nil), | |
Field: 50000, | |
Name: "example.message_list", | |
Tag: "bytes,50000,opt,name=message_list,json=messageList", | |
Filename: "custom-options.proto", | |
} | |
func init() { | |
proto.RegisterType((*MessageListOptions)(nil), "example.MessageListOptions") | |
proto.RegisterExtension(E_MessageList) | |
} | |
func init() { proto.RegisterFile("custom-options.proto", fileDescriptor0) } | |
var fileDescriptor0 = []byte{ | |
// 179 bytes of a gzipped FileDescriptorProto | |
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x49, 0x2e, 0x2d, 0x2e, | |
0xc9, 0xcf, 0xd5, 0xcd, 0x2f, 0x28, 0xc9, 0xcc, 0xcf, 0x2b, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, | |
0x17, 0x62, 0x4f, 0xad, 0x48, 0xcc, 0x2d, 0xc8, 0x49, 0x95, 0x52, 0x48, 0xcf, 0xcf, 0x4f, 0xcf, | |
0x49, 0xd5, 0x07, 0x0b, 0x27, 0x95, 0xa6, 0xe9, 0xa7, 0xa4, 0x16, 0x27, 0x17, 0x65, 0x16, 0x94, | |
0xe4, 0x17, 0x41, 0x94, 0x2a, 0xe9, 0x70, 0x09, 0xf9, 0xa6, 0x16, 0x17, 0x27, 0xa6, 0xa7, 0xfa, | |
0x64, 0x16, 0x97, 0xf8, 0x43, 0x8c, 0x11, 0x12, 0xe3, 0x62, 0x2b, 0x49, 0x2c, 0x4a, 0x4f, 0x2d, | |
0x91, 0xe0, 0x52, 0x60, 0xd4, 0xe0, 0x08, 0x82, 0xf2, 0xac, 0x12, 0xb9, 0x78, 0x72, 0x21, 0xaa, | |
0xe3, 0x73, 0x32, 0x8b, 0x4b, 0x84, 0xe4, 0xf5, 0x20, 0x16, 0xe8, 0xc1, 0x2c, 0xd0, 0x83, 0x1a, | |
0x06, 0x35, 0x48, 0xe2, 0x42, 0x1b, 0xb3, 0x02, 0xa3, 0x06, 0xb7, 0x91, 0xb4, 0x1e, 0xd4, 0x49, | |
0x7a, 0x98, 0xb6, 0x05, 0x71, 0xe7, 0x22, 0xc4, 0x9c, 0xd8, 0xa2, 0x58, 0x72, 0x13, 0x33, 0xf3, | |
0x92, 0xd8, 0xc0, 0x46, 0x1a, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xb5, 0xa5, 0xdc, 0x88, 0xe2, | |
0x00, 0x00, 0x00, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
package example; | |
option go_package = "main"; | |
import "google/protobuf/descriptor.proto"; | |
message MessageListOptions { | |
bool target = 10; | |
} | |
extend google.protobuf.MessageOptions { | |
MessageListOptions message_list = 50000; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"io" | |
"io/ioutil" | |
"log" | |
"os" | |
"github.com/golang/protobuf/proto" | |
descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor" | |
plugin "github.com/golang/protobuf/protoc-gen-go/plugin" | |
) | |
func parseReq(r io.Reader) (*plugin.CodeGeneratorRequest, error) { | |
buf, err := ioutil.ReadAll(r) | |
if err != nil { | |
return nil, err | |
} | |
var req plugin.CodeGeneratorRequest | |
if err = proto.Unmarshal(buf, &req); err != nil { | |
return nil, err | |
} | |
return &req, nil | |
} | |
func listNames(file *descriptor.FileDescriptorProto) []string { | |
var list []string | |
for _, m := range file.MessageType { | |
if !isTarget(m) { | |
continue | |
} | |
list = append(list, m.GetName()) | |
} | |
return list | |
} | |
func isTarget(m *descriptor.DescriptorProto) bool { | |
var opts = m.GetOptions() | |
if opts == nil { | |
return false | |
} | |
ext, err := proto.GetExtension(opts, E_MessageList) | |
if err == proto.ErrMissingExtension { | |
return false | |
} | |
if err != nil { | |
panic("unexpected error") | |
} | |
mopts := ext.(*MessageListOptions) | |
return mopts.GetTarget() | |
} | |
func processReq(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse { | |
files := make(map[string]*descriptor.FileDescriptorProto) | |
for _, f := range req.ProtoFile { | |
files[f.GetName()] = f | |
} | |
var buf bytes.Buffer | |
for _, fname := range req.FileToGenerate { | |
f := files[fname] | |
for _, name := range listNames(f) { | |
io.WriteString(&buf, name) | |
io.WriteString(&buf, "\n") | |
} | |
} | |
return &plugin.CodeGeneratorResponse{ | |
File: []*plugin.CodeGeneratorResponse_File{ | |
{ | |
Name: proto.String("messages.txt"), | |
Content: proto.String(buf.String()), | |
}, | |
}, | |
} | |
} | |
func emitResp(resp *plugin.CodeGeneratorResponse) error { | |
buf, err := proto.Marshal(resp) | |
if err != nil { | |
return err | |
} | |
_, err = os.Stdout.Write(buf) | |
return err | |
} | |
func run() error { | |
req, err := parseReq(os.Stdin) | |
if err != nil { | |
return err | |
} | |
resp := processReq(req) | |
return emitResp(resp) | |
} | |
func main() { | |
if err := run(); err != nil { | |
log.Fatalln(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment