Skip to content

Commit

Permalink
fix: barry 2024-11-29 22:08:06
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Nov 29, 2024
1 parent 3bd96b5 commit e76acaf
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 1,140 deletions.
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
57 changes: 19 additions & 38 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,25 @@ 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"))
genFile.Var().Op("_").Op("=").Qual(rpcMetaPkg, "Register").Call(
jen.Op("&").Qual(rpcMetaPkg, "RpcMeta").Values(
jen.Dict{
jen.Id("Input"): jen.New(jen.Id(meta.mth.Input.GoIdent.GoName)),
jen.Id("Output"): jen.New(jen.Id(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)
}
19 changes: 18 additions & 1 deletion cmds/protoc-gen-lava/internal/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
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

0 comments on commit e76acaf

Please sign in to comment.