cmd/protoc-gen-go: generate descriptor proto using v2 textpb
Changes:
* Modify protoc-gen-go to use a helper from internal/impl
to print the message as text.
* Add a helper function to internal/impl that calls v2 textpb.
* Modify encoding/textpb to avoid depending on descriptor proto,
which would cause an import cycle.
* Modify internal/fileinit to populate a pseudo-internal
method on MessageDescriptor to check whether the message should
use the message set wire format. We avoid adding this to the
main MessageDescriptor interface since message sets are a
deprecated proto1 feature that we should avoid promoting.
Change-Id: Ibaf79a563af695756f11ddc4db69b38e25a8f1a7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168439
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/internal/fileinit/desc.go b/internal/fileinit/desc.go
index dbfe377..6d3b1b4 100644
--- a/internal/fileinit/desc.go
+++ b/internal/fileinit/desc.go
@@ -292,6 +292,7 @@
new func() pref.Message
isMapEntry bool
+ isMessageSet bool
fields fieldDescs
oneofs oneofDescs
resvNames names
@@ -352,6 +353,12 @@
return md.lazy
}
+// IsMessageSet is a pseudo-internal API for checking whether a message
+// should serialize in the proto1 message format.
+func (md *messageDesc) IsMessageSet() bool {
+ return md.lazyInit().isMessageSet
+}
+
func (fd *fieldDesc) Options() pref.OptionsMessage {
return unmarshalOptions(ptype.X.FieldOptions(), fd.options)
}
diff --git a/internal/fileinit/desc_lazy.go b/internal/fileinit/desc_lazy.go
index 0ff208a..ffcad72 100644
--- a/internal/fileinit/desc_lazy.go
+++ b/internal/fileinit/desc_lazy.go
@@ -560,6 +560,8 @@
switch num {
case descfield.MessageOptions_MapEntry:
md.lazy.isMapEntry = wire.DecodeBool(v)
+ case descfield.MessageOptions_MessageSetWireFormat:
+ md.lazy.isMessageSet = wire.DecodeBool(v)
}
default:
m := wire.ConsumeFieldValue(num, typ, b)
diff --git a/internal/impl/export.go b/internal/impl/export.go
index 6e54939..12ce51b 100644
--- a/internal/impl/export.go
+++ b/internal/impl/export.go
@@ -7,6 +7,7 @@
import (
"strconv"
+ "github.com/golang/protobuf/v2/encoding/textpb"
ptype "github.com/golang/protobuf/v2/internal/prototype"
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
)
@@ -86,3 +87,10 @@
}
return legacyWrapper.ExtensionTypeOf(d, t)
}
+
+// MessageStringOf returns the message value as a string,
+// which is the message serialized in the protobuf text format.
+func (Export) MessageStringOf(m pref.ProtoMessage) string {
+ b, _ := textpb.MarshalOptions{Compact: true}.Marshal(m)
+ return string(b)
+}