Skip to content

Commit

Permalink
支持import, 移除service/rpc支持
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Jun 16, 2021
1 parent 84c736c commit 2b52e6b
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 245 deletions.
72 changes: 68 additions & 4 deletions gen/pbscheme/gen_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/davyxu/protoplus/codegen"
"github.com/davyxu/protoplus/gen"
"github.com/davyxu/protoplus/model"
"io/ioutil"
"path/filepath"
"strings"
Expand Down Expand Up @@ -33,20 +34,83 @@ message {{.Name}} { {{range .Fields}}
{{end}}
`

type PBDescriptorSet struct {
model.DescriptorSet

// pb生成文件依赖时, 使用以下字段
DependentSource []string // 按文件管理的描述符取出时, 这个字段有效. 本文件依赖的其他source的symbiol
SourceName string // 按文件管理的描述符取出时, 这个字段有效. 表示本DescriptorSet的文件名
dsBySource map[string]*PBDescriptorSet // 按文件名管理的描述符集合
}

func (self *PBDescriptorSet) addDependentSource(name string) {

if self.SourceName == name {
return
}

for _, n := range self.DependentSource {
if n == name {
return
}
}

self.DependentSource = append(self.DependentSource, name)
}

func (self *PBDescriptorSet) DescriptorSetBySource() map[string]*PBDescriptorSet {
if self.dsBySource != nil {
return self.dsBySource
}

self.dsBySource = map[string]*PBDescriptorSet{}

for _, obj := range self.Objects {
ds := self.dsBySource[obj.SrcName]
if ds == nil {
ds = &PBDescriptorSet{}

ds.PackageName = self.PackageName
ds.Codec = self.Codec
ds.SourceName = obj.SrcName

self.dsBySource[obj.SrcName] = ds
}

ds.AddObject(obj)
}

for _, file := range self.dsBySource {
for _, st := range file.Structs() {

for _, fd := range st.Fields {

switch fd.Kind {
case model.Kind_Struct, model.Kind_Enum:
refTarget := self.ObjectByName(fd.Type)
if refTarget != nil {
file.addDependentSource(refTarget.SrcName)
}
}
}
}
}

return self.dsBySource
}

func GenProtoDir(ctx *gen.Context) error {

rootDS := ctx.DescriptorSet
rootDS := &PBDescriptorSet{DescriptorSet: *ctx.DescriptorSet}

var sb strings.Builder

for srcName, ds := range rootDS.DescriptorSetBySource() {

ctx.DescriptorSet = ds

gen := codegen.NewCodeGen("dirproto").
RegisterTemplateFunc(codegen.UsefulFunc).
RegisterTemplateFunc(UsefulFunc).
ParseTemplate(protoDirCodeTemplate, ctx)
ParseTemplate(protoDirCodeTemplate, ds)

if gen.Error() != nil {
fmt.Println(string(gen.Data()))
Expand Down
70 changes: 0 additions & 70 deletions model/descriptorset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ type DescriptorSet struct {
Objects []*Descriptor `json:",omitempty"`
PackageName string
Codec string

DependentSource []string // 按文件管理的描述符取出时, 这个字段有效. 本文件依赖的其他source的symbiol
SourceName string // 按文件管理的描述符取出时, 这个字段有效. 表示本DescriptorSet的文件名
dsBySource map[string]*DescriptorSet // 按文件名管理的描述符集合
}

func (self *DescriptorSet) Services() (ret []*Descriptor) {

for _, o := range self.Objects {
if o.Kind == Kind_Service {
ret = append(ret, o)
}
}

return
}

func (self *DescriptorSet) Structs() (ret []*Descriptor) {
Expand Down Expand Up @@ -68,58 +53,3 @@ func (self *DescriptorSet) ObjectByName(name string) *Descriptor {
func (self *DescriptorSet) AddObject(d *Descriptor) {
self.Objects = append(self.Objects, d)
}

func (self *DescriptorSet) addDependentSource(name string) {

if self.SourceName == name {
return
}

for _, n := range self.DependentSource {
if n == name {
return
}
}

self.DependentSource = append(self.DependentSource, name)
}

func (self *DescriptorSet) DescriptorSetBySource() map[string]*DescriptorSet {
if self.dsBySource != nil {
return self.dsBySource
}

self.dsBySource = map[string]*DescriptorSet{}

for _, obj := range self.Objects {
ds := self.dsBySource[obj.SrcName]
if ds == nil {
ds = &DescriptorSet{
PackageName: self.PackageName,
Codec: self.Codec,
SourceName: obj.SrcName,
}
self.dsBySource[obj.SrcName] = ds
}

ds.AddObject(obj)
}

for _, file := range self.dsBySource {
for _, st := range file.Structs() {

for _, fd := range st.Fields {

switch fd.Kind {
case Kind_Struct, Kind_Enum:
refTarget := self.ObjectByName(fd.Type)
if refTarget != nil {
file.addDependentSource(refTarget.SrcName)
}
}
}
}
}

return self.dsBySource
}
1 change: 0 additions & 1 deletion model/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const (
Kind_Primitive = "primitive"
Kind_Struct = "struct"
Kind_Enum = "enum"
Kind_Service = "service"
)

var SchemeType2Type = map[string]string{
Expand Down
33 changes: 27 additions & 6 deletions parser/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@ package parser
import (
"github.com/davyxu/golexer"
"github.com/davyxu/protoplus/model"
"strings"
)

type Context struct {

// 每个文件对应的属性
SourceName string

*protoParser

*model.DescriptorSet

*model.Descriptor
// 全局属性
symbolPos map[interface{}]golexer.TokenPos

sourceByName map[string]struct{}
}

*model.FieldDescriptor
func (self *Context) AddSource(sourceName string) bool {
lowerName := strings.ToLower(sourceName)
if _, ok := self.sourceByName[lowerName]; ok {
return false
}

*model.ServiceCall
self.sourceByName[lowerName] = struct{}{}

symbolPos map[interface{}]golexer.TokenPos
return true
}

func (self *Context) QuerySymbolPosString(v interface{}) string {
Expand All @@ -31,13 +42,23 @@ func (self *Context) QuerySymbolPosString(v interface{}) string {
}

func (self *Context) AddSymbol(v interface{}, pos golexer.TokenPos) {

self.symbolPos[v] = pos
}

func (self *Context) Clone(srcName string) *Context {

return &Context{
SourceName: srcName,
symbolPos: self.symbolPos,
sourceByName: self.sourceByName,
DescriptorSet: self.DescriptorSet,
}
}

func newContext() *Context {

return &Context{
symbolPos: make(map[interface{}]golexer.TokenPos),
symbolPos: map[interface{}]golexer.TokenPos{},
sourceByName: map[string]struct{}{},
}
}
18 changes: 2 additions & 16 deletions parser/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package parser

import (
"github.com/davyxu/protoplus/model"
"os"
"strings"
)

Expand Down Expand Up @@ -32,23 +31,10 @@ func ParseFileList(dset *model.DescriptorSet, filelist ...string) error {
ctx.DescriptorSet = dset

for _, filename := range filelist {

ctx.SourceName = filename

if file, err := os.Open(filename); err != nil {
err := parseFile(ctx, filename)
if err != nil {
return err
} else {

if err := rawParse(ctx, file); err != nil {
file.Close()

return err
}

file.Close()

}

}

return checkAndFix(ctx)
Expand Down
31 changes: 16 additions & 15 deletions parser/enumfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,46 @@ package parser

import (
"errors"
"github.com/davyxu/protoplus/model"
)

func parseEnumField(ctx *Context) {
func parseEnumField(ctx *Context, fd *model.FieldDescriptor) {

// 注释
nameToken := ctx.RawToken()

// 字段名
ctx.FieldDescriptor.Name = ctx.Expect(Token_Identifier).Value()
fd.Name = ctx.Expect(Token_Identifier).Value()

if ctx.FieldNameExists(ctx.FieldDescriptor.Name) {
panic(errors.New("Duplicate field name: " + ctx.FieldDescriptor.Name))
if fd.Descriptor.FieldNameExists(fd.Name) {
panic(errors.New("Duplicate field name: " + fd.Name))
}

// 有等号
if ctx.TokenID() == Token_Assign {
ctx.NextToken()

// tag
ctx.FieldDescriptor.Tag = ctx.Expect(Token_Numeral).ToInt()
fd.Tag = ctx.Expect(Token_Numeral).ToInt()

} else { // 没等号自动生成枚举序号

if len(ctx.Fields) == 0 {
//fd.AutoTag = 0
} else {

// 按前面的序号+1
//fd.AutoTag = d.MaxTag() + 1
}
//if len(ctx.Fields) == 0 {
// //fd.AutoTag = 0
//} else {
//
// // 按前面的序号+1
// //fd.AutoTag = d.MaxTag() + 1
//}

}

ctx.FieldDescriptor.Comment = ctx.CommentGroupByLine(nameToken.Line())
fd.Comment = ctx.CommentGroupByLine(nameToken.Line())

// 枚举值类型,始终为int32
ctx.FieldDescriptor.ParseType("int32")
fd.ParseType("int32")

ctx.AddField(ctx.FieldDescriptor)
fd.Descriptor.AddField(fd)

return
}
Loading

0 comments on commit 2b52e6b

Please sign in to comment.