Skip to content

Commit

Permalink
Feat/fix examples (#1129)
Browse files Browse the repository at this point in the history
remove github protobuf dependency
  • Loading branch information
longXboy authored Jun 30, 2021
1 parent 6dd3f7f commit 7530fad
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 79 deletions.
36 changes: 18 additions & 18 deletions api/metadata/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"io/ioutil"
"sync"

"github.com/golang/protobuf/proto"
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
descriptorpb "google.golang.org/protobuf/types/descriptorpb"
dpb "google.golang.org/protobuf/types/descriptorpb"
)

//go:generate protoc --proto_path=. --proto_path=../../third_party --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. --go-http_out=paths=source_relative:. metadata.proto
Expand All @@ -26,15 +26,15 @@ type Server struct {

srv *grpc.Server
lock sync.Mutex
services map[string]*descriptorpb.FileDescriptorSet
services map[string]*dpb.FileDescriptorSet
methods map[string][]string
}

// NewServer create server instance
func NewServer(srv *grpc.Server) *Server {
return &Server{
srv: srv,
services: make(map[string]*descriptorpb.FileDescriptorSet),
services: make(map[string]*dpb.FileDescriptorSet),
methods: make(map[string][]string),
}
}
Expand All @@ -45,13 +45,9 @@ func (s *Server) load() error {
}
if s.srv != nil {
for name, info := range s.srv.GetServiceInfo() {
fdenc, ok := parseMetadata(info.Metadata)
if !ok {
return fmt.Errorf("invalid service %s metadata", name)
}
fd, err := decodeFileDesc(fdenc)
fd, err := parseMetadata(info.Metadata)
if err != nil {
return err
return fmt.Errorf("invalid service %s metadata err:%v", name, err)
}
protoSet, err := allDependency(fd)
if err != nil {
Expand Down Expand Up @@ -130,16 +126,20 @@ func (s *Server) GetServiceDesc(ctx context.Context, in *GetServiceDescRequest)
// For SupportPackageIsVersion4, m is the name of the proto file, we
// call proto.FileDescriptor to get the byte slice.
// For SupportPackageIsVersion3, m is a byte slice itself.
func parseMetadata(meta interface{}) ([]byte, bool) {
func parseMetadata(meta interface{}) (*dpb.FileDescriptorProto, error) {
// Check if meta is the file name.
if fileNameForMeta, ok := meta.(string); ok {
return proto.FileDescriptor(fileNameForMeta), true
return fileDescriptorProto(fileNameForMeta)
}
// Check if meta is the byte slice.
if enc, ok := meta.([]byte); ok {
return enc, true
fd, err := decodeFileDesc(enc)
if err != nil {
return nil, err
}
return fd, nil
}
return nil, false
return nil, fmt.Errorf("proto not sumpport metadata: %v", meta)
}

// decodeFileDesc does decompression and unmarshalling on the given
Expand Down Expand Up @@ -187,10 +187,10 @@ func decompress(b []byte) ([]byte, error) {
}

func fileDescriptorProto(path string) (*dpb.FileDescriptorProto, error) {
fdenc := proto.FileDescriptor(path)
fdDep, err := decodeFileDesc(fdenc)
fd, err := protoregistry.GlobalFiles.FindFileByPath(path)
if err != nil {
return nil, err
}
return fdDep, nil
fdpb := protodesc.ToFileDescriptorProto(fd)
return fdpb, nil
}
4 changes: 3 additions & 1 deletion cmd/protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati
body = rule.Body
responseBody = rule.ResponseBody
md := buildMethodDesc(g, m, method, path)
if body == "*" {
if method == "GET" {
md.HasBody = false
} else if body == "*" {
md.HasBody = true
md.Body = ""
} else if body != "" {
Expand Down
10 changes: 5 additions & 5 deletions examples/blog/api/blog/v1/blog_http.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/go-errors/errors v1.4.0 // indirect
github.com/go-kratos/consul v0.1.0
github.com/go-kratos/etcd v0.1.0
github.com/go-kratos/kratos/v2 v2.0.0-rc1
github.com/go-kratos/kratos/v2 v2.0.0-rc6
github.com/go-kratos/nacos v0.1.0
github.com/go-kratos/prometheus v0.0.0-20210522055322-137e29e7cf47
github.com/go-playground/validator/v10 v10.6.1 // indirect
Expand Down Expand Up @@ -46,9 +46,9 @@ require (
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.18.1
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
google.golang.org/genproto v0.0.0-20210524171403-669157292da3
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
google.golang.org/genproto v0.0.0-20210629200056-84d6f6074151
google.golang.org/grpc v1.39.0
google.golang.org/protobuf v1.27.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
)
Expand Down
Loading

0 comments on commit 7530fad

Please sign in to comment.