Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
5d13b84c7a | |||
ee4d83458f |
73
ast.go
73
ast.go
@@ -15,47 +15,58 @@ import (
|
|||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var astFields = make(map[string]map[string]map[string]*structtag.Tags) // map proto file with proto message ast struct
|
||||||
astFields = make(map[string]map[string]map[string]*structtag.Tags) // map proto file with proto message ast struct
|
|
||||||
)
|
func (g *Generator) astFill(file *protogen.File, message *protogen.Message) error {
|
||||||
|
for _, field := range message.Fields {
|
||||||
|
if field.Desc.Options() == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !proto.HasExtension(field.Desc.Options(), tag_options.E_Tags) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := proto.GetExtension(field.Desc.Options(), tag_options.E_Tags)
|
||||||
|
if opts != nil {
|
||||||
|
fpath := filepath.Join(g.tagPath, file.GeneratedFilenamePrefix+".pb.go")
|
||||||
|
mp, ok := astFields[fpath]
|
||||||
|
if !ok {
|
||||||
|
mp = make(map[string]map[string]*structtag.Tags)
|
||||||
|
}
|
||||||
|
nmp, ok := mp[message.GoIdent.GoName]
|
||||||
|
if !ok {
|
||||||
|
nmp = make(map[string]*structtag.Tags)
|
||||||
|
}
|
||||||
|
tags, err := structtag.Parse(opts.(string))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
nmp[field.GoName] = tags
|
||||||
|
mp[message.GoIdent.GoName] = nmp
|
||||||
|
astFields[fpath] = mp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, nmessage := range message.Messages {
|
||||||
|
if err := g.astFill(file, nmessage); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *Generator) astGenerate(plugin *protogen.Plugin) error {
|
func (g *Generator) astGenerate(plugin *protogen.Plugin) error {
|
||||||
if g.tagPath == "" {
|
if g.tagPath == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range plugin.Files {
|
for _, file := range plugin.Files {
|
||||||
if !file.Generate {
|
if !file.Generate {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, message := range file.Messages {
|
for _, message := range file.Messages {
|
||||||
for _, field := range message.Fields {
|
if err := g.astFill(file, message); err != nil {
|
||||||
if field.Desc.Options() == nil {
|
return err
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !proto.HasExtension(field.Desc.Options(), tag_options.E_Tags) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := proto.GetExtension(field.Desc.Options(), tag_options.E_Tags)
|
|
||||||
if opts != nil {
|
|
||||||
fpath := filepath.Join(g.tagPath, file.GeneratedFilenamePrefix+".pb.go")
|
|
||||||
mp, ok := astFields[fpath]
|
|
||||||
if !ok {
|
|
||||||
mp = make(map[string]map[string]*structtag.Tags)
|
|
||||||
}
|
|
||||||
nmp, ok := mp[message.GoIdent.GoName]
|
|
||||||
if !ok {
|
|
||||||
nmp = make(map[string]*structtag.Tags)
|
|
||||||
}
|
|
||||||
tags, err := structtag.Parse(opts.(string))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
nmp[field.GoName] = tags
|
|
||||||
mp[message.GoIdent.GoName] = nmp
|
|
||||||
astFields[fpath] = mp
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
http.go
1
http.go
@@ -29,6 +29,7 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
|
|||||||
|
|
||||||
gfile.Import(contextPackage)
|
gfile.Import(contextPackage)
|
||||||
gfile.Import(microApiPackage)
|
gfile.Import(microApiPackage)
|
||||||
|
|
||||||
if genClient {
|
if genClient {
|
||||||
gfile.Import(microClientPackage)
|
gfile.Import(microClientPackage)
|
||||||
gfile.Import(microClientHttpPackage)
|
gfile.Import(microClientHttpPackage)
|
||||||
|
32
util.go
32
util.go
@@ -10,19 +10,17 @@ import (
|
|||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var httpMethodMap = map[string]string{
|
||||||
httpMethodMap = map[string]string{
|
"GET": "MethodGet",
|
||||||
"GET": "MethodGet",
|
"HEAD": "MethodHead",
|
||||||
"HEAD": "MethodHead",
|
"POST": "MethodPost",
|
||||||
"POST": "MethodPost",
|
"PUT": "MethodPut",
|
||||||
"PUT": "MethodPut",
|
"PATCH": "MethodPatch",
|
||||||
"PATCH": "MethodPatch",
|
"DELETE": "MethodDelete",
|
||||||
"DELETE": "MethodDelete",
|
"CONNECT": "MethodConnect",
|
||||||
"CONNECT": "MethodConnect",
|
"OPTIONS": "MethodOptions",
|
||||||
"OPTIONS": "MethodOptions",
|
"TRACE": "MethodTrace",
|
||||||
"TRACE": "MethodTrace",
|
}
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func unexport(s string) string {
|
func unexport(s string) string {
|
||||||
return strings.ToLower(s[:1]) + s[1:]
|
return strings.ToLower(s[:1]) + s[1:]
|
||||||
@@ -30,7 +28,7 @@ func unexport(s string) string {
|
|||||||
|
|
||||||
func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||||
serviceName := service.GoName
|
serviceName := service.GoName
|
||||||
//if rule, ok := getMicroApiService(service); ok {
|
// if rule, ok := getMicroApiService(service); ok {
|
||||||
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
|
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
|
||||||
// }
|
// }
|
||||||
gfile.P("type ", unexport(serviceName), "Client struct {")
|
gfile.P("type ", unexport(serviceName), "Client struct {")
|
||||||
@@ -62,7 +60,11 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
|
|||||||
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
|
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
|
||||||
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
|
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
|
||||||
}
|
}
|
||||||
gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}")
|
if ref == "micro.codec.Frame" {
|
||||||
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}")
|
||||||
|
} else {
|
||||||
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,7 @@ var (
|
|||||||
microClientPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/client")
|
microClientPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/client")
|
||||||
microServerPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/server")
|
microServerPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/server")
|
||||||
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
|
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
|
||||||
|
microCodecPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/codec")
|
||||||
timePackage = protogen.GoImportPath("time")
|
timePackage = protogen.GoImportPath("time")
|
||||||
deprecationComment = "// Deprecated: Do not use."
|
deprecationComment = "// Deprecated: Do not use."
|
||||||
versionComment = "v3.4.2"
|
versionComment = "v3.4.2"
|
||||||
|
Reference in New Issue
Block a user