Skip to content

Commit e76acaf

Browse files
committed
fix: barry 2024-11-29 22:08:06
1 parent 3bd96b5 commit e76acaf

File tree

20 files changed

+107
-1140
lines changed

20 files changed

+107
-1140
lines changed

cmds/app/cmd.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import (
2727
"github.com/pubgo/lava/internal/middlewares/middleware_accesslog"
2828
"github.com/pubgo/lava/internal/middlewares/middleware_metric"
2929
"github.com/pubgo/lava/pkg/cmdutil"
30-
"github.com/pubgo/lava/services/errorservice"
31-
"github.com/pubgo/lava/services/metadataservice"
3230

3331
_ "github.com/pubgo/lava/core/debug/debug"
3432
// debug
@@ -65,9 +63,6 @@ var defaultProviders = []any{
6563

6664
lifecycle.New,
6765
scheduler.New,
68-
69-
metadataservice.New,
70-
errorservice.New,
7166
}
7267

7368
func NewBuilder(opts ...dix.Option) *dix.Dix {

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

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ package internal
22

33
import (
44
"fmt"
5+
"reflect"
56
"strings"
67

78
"github.com/dave/jennifer/jen"
8-
"github.com/pubgo/funk/assert"
9+
"github.com/pubgo/lava/core/rpcmeta"
910
"github.com/pubgo/lava/pkg/proto/lavapbv1"
1011
"google.golang.org/protobuf/compiler/protogen"
11-
"google.golang.org/protobuf/encoding/protojson"
1212
"google.golang.org/protobuf/proto"
1313
)
1414

15-
const protoJsonNamePkg = "google.golang.org/protobuf/encoding/protojson"
16-
const assertNamePkg = "github.com/pubgo/funk/assert"
17-
const lavapbv1Pkg = "github.com/pubgo/lava/pkg/proto/lavapbv1"
15+
var rpcMetaPkg = reflect.TypeOf(rpcmeta.RpcMeta{}).PkgPath()
1816

1917
type rpcMetaInfo struct {
2018
srv *protogen.Service
@@ -28,7 +26,7 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) (g *protogen.Genera
2826
genFile := jen.NewFile(string(file.GoPackageName))
2927
genFile.HeaderComment("Code generated by protoc-gen-lava. DO NOT EDIT.")
3028
genFile.HeaderComment("versions:")
31-
genFile.HeaderComment(fmt.Sprintf(" - protoc-gen-lava %s", version))
29+
genFile.HeaderComment(fmt.Sprintf(" - protoc-gen-lava %s", Version))
3230
genFile.HeaderComment(fmt.Sprintf(" - protoc %s", protocVersion(gen)))
3331
if file.Proto.GetOptions().GetDeprecated() {
3432
genFile.HeaderComment(fmt.Sprintf("%s is a deprecated file.", file.Desc.Path()))
@@ -74,42 +72,25 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) (g *protogen.Genera
7472
srvInfo := meta.srv
7573
keyPrefix := strings.ReplaceAll(srvInfo.GoName, "InnerService", "")
7674
keyPrefix = strings.ReplaceAll(keyPrefix, "Inner", "") + "Service"
77-
keyName := fmt.Sprintf("%s%sAction", keyPrefix, meta.mth.GoName)
7875

79-
//func() T{return T}()
80-
genFile.Commentf("%s %s/%s", keyName, meta.srv.GoName, meta.mth.GoName)
81-
genFile.Commentf(strings.TrimSpace(meta.mth.Comments.Leading.String()))
82-
genFile.Var().
83-
Id(keyName).
84-
Op("=").
85-
Func().Params().Op("*").Qual(lavapbv1Pkg, "RpcMeta").
86-
BlockFunc(
87-
func(group *jen.Group) {
88-
group.Var().Id("p").Qual(lavapbv1Pkg, "RpcMeta")
89-
group.Var().Id("data").Op("=").Id(fmt.Sprintf("`%s`", assert.Exit1(protojson.Marshal(meta.meta))))
90-
group.Qual(assertNamePkg, "Exit").Call(
91-
jen.Qual(protoJsonNamePkg, "Unmarshal").Call(
92-
jen.Op("[]").Byte().Call(jen.Id("data")),
93-
jen.Id("&p"),
94-
),
95-
)
96-
group.Return(jen.Id("&p"))
76+
genFile.Var().Op("_").Op("=").Qual(rpcMetaPkg, "Register").Call(
77+
jen.Op("&").Qual(rpcMetaPkg, "RpcMeta").Values(
78+
jen.Dict{
79+
jen.Id("Input"): jen.New(jen.Id(meta.mth.Input.GoIdent.GoName)),
80+
jen.Id("Output"): jen.New(jen.Id(meta.mth.Output.GoIdent.GoName)),
81+
jen.Id("Name"): jen.Lit(meta.meta.Name),
82+
jen.Id("Method"): jen.Lit(fmt.Sprintf("%s/%s", meta.srv.Desc.FullName(), meta.mth.GoName)),
83+
jen.Id("Tags"): jen.Map(jen.String()).String().Values(
84+
jen.DictFunc(func(dict jen.Dict) {
85+
for k, v := range meta.meta.Tags {
86+
dict[jen.Lit(k)] = jen.Lit(v)
87+
}
88+
}),
89+
),
9790
},
98-
).Call()
91+
))
9992
}
10093

10194
g.P(genFile.GoString())
10295
return g
10396
}
104-
105-
func protocVersion(gen *protogen.Plugin) string {
106-
v := gen.Request.GetCompilerVersion()
107-
if v == nil {
108-
return "(unknown)"
109-
}
110-
var suffix string
111-
if s := v.GetSuffix(); s != "" {
112-
suffix = "-" + s
113-
}
114-
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
115-
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
package internal
22

3-
const version = "v0.0.1"
3+
import (
4+
"fmt"
5+
"google.golang.org/protobuf/compiler/protogen"
6+
)
7+
8+
const Version = "v0.0.2"
9+
10+
func protocVersion(gen *protogen.Plugin) string {
11+
v := gen.Request.GetCompilerVersion()
12+
if v == nil {
13+
return "(unknown)"
14+
}
15+
var suffix string
16+
if s := v.GetSuffix(); s != "" {
17+
suffix = "-" + s
18+
}
19+
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
20+
}

cmds/protoc-gen-lava/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
import (
44
"flag"
5-
5+
66
"github.com/pubgo/lava/cmds/protoc-gen-lava/internal"
77
"google.golang.org/protobuf/compiler/protogen"
88
"google.golang.org/protobuf/types/pluginpb"
99
)
1010

11+
var _ = flag.String("version", internal.Version, "version")
12+
1113
func main() {
1214
flag.Parse()
1315

core/rpcmeta/aaa.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package rpcmeta
2+
3+
import "google.golang.org/protobuf/proto"
4+
5+
type RpcMeta struct {
6+
Method string
7+
Name string
8+
Tags map[string]string
9+
Input proto.Message
10+
Output proto.Message
11+
}

core/rpcmeta/registry.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package rpcmeta
2+
3+
import (
4+
"github.com/pubgo/funk/assert"
5+
)
6+
7+
var rpcMetas = make(map[string]*RpcMeta)
8+
9+
func Register(meta *RpcMeta) error {
10+
assert.If(meta == nil, "rpc meta is nil")
11+
assert.If(meta.Name == "", "rpc meta name is empty")
12+
assert.If(meta.Method == "", "rpc meta method is nil")
13+
assert.If(rpcMetas[meta.Name] != nil, "rpc meta name already exists")
14+
assert.If(rpcMetas[meta.Method] != nil, "rpc meta method already exists")
15+
16+
rpcMetas[meta.Name] = meta
17+
rpcMetas[meta.Method] = meta
18+
return nil
19+
}
20+
21+
func Get(nameOrMethod string) *RpcMeta { return rpcMetas[nameOrMethod] }

pkg/proto/errcodepb/api.pb.go

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)