encode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package prototext
  5. import (
  6. "fmt"
  7. "strconv"
  8. "unicode/utf8"
  9. "google.golang.org/protobuf/encoding/protowire"
  10. "google.golang.org/protobuf/internal/encoding/messageset"
  11. "google.golang.org/protobuf/internal/encoding/text"
  12. "google.golang.org/protobuf/internal/errors"
  13. "google.golang.org/protobuf/internal/flags"
  14. "google.golang.org/protobuf/internal/genid"
  15. "google.golang.org/protobuf/internal/order"
  16. "google.golang.org/protobuf/internal/pragma"
  17. "google.golang.org/protobuf/internal/strs"
  18. "google.golang.org/protobuf/proto"
  19. "google.golang.org/protobuf/reflect/protoreflect"
  20. "google.golang.org/protobuf/reflect/protoregistry"
  21. )
  22. const defaultIndent = " "
  23. // Format formats the message as a multiline string.
  24. // This function is only intended for human consumption and ignores errors.
  25. // Do not depend on the output being stable. Its output will change across
  26. // different builds of your program, even when using the same version of the
  27. // protobuf module.
  28. func Format(m proto.Message) string {
  29. return MarshalOptions{Multiline: true}.Format(m)
  30. }
  31. // Marshal writes the given [proto.Message] in textproto format using default
  32. // options. Do not depend on the output being stable. Its output will change
  33. // across different builds of your program, even when using the same version of
  34. // the protobuf module.
  35. func Marshal(m proto.Message) ([]byte, error) {
  36. return MarshalOptions{}.Marshal(m)
  37. }
  38. // MarshalOptions is a configurable text format marshaler.
  39. type MarshalOptions struct {
  40. pragma.NoUnkeyedLiterals
  41. // Multiline specifies whether the marshaler should format the output in
  42. // indented-form with every textual element on a new line.
  43. // If Indent is an empty string, then an arbitrary indent is chosen.
  44. Multiline bool
  45. // Indent specifies the set of indentation characters to use in a multiline
  46. // formatted output such that every entry is preceded by Indent and
  47. // terminated by a newline. If non-empty, then Multiline is treated as true.
  48. // Indent can only be composed of space or tab characters.
  49. Indent string
  50. // EmitASCII specifies whether to format strings and bytes as ASCII only
  51. // as opposed to using UTF-8 encoding when possible.
  52. EmitASCII bool
  53. // allowInvalidUTF8 specifies whether to permit the encoding of strings
  54. // with invalid UTF-8. This is unexported as it is intended to only
  55. // be specified by the Format method.
  56. allowInvalidUTF8 bool
  57. // AllowPartial allows messages that have missing required fields to marshal
  58. // without returning an error. If AllowPartial is false (the default),
  59. // Marshal will return error if there are any missing required fields.
  60. AllowPartial bool
  61. // EmitUnknown specifies whether to emit unknown fields in the output.
  62. // If specified, the unmarshaler may be unable to parse the output.
  63. // The default is to exclude unknown fields.
  64. EmitUnknown bool
  65. // Resolver is used for looking up types when expanding google.protobuf.Any
  66. // messages. If nil, this defaults to using protoregistry.GlobalTypes.
  67. Resolver interface {
  68. protoregistry.ExtensionTypeResolver
  69. protoregistry.MessageTypeResolver
  70. }
  71. }
  72. // Format formats the message as a string.
  73. // This method is only intended for human consumption and ignores errors.
  74. // Do not depend on the output being stable. Its output will change across
  75. // different builds of your program, even when using the same version of the
  76. // protobuf module.
  77. func (o MarshalOptions) Format(m proto.Message) string {
  78. if m == nil || !m.ProtoReflect().IsValid() {
  79. return "<nil>" // invalid syntax, but okay since this is for debugging
  80. }
  81. o.allowInvalidUTF8 = true
  82. o.AllowPartial = true
  83. o.EmitUnknown = true
  84. b, _ := o.Marshal(m)
  85. return string(b)
  86. }
  87. // Marshal writes the given [proto.Message] in textproto format using options in
  88. // MarshalOptions object. Do not depend on the output being stable. Its output
  89. // will change across different builds of your program, even when using the
  90. // same version of the protobuf module.
  91. func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
  92. return o.marshal(nil, m)
  93. }
  94. // MarshalAppend appends the textproto format encoding of m to b,
  95. // returning the result.
  96. func (o MarshalOptions) MarshalAppend(b []byte, m proto.Message) ([]byte, error) {
  97. return o.marshal(b, m)
  98. }
  99. // marshal is a centralized function that all marshal operations go through.
  100. // For profiling purposes, avoid changing the name of this function or
  101. // introducing other code paths for marshal that do not go through this.
  102. func (o MarshalOptions) marshal(b []byte, m proto.Message) ([]byte, error) {
  103. var delims = [2]byte{'{', '}'}
  104. if o.Multiline && o.Indent == "" {
  105. o.Indent = defaultIndent
  106. }
  107. if o.Resolver == nil {
  108. o.Resolver = protoregistry.GlobalTypes
  109. }
  110. internalEnc, err := text.NewEncoder(b, o.Indent, delims, o.EmitASCII)
  111. if err != nil {
  112. return nil, err
  113. }
  114. // Treat nil message interface as an empty message,
  115. // in which case there is nothing to output.
  116. if m == nil {
  117. return b, nil
  118. }
  119. enc := encoder{internalEnc, o}
  120. err = enc.marshalMessage(m.ProtoReflect(), false)
  121. if err != nil {
  122. return nil, err
  123. }
  124. out := enc.Bytes()
  125. if len(o.Indent) > 0 && len(out) > 0 {
  126. out = append(out, '\n')
  127. }
  128. if o.AllowPartial {
  129. return out, nil
  130. }
  131. return out, proto.CheckInitialized(m)
  132. }
  133. type encoder struct {
  134. *text.Encoder
  135. opts MarshalOptions
  136. }
  137. // marshalMessage marshals the given protoreflect.Message.
  138. func (e encoder) marshalMessage(m protoreflect.Message, inclDelims bool) error {
  139. messageDesc := m.Descriptor()
  140. if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
  141. return errors.New("no support for proto1 MessageSets")
  142. }
  143. if inclDelims {
  144. e.StartMessage()
  145. defer e.EndMessage()
  146. }
  147. // Handle Any expansion.
  148. if messageDesc.FullName() == genid.Any_message_fullname {
  149. if e.marshalAny(m) {
  150. return nil
  151. }
  152. // If unable to expand, continue on to marshal Any as a regular message.
  153. }
  154. // Marshal fields.
  155. var err error
  156. order.RangeFields(m, order.IndexNameFieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  157. if err = e.marshalField(fd.TextName(), v, fd); err != nil {
  158. return false
  159. }
  160. return true
  161. })
  162. if err != nil {
  163. return err
  164. }
  165. // Marshal unknown fields.
  166. if e.opts.EmitUnknown {
  167. e.marshalUnknown(m.GetUnknown())
  168. }
  169. return nil
  170. }
  171. // marshalField marshals the given field with protoreflect.Value.
  172. func (e encoder) marshalField(name string, val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
  173. switch {
  174. case fd.IsList():
  175. return e.marshalList(name, val.List(), fd)
  176. case fd.IsMap():
  177. return e.marshalMap(name, val.Map(), fd)
  178. default:
  179. e.WriteName(name)
  180. return e.marshalSingular(val, fd)
  181. }
  182. }
  183. // marshalSingular marshals the given non-repeated field value. This includes
  184. // all scalar types, enums, messages, and groups.
  185. func (e encoder) marshalSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
  186. kind := fd.Kind()
  187. switch kind {
  188. case protoreflect.BoolKind:
  189. e.WriteBool(val.Bool())
  190. case protoreflect.StringKind:
  191. s := val.String()
  192. if !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !utf8.ValidString(s) {
  193. return errors.InvalidUTF8(string(fd.FullName()))
  194. }
  195. e.WriteString(s)
  196. case protoreflect.Int32Kind, protoreflect.Int64Kind,
  197. protoreflect.Sint32Kind, protoreflect.Sint64Kind,
  198. protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
  199. e.WriteInt(val.Int())
  200. case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
  201. protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
  202. e.WriteUint(val.Uint())
  203. case protoreflect.FloatKind:
  204. // Encoder.WriteFloat handles the special numbers NaN and infinites.
  205. e.WriteFloat(val.Float(), 32)
  206. case protoreflect.DoubleKind:
  207. // Encoder.WriteFloat handles the special numbers NaN and infinites.
  208. e.WriteFloat(val.Float(), 64)
  209. case protoreflect.BytesKind:
  210. e.WriteString(string(val.Bytes()))
  211. case protoreflect.EnumKind:
  212. num := val.Enum()
  213. if desc := fd.Enum().Values().ByNumber(num); desc != nil {
  214. e.WriteLiteral(string(desc.Name()))
  215. } else {
  216. // Use numeric value if there is no enum description.
  217. e.WriteInt(int64(num))
  218. }
  219. case protoreflect.MessageKind, protoreflect.GroupKind:
  220. return e.marshalMessage(val.Message(), true)
  221. default:
  222. panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
  223. }
  224. return nil
  225. }
  226. // marshalList marshals the given protoreflect.List as multiple name-value fields.
  227. func (e encoder) marshalList(name string, list protoreflect.List, fd protoreflect.FieldDescriptor) error {
  228. size := list.Len()
  229. for i := 0; i < size; i++ {
  230. e.WriteName(name)
  231. if err := e.marshalSingular(list.Get(i), fd); err != nil {
  232. return err
  233. }
  234. }
  235. return nil
  236. }
  237. // marshalMap marshals the given protoreflect.Map as multiple name-value fields.
  238. func (e encoder) marshalMap(name string, mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error {
  239. var err error
  240. order.RangeEntries(mmap, order.GenericKeyOrder, func(key protoreflect.MapKey, val protoreflect.Value) bool {
  241. e.WriteName(name)
  242. e.StartMessage()
  243. defer e.EndMessage()
  244. e.WriteName(string(genid.MapEntry_Key_field_name))
  245. err = e.marshalSingular(key.Value(), fd.MapKey())
  246. if err != nil {
  247. return false
  248. }
  249. e.WriteName(string(genid.MapEntry_Value_field_name))
  250. err = e.marshalSingular(val, fd.MapValue())
  251. if err != nil {
  252. return false
  253. }
  254. return true
  255. })
  256. return err
  257. }
  258. // marshalUnknown parses the given []byte and marshals fields out.
  259. // This function assumes proper encoding in the given []byte.
  260. func (e encoder) marshalUnknown(b []byte) {
  261. const dec = 10
  262. const hex = 16
  263. for len(b) > 0 {
  264. num, wtype, n := protowire.ConsumeTag(b)
  265. b = b[n:]
  266. e.WriteName(strconv.FormatInt(int64(num), dec))
  267. switch wtype {
  268. case protowire.VarintType:
  269. var v uint64
  270. v, n = protowire.ConsumeVarint(b)
  271. e.WriteUint(v)
  272. case protowire.Fixed32Type:
  273. var v uint32
  274. v, n = protowire.ConsumeFixed32(b)
  275. e.WriteLiteral("0x" + strconv.FormatUint(uint64(v), hex))
  276. case protowire.Fixed64Type:
  277. var v uint64
  278. v, n = protowire.ConsumeFixed64(b)
  279. e.WriteLiteral("0x" + strconv.FormatUint(v, hex))
  280. case protowire.BytesType:
  281. var v []byte
  282. v, n = protowire.ConsumeBytes(b)
  283. e.WriteString(string(v))
  284. case protowire.StartGroupType:
  285. e.StartMessage()
  286. var v []byte
  287. v, n = protowire.ConsumeGroup(num, b)
  288. e.marshalUnknown(v)
  289. e.EndMessage()
  290. default:
  291. panic(fmt.Sprintf("prototext: error parsing unknown field wire type: %v", wtype))
  292. }
  293. b = b[n:]
  294. }
  295. }
  296. // marshalAny marshals the given google.protobuf.Any message in expanded form.
  297. // It returns true if it was able to marshal, else false.
  298. func (e encoder) marshalAny(any protoreflect.Message) bool {
  299. // Construct the embedded message.
  300. fds := any.Descriptor().Fields()
  301. fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
  302. typeURL := any.Get(fdType).String()
  303. mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
  304. if err != nil {
  305. return false
  306. }
  307. m := mt.New().Interface()
  308. // Unmarshal bytes into embedded message.
  309. fdValue := fds.ByNumber(genid.Any_Value_field_number)
  310. value := any.Get(fdValue)
  311. err = proto.UnmarshalOptions{
  312. AllowPartial: true,
  313. Resolver: e.opts.Resolver,
  314. }.Unmarshal(value.Bytes(), m)
  315. if err != nil {
  316. return false
  317. }
  318. // Get current encoder position. If marshaling fails, reset encoder output
  319. // back to this position.
  320. pos := e.Snapshot()
  321. // Field name is the proto field name enclosed in [].
  322. e.WriteName("[" + typeURL + "]")
  323. err = e.marshalMessage(m.ProtoReflect(), true)
  324. if err != nil {
  325. e.Reset(pos)
  326. return false
  327. }
  328. return true
  329. }