Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rpc meta #55

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions cmds/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/pubgo/lava/internal/middlewares/middleware_accesslog"
"github.com/pubgo/lava/internal/middlewares/middleware_metric"
"github.com/pubgo/lava/pkg/cmdutil"
"github.com/pubgo/lava/services/errorservice"
"github.com/pubgo/lava/services/metadataservice"

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

lifecycle.New,
scheduler.New,

metadataservice.New,
errorservice.New,
}

func NewBuilder(opts ...dix.Option) *dix.Dix {
Expand Down
64 changes: 25 additions & 39 deletions cmds/protoc-gen-lava/internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package internal

import (
"fmt"
"reflect"
"strings"

"github.com/dave/jennifer/jen"
"github.com/pubgo/funk/assert"
"github.com/pubgo/lava/core/rpcmeta"
"github.com/pubgo/lava/pkg/proto/lavapbv1"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

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

type rpcMetaInfo struct {
srv *protogen.Service
Expand All @@ -28,7 +26,7 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) (g *protogen.Genera
genFile := jen.NewFile(string(file.GoPackageName))
genFile.HeaderComment("Code generated by protoc-gen-lava. DO NOT EDIT.")
genFile.HeaderComment("versions:")
genFile.HeaderComment(fmt.Sprintf(" - protoc-gen-lava %s", version))
genFile.HeaderComment(fmt.Sprintf(" - protoc-gen-lava %s", Version))
genFile.HeaderComment(fmt.Sprintf(" - protoc %s", protocVersion(gen)))
if file.Proto.GetOptions().GetDeprecated() {
genFile.HeaderComment(fmt.Sprintf("%s is a deprecated file.", file.Desc.Path()))
Expand Down Expand Up @@ -74,42 +72,30 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) (g *protogen.Genera
srvInfo := meta.srv
keyPrefix := strings.ReplaceAll(srvInfo.GoName, "InnerService", "")
keyPrefix = strings.ReplaceAll(keyPrefix, "Inner", "") + "Service"
keyName := fmt.Sprintf("%s%sAction", keyPrefix, meta.mth.GoName)

//func() T{return T}()
genFile.Commentf("%s %s/%s", keyName, meta.srv.GoName, meta.mth.GoName)
genFile.Commentf(strings.TrimSpace(meta.mth.Comments.Leading.String()))
genFile.Var().
Id(keyName).
Op("=").
Func().Params().Op("*").Qual(lavapbv1Pkg, "RpcMeta").
BlockFunc(
func(group *jen.Group) {
group.Var().Id("p").Qual(lavapbv1Pkg, "RpcMeta")
group.Var().Id("data").Op("=").Id(fmt.Sprintf("`%s`", assert.Exit1(protojson.Marshal(meta.meta))))
group.Qual(assertNamePkg, "Exit").Call(
jen.Qual(protoJsonNamePkg, "Unmarshal").Call(
jen.Op("[]").Byte().Call(jen.Id("data")),
jen.Id("&p"),
),
)
group.Return(jen.Id("&p"))

var pbPkg = ""
if file.GoImportPath != meta.mth.Input.GoIdent.GoImportPath {
pbPkg = string(meta.mth.Input.GoIdent.GoImportPath)
}

genFile.Var().Op("_").Op("=").Qual(rpcMetaPkg, "Register").Call(
jen.Op("&").Qual(rpcMetaPkg, "RpcMeta").Values(
jen.Dict{
jen.Id("Input"): jen.New(jen.Qual(pbPkg, meta.mth.Input.GoIdent.GoName)),
jen.Id("Output"): jen.New(jen.Qual(pbPkg, meta.mth.Output.GoIdent.GoName)),
jen.Id("Name"): jen.Lit(meta.meta.Name),
jen.Id("Method"): jen.Lit(fmt.Sprintf("%s/%s", meta.srv.Desc.FullName(), meta.mth.GoName)),
jen.Id("Tags"): jen.Map(jen.String()).String().Values(
jen.DictFunc(func(dict jen.Dict) {
for k, v := range meta.meta.Tags {
dict[jen.Lit(k)] = jen.Lit(v)
}
}),
),
},
).Call()
))
}

g.P(genFile.GoString())
return g
}

func protocVersion(gen *protogen.Plugin) string {
v := gen.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}
20 changes: 19 additions & 1 deletion cmds/protoc-gen-lava/internal/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package internal

const version = "v0.0.1"
import (
"fmt"

"google.golang.org/protobuf/compiler/protogen"
)

const Version = "v0.0.2"

func protocVersion(gen *protogen.Plugin) string {
v := gen.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}
4 changes: 3 additions & 1 deletion cmds/protoc-gen-lava/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"flag"

"github.com/pubgo/lava/cmds/protoc-gen-lava/internal"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
)

var _ = flag.String("version", internal.Version, "version")

func main() {
flag.Parse()

Expand Down
11 changes: 11 additions & 0 deletions core/rpcmeta/aaa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package rpcmeta

import "google.golang.org/protobuf/proto"

type RpcMeta struct {
Method string
Name string
Tags map[string]string
Input proto.Message
Output proto.Message
}
21 changes: 21 additions & 0 deletions core/rpcmeta/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package rpcmeta

import (
"github.com/pubgo/funk/assert"
)

var rpcMetas = make(map[string]*RpcMeta)

func Register(meta *RpcMeta) error {
assert.If(meta == nil, "rpc meta is nil")
assert.If(meta.Name == "", "rpc meta name is empty")
assert.If(meta.Method == "", "rpc meta method is nil")
assert.If(rpcMetas[meta.Name] != nil, "rpc meta name already exists")
assert.If(rpcMetas[meta.Method] != nil, "rpc meta method already exists")

rpcMetas[meta.Name] = meta
rpcMetas[meta.Method] = meta
return nil
}

func Get(nameOrMethod string) *RpcMeta { return rpcMetas[nameOrMethod] }
150 changes: 0 additions & 150 deletions pkg/proto/errcodepb/api.pb.go

This file was deleted.

Loading
Loading