Skip to content
Permalink
Browse files
refactor: sample add reflection
  • Loading branch information
PhilYue committed Dec 5, 2021
1 parent 22b1c4b commit 1d668955d2e75b5e96c7b1c130c5853c31519ff0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 69 deletions.
@@ -19,48 +19,12 @@ package grpcproxy

import (
"fmt"
"sync"
)

import (
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/dynamic"
)

type fileSource struct {
files map[string]*desc.FileDescriptor
er *dynamic.ExtensionRegistry
erInit sync.Once
}

// initFromFileDescriptor
// protoparse.ResolveFilenames(importPaths, fileNames...)
// rel: https://pkg.go.dev/github.com/jhump/protoreflect/desc/protoparse?utm_source=godoc#ResolveFilenames
func (f *Filter) initFromFileDescriptor(importPaths []string, fileNames ...string) error {
fileNames, err := protoparse.ResolveFilenames(importPaths, fileNames...)
if err != nil {
return err
}
p := protoparse.Parser{
ImportPaths: importPaths,
InferImportPaths: len(importPaths) == 0,
IncludeSourceCodeInfo: true,
}
fds, err := p.ParseFiles(fileNames...)
if err != nil {
return fmt.Errorf("could not parse given files: %v", err)
}

fsrc.files = make(map[string]*desc.FileDescriptor)
for _, fd := range fds {
name := fd.GetName()
fsrc.files[name] = fd
}

return nil
}

func DescriptorSourceFromFileDescriptors(files ...*desc.FileDescriptor) (*fileSource, error) {
fds := map[string]*desc.FileDescriptor{}
for _, fd := range files {
@@ -90,20 +54,6 @@ func addFile(fd *desc.FileDescriptor, fds map[string]*desc.FileDescriptor) error
return nil
}

func (fs *fileSource) ListServices() ([]string, error) {
set := map[string]bool{}
for _, fd := range fs.files {
for _, svc := range fd.GetServices() {
set[svc.GetFullyQualifiedName()] = true
}
}
sl := make([]string, 0, len(set))
for svc := range set {
sl = append(sl, svc)
}
return sl, nil
}

func (fs *fileSource) GetAllFiles() ([]*desc.FileDescriptor, error) {
files := make([]*desc.FileDescriptor, len(fs.files))
i := 0
@@ -113,22 +63,3 @@ func (fs *fileSource) GetAllFiles() ([]*desc.FileDescriptor, error) {
}
return files, nil
}

func (fs *fileSource) FindSymbol(fullyQualifiedName string) (desc.Descriptor, error) {
for _, fd := range fs.files {
if dsc := fd.FindSymbol(fullyQualifiedName); dsc != nil {
return dsc, nil
}
}
return nil, fmt.Errorf("could not found symbol %v", fullyQualifiedName)
}

func (fs *fileSource) AllExtensionsForType(typeName string) ([]*desc.FieldDescriptor, error) {
fs.erInit.Do(func() {
fs.er = &dynamic.ExtensionRegistry{}
for _, fd := range fs.files {
fs.er.AddExtensionsFromFile(fd)
}
})
return fs.er.AllExtensionsForType(typeName), nil
}
@@ -19,6 +19,7 @@ package main

import (
"context"
"google.golang.org/grpc/reflection"
"net"
)

@@ -71,7 +72,12 @@ func main() {
initUsers(s)

gs := grpc.NewServer()

proto.RegisterUserProviderServer(gs, s)

// registers the server reflection service on the given gRPC server.
reflection.Register(gs)

logger.Info("grpc test server is now running...")
err = gs.Serve(l)
if err != nil {

0 comments on commit 1d66895

Please sign in to comment.