all: refactor extensions, add proto.GetExtension etc.
Change protoiface.ExtensionDescV1 to implement protoreflect.ExtensionType.
ExtensionDescV1's Name field conflicts with the Descriptor Name method,
so change the protoreflect.{Message,Enum,Extension}Type types to no
longer implement the corresponding Descriptor interface. This also leads
to a clearer distinction between the two types.
Introduce a protoreflect.ExtensionTypeDescriptor type which bridges
between ExtensionType and ExtensionDescriptor.
Add extension accessor functions to the proto package:
proto.{Has,Clear,Get,Set}Extension. These functions take a
protoreflect.ExtensionType parameter, which allows writing the
same function call using either the old or new API:
proto.GetExtension(message, somepb.E_ExtensionFoo)
Fixes golang/protobuf#908
Change-Id: Ibc65d12a46666297849114fd3aefbc4a597d9f08
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189199
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/proto/merge_test.go b/proto/merge_test.go
index 4fa161e..a0ec571 100644
--- a/proto/merge_test.go
+++ b/proto/merge_test.go
@@ -274,65 +274,41 @@
desc: "merge extension fields",
dst: func() proto.Message {
m := new(testpb.TestAllExtensions)
- m.ProtoReflect().Set(
- testpb.E_OptionalInt32Extension.Type,
- testpb.E_OptionalInt32Extension.Type.ValueOf(int32(32)),
- )
- m.ProtoReflect().Set(
- testpb.E_OptionalNestedMessageExtension.Type,
- testpb.E_OptionalNestedMessageExtension.Type.ValueOf(&testpb.TestAllTypes_NestedMessage{
+ proto.SetExtension(m, testpb.E_OptionalInt32Extension.Type, int32(32))
+ proto.SetExtension(m, testpb.E_OptionalNestedMessageExtension.Type,
+ &testpb.TestAllTypes_NestedMessage{
A: proto.Int32(50),
- }),
+ },
)
- m.ProtoReflect().Set(
- testpb.E_RepeatedFixed32Extension.Type,
- testpb.E_RepeatedFixed32Extension.Type.ValueOf(&[]uint32{1, 2, 3}),
- )
+ proto.SetExtension(m, testpb.E_RepeatedFixed32Extension.Type, &[]uint32{1, 2, 3})
return m
}(),
src: func() proto.Message {
m := new(testpb.TestAllExtensions)
- m.ProtoReflect().Set(
- testpb.E_OptionalInt64Extension.Type,
- testpb.E_OptionalInt64Extension.Type.ValueOf(int64(64)),
- )
- m.ProtoReflect().Set(
- testpb.E_OptionalNestedMessageExtension.Type,
- testpb.E_OptionalNestedMessageExtension.Type.ValueOf(&testpb.TestAllTypes_NestedMessage{
+ proto.SetExtension(m, testpb.E_OptionalInt64Extension.Type, int64(64))
+ proto.SetExtension(m, testpb.E_OptionalNestedMessageExtension.Type,
+ &testpb.TestAllTypes_NestedMessage{
Corecursive: &testpb.TestAllTypes{
OptionalInt64: proto.Int64(1000),
},
- }),
+ },
)
- m.ProtoReflect().Set(
- testpb.E_RepeatedFixed32Extension.Type,
- testpb.E_RepeatedFixed32Extension.Type.ValueOf(&[]uint32{4, 5, 6}),
- )
+ proto.SetExtension(m, testpb.E_RepeatedFixed32Extension.Type, &[]uint32{4, 5, 6})
return m
}(),
want: func() proto.Message {
m := new(testpb.TestAllExtensions)
- m.ProtoReflect().Set(
- testpb.E_OptionalInt32Extension.Type,
- testpb.E_OptionalInt32Extension.Type.ValueOf(int32(32)),
- )
- m.ProtoReflect().Set(
- testpb.E_OptionalInt64Extension.Type,
- testpb.E_OptionalInt64Extension.Type.ValueOf(int64(64)),
- )
- m.ProtoReflect().Set(
- testpb.E_OptionalNestedMessageExtension.Type,
- testpb.E_OptionalNestedMessageExtension.Type.ValueOf(&testpb.TestAllTypes_NestedMessage{
+ proto.SetExtension(m, testpb.E_OptionalInt32Extension.Type, int32(32))
+ proto.SetExtension(m, testpb.E_OptionalInt64Extension.Type, int64(64))
+ proto.SetExtension(m, testpb.E_OptionalNestedMessageExtension.Type,
+ &testpb.TestAllTypes_NestedMessage{
A: proto.Int32(50),
Corecursive: &testpb.TestAllTypes{
OptionalInt64: proto.Int64(1000),
},
- }),
+ },
)
- m.ProtoReflect().Set(
- testpb.E_RepeatedFixed32Extension.Type,
- testpb.E_RepeatedFixed32Extension.Type.ValueOf(&[]uint32{1, 2, 3, 4, 5, 6}),
- )
+ proto.SetExtension(m, testpb.E_RepeatedFixed32Extension.Type, &[]uint32{1, 2, 3, 4, 5, 6})
return m
}(),
}, {