Skip to content

Commit 7dd7184

Browse files
committed
fix: update
1 parent 2fba3ff commit 7dd7184

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

cmd/cmds/protoc/cmd.go

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/emicklei/proto"
17+
"github.com/pubgo/x/iox"
1718
"github.com/pubgo/x/pathutil"
1819
"github.com/pubgo/xerror"
1920
"github.com/urfave/cli/v2"
@@ -24,6 +25,7 @@ import (
2425
"github.com/pubgo/lava/pkg/env"
2526
"github.com/pubgo/lava/pkg/lavax"
2627
"github.com/pubgo/lava/pkg/modutil"
28+
"github.com/pubgo/lava/pkg/protoutil"
2729
"github.com/pubgo/lava/pkg/shutil"
2830
)
2931

@@ -297,7 +299,7 @@ func Cmd() *cli.Command {
297299
var protoList sync.Map
298300
for i := range protoRoot {
299301
if pathutil.IsNotExist(protoRoot[i]) {
300-
zap.S().Warnf("file %s not flund", protoRoot[i])
302+
log.Printf("proto root (%s) not flund\n", protoRoot[i])
301303
continue
302304
}
303305

@@ -319,66 +321,69 @@ func Cmd() *cli.Command {
319321
}))
320322
}
321323

324+
// 处理检测gateway url
322325
var handler = func(protoFile string) {
323-
reader, err := os.Open(protoFile)
324-
xerror.Panic(err, protoFile)
325-
defer reader.Close()
326+
var data, err = iox.ReadText(protoFile)
327+
xerror.Panic(err)
326328

327-
parser := proto.NewParser(reader)
329+
parser := proto.NewParser(strings.NewReader(data))
328330
definition, err := parser.Parse()
329331
xerror.Panic(err, protoFile)
330332

333+
// package name
331334
var pkg string
332335
proto.Walk(definition, proto.WithPackage(func(p *proto.Package) {
333336
var replacer = strings.NewReplacer(".", "/", "-", "/")
334337
pkg = replacer.Replace(p.Name)
335338
}))
336339

337-
//pkg= "/" + protoutil.Camel2Case(fmt.Sprintf("%s/%s/%s", protoutil.Camel2Case(pkg), protoutil.Camel2Case(srv), protoutil.Camel2Case(mth)))
338-
//hr = protoutil.DefaultAPIOptions(replacer.Replace(string(file.Desc.Package())), service.GoName, m.GoName)
339-
340-
//proto.Walk(definition, proto.WithService(func(service *proto.Service) {
341-
// for _, element := range service.Elements {
342-
// rpc, ok := element.(*proto.RPC)
343-
// if !ok {
344-
// continue
345-
// }
346-
//
347-
// if len(rpc.Options) == 0 {
348-
// if err := InsertOption(rpc); err != nil {
349-
// }
350-
// fmt.Printf("Rpc %s Insert option.\n", rpc.Name)
351-
// return
352-
// }
353-
// }
354-
//}))
355-
356-
proto.Walk(definition, proto.WithRPC(func(rpc *proto.RPC) {
357-
if rpc.StreamsRequest || rpc.StreamsReturns {
358-
return
359-
}
360-
361-
var hasHttp bool
362-
for _, e := range rpc.Elements {
363-
var opt, ok = e.(*proto.Option)
340+
var rpcList []*proto.RPC
341+
proto.Walk(definition, proto.WithService(func(srv *proto.Service) {
342+
for _, e := range srv.Elements {
343+
var rpc, ok = e.(*proto.RPC)
364344
if !ok {
365345
continue
366346
}
367347

368-
if strings.Contains(opt.Name, "google.api.http") {
348+
rpcList = append(rpcList, rpc)
349+
}
350+
}))
351+
352+
var dataLine = strings.Split(data, "\n")
353+
for i := range rpcList {
354+
rpc := rpcList[i]
355+
insert := fmt.Sprintf(`
356+
rpc %s (%s) returns (%s) {
357+
option (google.api.http) = {
358+
post: "%s"
359+
body: "*"
360+
};`, rpc.Name, rpc.RequestType, rpc.ReturnsType, "/"+protoutil.Camel2Case(fmt.Sprintf("%s/%s/%s", protoutil.Camel2Case(pkg), protoutil.Camel2Case(rpc.Parent.(*proto.Service).Name), protoutil.Camel2Case(rpc.Name))))
361+
362+
var hasHttp bool
363+
for i := range rpc.Options {
364+
if rpc.Options[i].Name == "(google.api.http)" {
369365
hasHttp = true
370366
}
371367
}
372368

373-
if !hasHttp {
374-
panic(fmt.Errorf("method=>%s.%s path=>%s 请设置gateway url", pkg, rpc.Name, protoFile))
369+
// 如果option为0, 那么可以整体替换, 通过正则表达式
370+
if len(rpc.Options) == 0 || !hasHttp {
371+
_ = insert
372+
var rpcData = strings.Trim(dataLine[rpc.Position.Line-1], ";")
373+
// 以}结尾
374+
if rpcData[len(rpcData)-1] == '}' {
375+
dataLine[rpc.Position.Line-1] = insert + "\n}\n"
376+
} else {
377+
dataLine[rpc.Position.Line-1] = insert
378+
}
375379
}
376-
}))
377-
}
380+
}
378381

382+
data = strings.Join(dataLine, "\n")
383+
xerror.Panic(ioutil.WriteFile(protoFile, []byte(data), 0755))
384+
}
379385
protoList.Range(func(key, _ interface{}) bool {
380386
defer xerror.RespExit(key)
381-
382387
handler(key.(string))
383388
return true
384389
})

cmd/protoc-gen-lava/internal/lava.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/pubgo/lava/pkg/protoutil"
1212
"github.com/pubgo/lava/proto/lava"
13-
_ "google.golang.org/genproto/googleapis/api/annotations"
1413
)
1514

1615
var (
@@ -138,7 +137,7 @@ func genRpcInfo(gen *protogen.Plugin, file *protogen.File, g *protogen.Generated
138137
defaultUrl = true
139138
isDefault = true
140139

141-
var replacer = strings.NewReplacer(".","/","-","/")
140+
var replacer = strings.NewReplacer(".", "/", "-", "/")
142141
hr = protoutil.DefaultAPIOptions(replacer.Replace(string(file.Desc.Package())), service.GoName, m.GoName)
143142
}
144143
method, path := protoutil.ExtractHttpMethod(hr)

0 commit comments

Comments
 (0)