You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

371 lines
11 KiB

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