github.com/junaozun/protoc-gen-go-clone@v0.0.0-20231229025521-89199567f720/plugin/clone/plugin.go (about)

     1  package clone
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/junaozun/protoc-gen-go-clone/generator"
     7  
     8  	"github.com/golang/protobuf/protoc-gen-go/descriptor"
     9  )
    10  
    11  // Plugin is an implementation of the Go protocol buffer compiler's
    12  // plugin architecture.  It generates bindings for Plugin support.
    13  type Plugin struct {
    14  	gen      *generator.Generator
    15  	parseFd  *parseFd
    16  	importFd map[string]bool
    17  }
    18  
    19  // Name returns the name of this plugin, "Plugin".
    20  func (g *Plugin) Name() string {
    21  	return "clone"
    22  }
    23  
    24  // The names for packages imported in the generated code.
    25  // They may vary from the final path component of the import path
    26  // if the name is used by other packages.
    27  
    28  // Init initializes the plugin.
    29  func (g *Plugin) Init(gen *generator.Generator) {
    30  	g.gen = gen
    31  	g.parseFd = NewParseFd(gen)
    32  	g.importFd = make(map[string]bool)
    33  }
    34  
    35  // Given a type name defined in a .proto, return its object.
    36  // Also record that we're using it, to guarantee the associated import.
    37  func (g *Plugin) objectNamed(name string) generator.Object {
    38  	g.gen.RecordTypeUse(name)
    39  	return g.gen.ObjectNamed(name)
    40  }
    41  
    42  // Given a type name defined in a .proto, return its name as we will print it.
    43  func (g *Plugin) typeName(str string) string {
    44  	return g.gen.TypeName(g.objectNamed(str))
    45  }
    46  
    47  // P forwards to g.gen.P.
    48  func (g *Plugin) P(args ...interface{}) { g.gen.P(args...) }
    49  
    50  // Generate generates code for the services in the given file.
    51  func (g *Plugin) Generate(file *generator.FileDescriptor) {
    52  	ok := g.parseFd.ParseFileDesc(file, func(comments []*descriptor.SourceCodeInfo_Location) []string {
    53  		return parseSpecifiedComments(comments)
    54  	})
    55  	for k := range g.gen.GetImport() {
    56  		g.importFd[string(k)] = true
    57  	}
    58  
    59  	if !ok {
    60  		return
    61  	}
    62  
    63  	clone := NewGenClone(g.parseFd)
    64  	clone.GenClone(file.GetPackage())
    65  	for k := range g.importFd {
    66  		// 需要过滤一下无用的import
    67  		m := strings.Split(k, "/")
    68  		imp := m[len(m)-1]
    69  		if !clone.ContainString(imp) {
    70  			continue
    71  		}
    72  		g.gen.AddImport(generator.GoImportPath(k))
    73  	}
    74  
    75  }
    76  
    77  // GenerateImports generates the import declaration for this file.
    78  func (g *Plugin) GenerateImports(file *generator.FileDescriptor) {
    79  	// g.P("var _ = context.Backgroud()")
    80  }
    81  
    82  // user指定解析注释格式
    83  func parseSpecifiedComments(comments []*descriptor.SourceCodeInfo_Location) []string {
    84  	var res []string
    85  	for _, lc := range comments {
    86  		if lc.GetLeadingComments() == "" {
    87  			continue
    88  		}
    89  		var strName string
    90  		for i, v := range lc.GetLeadingComments() {
    91  			if string(v) == "|" {
    92  				strName = lc.GetLeadingComments()[i+1:]
    93  				break
    94  			}
    95  		}
    96  		if strName == "" {
    97  			continue
    98  		}
    99  		strName = strings.Replace(strName, " ", "", -1)
   100  		// 去除换行符
   101  		strName = strings.Replace(strName, "\n", "", -1)
   102  		res = append(res, strName)
   103  	}
   104  	return res
   105  }