encode.go 12 KB

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