Skip to content

Commit

Permalink
Merge 9ceecf9 into cee3667
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielerzinger committed Sep 25, 2018
2 parents cee3667 + 9ceecf9 commit c36e45f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app.go
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/topfreegames/pitaya/constants"
pcontext "github.com/topfreegames/pitaya/context"
"github.com/topfreegames/pitaya/defaultpipelines"
"github.com/topfreegames/pitaya/docgenerator"
"github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/internal/codec"
"github.com/topfreegames/pitaya/internal/message"
Expand Down Expand Up @@ -585,3 +586,8 @@ func AddGRPCInfoToMetadata(
metadata[constants.RegionKey] = region
return metadata
}

// Descriptor returns the protobuf message descriptor for a given message name
func Descriptor(protoName string) ([]byte, error) {
return docgenerator.ProtoDescriptors(protoName)
}
10 changes: 10 additions & 0 deletions app_test.go
Expand Up @@ -494,6 +494,16 @@ func TestExtractSpan(t *testing.T) {
assert.Equal(t, span.Context(), spanCtx)
}

func TestDescriptor(t *testing.T) {
bts, err := Descriptor("kick.proto")
assert.NoError(t, err)
assert.NotNil(t, bts)

bts, err = Descriptor("not_exists.proto")
assert.Nil(t, bts)
assert.EqualError(t, constants.ErrProtodescriptor, err.Error())
}

func TestDocumentation(t *testing.T) {
doc, err := Documentation(false)
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions constants/errors.go
Expand Up @@ -54,6 +54,7 @@ var (
ErrNonsenseRPC = errors.New("you are making a rpc that may be processed locally, either specify a different server type or specify a server id")
ErrNotifyOnRequest = errors.New("tried to notify a request route")
ErrOnCloseBackend = errors.New("onclose callbacks are not allowed on backend servers")
ErrProtodescriptor = errors.New("failed to get protobuf message descriptor")
ErrRPCClientNotInitialized = errors.New("RPC client is not running")
ErrRPCLocal = errors.New("RPC must be to a different server type")
ErrRPCServerNotInitialized = errors.New("RPC server is not running")
Expand Down
39 changes: 39 additions & 0 deletions docgenerator/descriptors.go
@@ -0,0 +1,39 @@
package docgenerator

import (
"reflect"
"strings"

"github.com/gogo/protobuf/proto"
"github.com/topfreegames/pitaya/constants"
)

// ProtoDescriptors returns the descriptor for a given message name or .proto file
func ProtoDescriptors(protoName string) ([]byte, error) {
if strings.HasSuffix(protoName, ".proto") {
descriptor := proto.FileDescriptor(protoName)
if descriptor == nil {
return nil, constants.ErrProtodescriptor
}
return descriptor, nil
}

protoReflectTypePointer := proto.MessageType(protoName)

if protoReflectTypePointer == nil {
return nil, constants.ErrProtodescriptor
}

protoReflectType := protoReflectTypePointer.Elem()
protoValue := reflect.New(protoReflectType)
descriptorMethod, ok := protoReflectTypePointer.MethodByName("Descriptor")

if !ok {
return nil, constants.ErrProtodescriptor
}

descriptorValue := descriptorMethod.Func.Call([]reflect.Value{protoValue})
protoDescriptor := descriptorValue[0].Bytes()

return protoDescriptor, nil
}
35 changes: 35 additions & 0 deletions docgenerator/descriptors_test.go
@@ -0,0 +1,35 @@
package docgenerator

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/constants"
_ "github.com/topfreegames/pitaya/protos"
)

func TestProtoDescriptors(t *testing.T) {
t.Parallel()
tables := []struct {
name string
messageName string
err error
}{
{"fail filename", "not_exists.proto", constants.ErrProtodescriptor},
{"success filename", "kick.proto", nil},
{"success message", "protos.Push", nil},
{"fail message", "protos.DoNotExist", constants.ErrProtodescriptor},
}
for _, table := range tables {
t.Run(table.name, func(t *testing.T) {
bts, err := ProtoDescriptors(table.messageName)
if table.err != nil {
assert.EqualError(t, table.err, err.Error())
assert.Nil(t, bts)
} else {
assert.NoError(t, err)
assert.NotNil(t, bts)
}
})
}
}

0 comments on commit c36e45f

Please sign in to comment.