pbmeta

package module
v0.0.0-...-20fa095 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2019 License: MIT Imports: 12 Imported by: 36

README

pbmeta

Google Protobuf的Golang的Descriptor系列的封装扩展 带有对SourceCodeInfo支持,可以proto中对结构字段的各种注释做meta扩展

protoc-gen-meta

通过protoc的plugin机制扩展,将plugin.proto中定义的CodeGeneratorRequest输出成文件

本插件输出的FileDescriptorSet中包含有SourceCodeInfo,可以借由注释来拓展protobuf的meta描述能力

注意: protoc的-o功能也可以输出FileDescriptorSet,但出于性能和数据量考虑,并不包含SourceCodeInfo部分

使用方法

  • 编译出的protoc-gen-meta

  • 编写批处理/shell调用protoc调用protoc-gen-meta导出你的proto文件的二进制描述

  • 通过pbmeta库读取

功能实现及限制

  • 嵌套结构不能被很好的支持, 建议使用全局结构

原理参考

我的博客有核心原理描述 http://www.cppblog.com/sunicdavy/archive/2015/03/01/209894.html

官方descriptor.proto升级方法

如果官方有descriptor的更新,可以根据下面方法进行更新,重新生成golang代码

go get https://github.com/google/protobuf

go get https://github.com/golang/protobuf

go install https://github.com/golang/protobuf/protoc-gen-go

确保本项目与上面两个项目在同一个GOPATH下

修改compiler/plugin.pb.go的 import google_protobuf "google/protobuf" 为 import google_protobuf "github.com/davyxu/pbmeta/proto"

  • Windows 运行方法 proto/GenerateProto.bat

备注

感觉不错请star, 谢谢!

知乎: http://www.zhihu.com/people/sunicdavy

Documentation

Index

Constants

View Source
const (
	Token_EOF = iota
	Token_LeftBrace
	Token_RightBrace
	Token_WhiteSpace
	Token_LineEnd
	Token_UnixStyleComment
	Token_Identifier
	Token_Unknown
)

自定义的token id

Variables

This section is empty.

Functions

func LoadFileDescriptorSet

func LoadFileDescriptorSet(filename string) (*pbprotos.FileDescriptorSet, error)

文件格式必须是由protoc-gen-meta输出的CodeGeneratorRequest格式

Types

type CommentMeta

type CommentMeta struct {
	// contains filtered or unexported fields
}

func (*CommentMeta) FindTaggedComment

func (self *CommentMeta) FindTaggedComment(name string) (string, bool)

func (*CommentMeta) LeadingComment

func (self *CommentMeta) LeadingComment() string

func (*CommentMeta) ParseTaggedComment

func (self *CommentMeta) ParseTaggedComment() []*TaggedComment

解析带有tag的comment, 类似于go结构体中的命名tag

func (*CommentMeta) TrailingComment

func (self *CommentMeta) TrailingComment() string

type CommentParser

type CommentParser struct {
	*golexer.Parser
}

func NewCommentParser

func NewCommentParser(srcName string) *CommentParser

func (*CommentParser) Run

func (self *CommentParser) Run(src string, commentArray []*TaggedComment) (ret []*TaggedComment)

type Descriptor

type Descriptor struct {
	Define  *pbprotos.DescriptorProto
	EnumSet // 内嵌枚举
	CommentMeta

	NestedMsg MessageSet // 内嵌结构
	// contains filtered or unexported fields
}

func (*Descriptor) Contains

func (self *Descriptor) Contains(fd *FieldDescriptor) bool

func (*Descriptor) Field

func (self *Descriptor) Field(index int) *FieldDescriptor

func (*Descriptor) FieldByName

func (self *Descriptor) FieldByName(name string) *FieldDescriptor

func (*Descriptor) FieldByNumber

func (self *Descriptor) FieldByNumber(number int32) *FieldDescriptor

func (*Descriptor) FieldCount

func (self *Descriptor) FieldCount() int

func (*Descriptor) Name

func (self *Descriptor) Name() string

type DescriptorPool

type DescriptorPool struct {
	// contains filtered or unexported fields
}

func CreatePoolByFile

func CreatePoolByFile(filename string) (*DescriptorPool, error)

根据文件描述符创建描述池

func NewDescriptorPool

func NewDescriptorPool(fds *pbprotos.FileDescriptorSet) *DescriptorPool

func (*DescriptorPool) EnumByFullName

func (self *DescriptorPool) EnumByFullName(fullname string) *EnumDescriptor

通过全名取枚举

func (*DescriptorPool) File

