...
1
2
3
4
5 package dynamicpb_test
6
7 import (
8 "testing"
9
10 "google.golang.org/protobuf/proto"
11 "google.golang.org/protobuf/reflect/protoreflect"
12 "google.golang.org/protobuf/reflect/protoregistry"
13 "google.golang.org/protobuf/testing/prototest"
14 "google.golang.org/protobuf/types/dynamicpb"
15
16 testpb "google.golang.org/protobuf/internal/testprotos/test"
17 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
18 )
19
20 func TestConformance(t *testing.T) {
21 for _, message := range []proto.Message{
22 (*testpb.TestAllTypes)(nil),
23 (*test3pb.TestAllTypes)(nil),
24 (*testpb.TestAllExtensions)(nil),
25 } {
26 mt := dynamicpb.NewMessageType(message.ProtoReflect().Descriptor())
27 prototest.Message{}.Test(t, mt)
28 }
29 }
30
31 func TestDynamicExtensions(t *testing.T) {
32 for _, message := range []proto.Message{
33 (*testpb.TestAllExtensions)(nil),
34 } {
35 mt := dynamicpb.NewMessageType(message.ProtoReflect().Descriptor())
36 prototest.Message{
37 Resolver: extResolver{},
38 }.Test(t, mt)
39 }
40 }
41
42 func TestDynamicEnums(t *testing.T) {
43 for _, enum := range []protoreflect.Enum{
44 testpb.TestAllTypes_FOO,
45 test3pb.TestAllTypes_FOO,
46 } {
47 et := dynamicpb.NewEnumType(enum.Descriptor())
48 prototest.Enum{}.Test(t, et)
49 }
50 }
51
52 type extResolver struct{}
53
54 func (extResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
55 xt, err := protoregistry.GlobalTypes.FindExtensionByName(field)
56 if err != nil {
57 return nil, err
58 }
59 return dynamicpb.NewExtensionType(xt.TypeDescriptor().Descriptor()), nil
60 }
61
62 func (extResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
63 xt, err := protoregistry.GlobalTypes.FindExtensionByNumber(message, field)
64 if err != nil {
65 return nil, err
66 }
67 return dynamicpb.NewExtensionType(xt.TypeDescriptor().Descriptor()), nil
68 }
69
70 func (extResolver) RangeExtensionsByMessage(message protoreflect.FullName, f func(protoreflect.ExtensionType) bool) {
71 protoregistry.GlobalTypes.RangeExtensionsByMessage(message, func(xt protoreflect.ExtensionType) bool {
72 return f(dynamicpb.NewExtensionType(xt.TypeDescriptor().Descriptor()))
73 })
74 }
75
View as plain text