reflect/protoreflect: add ExtensionType IsValid{Interface,Value} methods
Add a way to typecheck a Value or interface{} without converting it to
the other form. This permits implementations which store field values as
a Value (such as dynamicpb, or (soon) extensions in generated messages)
to validate inputs without an unnecessary conversion.
Fixes golang/protobuf#905
Change-Id: I1b78612b22ae832efbb55f81ae420871729e3a02
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/192457
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/extension.go b/internal/impl/extension.go
index eda6ab9..8192223 100644
--- a/internal/impl/extension.go
+++ b/internal/impl/extension.go
@@ -114,6 +114,12 @@
func (xi *ExtensionInfo) InterfaceOf(v pref.Value) interface{} {
return xi.lazyInit().GoValueOf(v).Interface()
}
+func (xi *ExtensionInfo) IsValidValue(v pref.Value) bool {
+ return xi.lazyInit().IsValidPB(v)
+}
+func (xi *ExtensionInfo) IsValidInterface(v interface{}) bool {
+ return xi.lazyInit().IsValidGo(reflect.ValueOf(v))
+}
func (xi *ExtensionInfo) GoType() reflect.Type {
xi.lazyInit()
return xi.goType