This commit is contained in:
yanweidong 2025-09-07 13:18:45 +08:00
commit b179e22f93
6 changed files with 224 additions and 0 deletions

13
go.mod Normal file
View File

@ -0,0 +1,13 @@
module git.apinb.com/bsm-tools/protoc-gen-test-http
go 1.24.6
require (
git.apinb.com/bsm-sdk/core v0.0.65
google.golang.org/protobuf v1.36.8
)
require (
github.com/google/uuid v1.6.0 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
)

13
go.sum Normal file
View File

@ -0,0 +1,13 @@
git.apinb.com/bsm-sdk/core v0.0.65 h1:mUgX3X6W1Lz4Keh5K6/XGVLc1EcY7AX2xBw6srKQT2g=
git.apinb.com/bsm-sdk/core v0.0.65/go.mod h1:FY2knuEVN7d7eHhpkyI+Cj/4oJ34gqZ8xzE7hB3JkOE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=

79
main.go Normal file
View File

@ -0,0 +1,79 @@
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"git.apinb.com/bsm-sdk/core/utils"
"google.golang.org/protobuf/compiler/protogen"
)
var (
flags flag.FlagSet
path = flags.String("path", "", "输出目录,默认为当前目录")
)
func main() {
opts := protogen.Options{
ParamFunc: func(name, value string) error {
//fmt.Println(name, value)
flags.Set(name, value)
return nil
},
}
opts.Run(func(gen *protogen.Plugin) error {
// set output directory
if *path == "" {
*path, _ = os.Getwd()
*path = filepath.Join(*path, "test/http")
}
if !utils.PathExists(*path) {
os.MkdirAll(*path, 0755)
}
for _, file := range gen.Files {
if len(file.Services) == 0 {
continue
}
if err := generateFiles(gen, file); err != nil {
return err
}
}
return nil
})
}
func generateFiles(gen *protogen.Plugin, file *protogen.File) error {
for _, service := range file.Services {
for _, method := range service.Methods {
sm := fmt.Sprintf("==> %s.%s", service.GoName, method.GoName)
// 生成测试文件
if err := generateHttpTestFile(gen, file, service, method); err != nil {
return err
}
fmt.Println(sm) // 打印服务和方法名
}
}
return nil
}
func generateHttpTestFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service, method *protogen.Method) error {
filename := fmt.Sprintf("%s_%s_test.json", service.GoName, method.GoName)
g := gen.NewGeneratedFile(filepath.Join(*path, filename), "")
g.P("{")
g.P(` "name": "` + method.GoName + `",`)
g.P(` "url": "http://localhost:8080/` + service.GoName + `/` + method.GoName + `",`)
g.P(` "method": "POST",`)
g.P(` "headers": {`)
g.P(` "Content-Type": "application/json"`)
g.P(` },`)
g.P(` "body": {`)
g.P(` }`)
g.P("}")
return nil
}

47
proto/check.proto Normal file
View File

@ -0,0 +1,47 @@
syntax = "proto3";
package initial;
option go_package = ".;initial";
// initial-
service Check{
// HELLO
rpc Hello(Crc) returns (StatusReply){}
//
rpc Updates(CheckForUpdatesRequest) returns (CheckForUpdatesReply) {}
}
message Crc{
string code = 1;
}
message StatusReply{
int64 status = 1; //
string identity=2; //
string message=3; //
int64 timeseq=4; //
}
message CheckForUpdatesRequest {
string app = 1; // <>
string os = 2; // <>
string arch = 3; // <>
string version = 4; // <>
}
message CheckForUpdatesReply {
string identity = 1; //
string version = 2; //
string summary = 3; //
string files = 4; // hash
string pubdate =5; //
}

70
proto/data.proto Normal file
View File

@ -0,0 +1,70 @@
syntax = "proto3";
package initial;
option go_package = ".;initial";
// initial-
service Data{
//
rpc Config(ConfigRequest) returns (ConfigReply) {}
// ,
rpc Areas(AreasRequest) returns (AreasReply) {}
//
rpc Tags(Empty) returns (TagsReply) {}
}
message Empty{
}
message AreasRequest {
bool show_town=1; //
}
message ConfigRequest {
string app = 1; // <>
string os = 2; // <>
}
message ConfigReply {
repeated ConfigItem data=1; //
}
message ConfigItem {
string Identity=1; //
string key=2; //
string value=3; //
int64 version=4; //
}
message AreasReply {
repeated AreasItem areas=1;
}
message AreasItem {
string id =1;
string pid=2;
int32 deep=3;
string name=4;
string pinyin_prefix=5;
string pinyin=6;
string ext_id=7;
string ext_name=8;
}
message TagsReply {
repeated TagsItem tags=1;
}
message TagsItem {
int64 id =1;
int32 type=2;
string key=3;
string title=4;
string remark=5;
string icon=6;
}

2
test.sh Normal file
View File

@ -0,0 +1,2 @@
go build -o C:\Users\david\go\bin\protoc-gen-test-http.exe main.go
protoc ./proto/*.proto --test-http_out=path=./test/http:./