func (self *DescriptorPool) File(index int) *FileDescriptor

取文件描述符

func (*DescriptorPool) FileByName

func (self *DescriptorPool) FileByName(name string) *FileDescriptor

获取文件描述符

func (*DescriptorPool) FileCount

func (self *DescriptorPool) FileCount() int

文件描述符数量

func (*DescriptorPool) MessageByFullName

func (self *DescriptorPool) MessageByFullName(fullname string) *Descriptor

通过全名取消息

type EnumDescriptor

type EnumDescriptor struct {
	Define *pbprotos.EnumDescriptorProto
	CommentMeta
	// contains filtered or unexported fields
}

func (*EnumDescriptor) Name

func (self *EnumDescriptor) Name() string

func (*EnumDescriptor) String

func (self *EnumDescriptor) String() string

func (*EnumDescriptor) Value

func (self *EnumDescriptor) Value(index int) *EnumValueDescriptor

func (*EnumDescriptor) ValueByName

func (self *EnumDescriptor) ValueByName(name string) *EnumValueDescriptor

func (*EnumDescriptor) ValueByNumber

func (self *EnumDescriptor) ValueByNumber(number int32) *EnumValueDescriptor

func (*EnumDescriptor) ValueCount

func (self *EnumDescriptor) ValueCount() int

type EnumSet

type EnumSet struct {
	// contains filtered or unexported fields
}

func (*EnumSet) Enum

func (self *EnumSet) Enum(index int) *EnumDescriptor

func (*EnumSet) EnumByName

func (self *EnumSet) EnumByName(name string) *EnumDescriptor

func (*EnumSet) EnumCount

func (self *EnumSet) EnumCount() int

func (*EnumSet) EnumValueByName

func (self *EnumSet) EnumValueByName(name string) *EnumValueDescriptor

type EnumValueDescriptor

type EnumValueDescriptor struct {
	Define *pbprotos.EnumValueDescriptorProto
	CommentMeta
}

func (*EnumValueDescriptor) Name

func (self *EnumValueDescriptor) Name() string

func (*EnumValueDescriptor) Value

func (self *EnumValueDescriptor) Value() int32

type FieldDescriptor

type FieldDescriptor struct {
	Define *pbprotos.FieldDescriptorProto
	CommentMeta
	// contains filtered or unexported fields
}

func (*FieldDescriptor) DefaultValue

func (self *FieldDescriptor) DefaultValue() string

func (*FieldDescriptor) EnumDesc

func (self *FieldDescriptor) EnumDesc() *EnumDescriptor

func (*FieldDescriptor) FullTypeName

func (self *FieldDescriptor) FullTypeName() string

pb定义中插件返回的格式时 .package.type.value的格式, 这里返回去掉头的.

func (*FieldDescriptor) IsEnumType

func (self *FieldDescriptor) IsEnumType() bool

func (*FieldDescriptor) IsMessageType

func (self *FieldDescriptor) IsMessageType() bool

func (*FieldDescriptor) IsOptional

func (self *FieldDescriptor) IsOptional() bool

func (*FieldDescriptor) IsRepeated

func (self *FieldDescriptor) IsRepeated() bool

func (*FieldDescriptor) IsRequired

func (self *FieldDescriptor) IsRequired() bool

func (*FieldDescriptor) Label

func (*FieldDescriptor) MessageDesc

func (self *FieldDescriptor) MessageDesc() *Descriptor

func (*FieldDescriptor) Name

func (self *FieldDescriptor) Name() string

func (*FieldDescriptor) Type

func (*FieldDescriptor) TypeName

func (self *FieldDescriptor) TypeName() string

type FileDescriptor

type FileDescriptor struct {
	Define *pbprotos.FileDescriptorProto

	EnumSet
	MessageSet
	// contains filtered or unexported fields
}

func (*FileDescriptor) Comment

func (*FileDescriptor) FileName

func (self *FileDescriptor) FileName() string

func (*FileDescriptor) PackageName

func (self *FileDescriptor) PackageName() string

type MessageSet

type MessageSet struct {
	// contains filtered or unexported fields
}

func (*MessageSet) DebugPrint

func (self *MessageSet) DebugPrint()

func (*MessageSet) Message

func (self *MessageSet) Message(index int) *Descriptor

func (*MessageSet) MessageByName

func (self *MessageSet) MessageByName(name string) *Descriptor

func (*MessageSet) MessageCount

func (self *MessageSet) MessageCount() int

type TaggedComment

type TaggedComment struct {
	Name string
	Data string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL