forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cee3667
commit 33c6262
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |