range.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2020 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 protorange provides functionality to traverse a message value.
  5. package protorange
  6. import (
  7. "bytes"
  8. "errors"
  9. "google.golang.org/protobuf/internal/genid"
  10. "google.golang.org/protobuf/internal/order"
  11. "google.golang.org/protobuf/proto"
  12. "google.golang.org/protobuf/reflect/protopath"
  13. "google.golang.org/protobuf/reflect/protoreflect"
  14. "google.golang.org/protobuf/reflect/protoregistry"
  15. )
  16. var (
  17. // Break breaks traversal of children in the current value.
  18. // It has no effect when traversing values that are not composite types
  19. // (e.g., messages, lists, and maps).
  20. Break = errors.New("break traversal of children in current value")
  21. // Terminate terminates the entire range operation.
  22. // All necessary Pop operations continue to be called.
  23. Terminate = errors.New("terminate range operation")
  24. )
  25. // Range performs a depth-first traversal over reachable values in a message.
  26. //
  27. // See [Options.Range] for details.
  28. func Range(m protoreflect.Message, f func(protopath.Values) error) error {
  29. return Options{}.Range(m, f, nil)
  30. }
  31. // Options configures traversal of a message value tree.
  32. type Options struct {
  33. // Stable specifies whether to visit message fields and map entries
  34. // in a stable ordering. If false, then the ordering is undefined and
  35. // may be non-deterministic.
  36. //
  37. // Message fields are visited in ascending order by field number.
  38. // Map entries are visited in ascending order, where
  39. // boolean keys are ordered such that false sorts before true,
  40. // numeric keys are ordered based on the numeric value, and
  41. // string keys are lexicographically ordered by Unicode codepoints.
  42. Stable bool
  43. // Resolver is used for looking up types when expanding google.protobuf.Any
  44. // messages. If nil, this defaults to using protoregistry.GlobalTypes.
  45. // To prevent expansion of Any messages, pass an empty protoregistry.Types:
  46. //
  47. // Options{Resolver: (*protoregistry.Types)(nil)}
  48. //
  49. Resolver interface {
  50. protoregistry.ExtensionTypeResolver
  51. protoregistry.MessageTypeResolver
  52. }
  53. }
  54. // Range performs a depth-first traversal over reachable values in a message.
  55. // The first push and the last pop are to push/pop a [protopath.Root] step.
  56. // If push or pop return any non-nil error (other than [Break] or [Terminate]),
  57. // it terminates the traversal and is returned by Range.
  58. //
  59. // The rules for traversing a message is as follows:
  60. //
  61. // - For messages, iterate over every populated known and extension field.
  62. // Each field is preceded by a push of a [protopath.FieldAccess] step,
  63. // followed by recursive application of the rules on the field value,
  64. // and succeeded by a pop of that step.
  65. // If the message has unknown fields, then push an [protopath.UnknownAccess] step
  66. // followed immediately by pop of that step.
  67. //
  68. // - As an exception to the above rule, if the current message is a
  69. // google.protobuf.Any message, expand the underlying message (if resolvable).
  70. // The expanded message is preceded by a push of a [protopath.AnyExpand] step,
  71. // followed by recursive application of the rules on the underlying message,
  72. // and succeeded by a pop of that step. Mutations to the expanded message
  73. // are written back to the Any message when popping back out.
  74. //
  75. // - For lists, iterate over every element. Each element is preceded by a push
  76. // of a [protopath.ListIndex] step, followed by recursive application of the rules
  77. // on the list element, and succeeded by a pop of that step.
  78. //
  79. // - For maps, iterate over every entry. Each entry is preceded by a push
  80. // of a [protopath.MapIndex] step, followed by recursive application of the rules
  81. // on the map entry value, and succeeded by a pop of that step.
  82. //
  83. // Mutations should only be made to the last value, otherwise the effects on
  84. // traversal will be undefined. If the mutation is made to the last value
  85. // during to a push, then the effects of the mutation will affect traversal.
  86. // For example, if the last value is currently a message, and the push function
  87. // populates a few fields in that message, then the newly modified fields
  88. // will be traversed.
  89. //
  90. // The [protopath.Values] provided to push functions is only valid until the
  91. // corresponding pop call and the values provided to a pop call is only valid
  92. // for the duration of the pop call itself.
  93. func (o Options) Range(m protoreflect.Message, push, pop func(protopath.Values) error) error {
  94. var err error
  95. p := new(protopath.Values)
  96. if o.Resolver == nil {
  97. o.Resolver = protoregistry.GlobalTypes
  98. }
  99. pushStep(p, protopath.Root(m.Descriptor()), protoreflect.ValueOfMessage(m))
  100. if push != nil {
  101. err = amendError(err, push(*p))
  102. }
  103. if err == nil {
  104. err = o.rangeMessage(p, m, push, pop)
  105. }
  106. if pop != nil {
  107. err = amendError(err, pop(*p))
  108. }
  109. popStep(p)
  110. if err == Break || err == Terminate {
  111. err = nil
  112. }
  113. return err
  114. }
  115. func (o Options) rangeMessage(p *protopath.Values, m protoreflect.Message, push, pop func(protopath.Values) error) (err error) {
  116. if ok, err := o.rangeAnyMessage(p, m, push, pop); ok {
  117. return err
  118. }
  119. fieldOrder := order.AnyFieldOrder
  120. if o.Stable {
  121. fieldOrder = order.NumberFieldOrder
  122. }
  123. order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  124. pushStep(p, protopath.FieldAccess(fd), v)
  125. if push != nil {
  126. err = amendError(err, push(*p))
  127. }
  128. if err == nil {
  129. switch {
  130. case fd.IsMap():
  131. err = o.rangeMap(p, fd, v.Map(), push, pop)
  132. case fd.IsList():
  133. err = o.rangeList(p, fd, v.List(), push, pop)
  134. case fd.Message() != nil:
  135. err = o.rangeMessage(p, v.Message(), push, pop)
  136. }
  137. }
  138. if pop != nil {
  139. err = amendError(err, pop(*p))
  140. }
  141. popStep(p)
  142. return err == nil
  143. })
  144. if b := m.GetUnknown(); len(b) > 0 && err == nil {
  145. pushStep(p, protopath.UnknownAccess(), protoreflect.ValueOfBytes(b))
  146. if push != nil {
  147. err = amendError(err, push(*p))
  148. }
  149. if pop != nil {
  150. err = amendError(err, pop(*p))
  151. }
  152. popStep(p)
  153. }
  154. if err == Break {
  155. err = nil
  156. }
  157. return err
  158. }
  159. func (o Options) rangeAnyMessage(p *protopath.Values, m protoreflect.Message, push, pop func(protopath.Values) error) (ok bool, err error) {
  160. md := m.Descriptor()
  161. if md.FullName() != "google.protobuf.Any" {
  162. return false, nil
  163. }
  164. fds := md.Fields()
  165. url := m.Get(fds.ByNumber(genid.Any_TypeUrl_field_number)).String()
  166. val := m.Get(fds.ByNumber(genid.Any_Value_field_number)).Bytes()
  167. mt, errFind := o.Resolver.FindMessageByURL(url)
  168. if errFind != nil {
  169. return false, nil
  170. }
  171. // Unmarshal the raw encoded message value into a structured message value.
  172. m2 := mt.New()
  173. errUnmarshal := proto.UnmarshalOptions{
  174. Merge: true,
  175. AllowPartial: true,
  176. Resolver: o.Resolver,
  177. }.Unmarshal(val, m2.Interface())
  178. if errUnmarshal != nil {
  179. // If the the underlying message cannot be unmarshaled,
  180. // then just treat this as an normal message type.
  181. return false, nil
  182. }
  183. // Marshal Any before ranging to detect possible mutations.
  184. b1, errMarshal := proto.MarshalOptions{
  185. AllowPartial: true,
  186. Deterministic: true,
  187. }.Marshal(m2.Interface())
  188. if errMarshal != nil {
  189. return true, errMarshal
  190. }
  191. pushStep(p, protopath.AnyExpand(m2.Descriptor()), protoreflect.ValueOfMessage(m2))
  192. if push != nil {
  193. err = amendError(err, push(*p))
  194. }
  195. if err == nil {
  196. err = o.rangeMessage(p, m2, push, pop)
  197. }
  198. if pop != nil {
  199. err = amendError(err, pop(*p))
  200. }
  201. popStep(p)
  202. // Marshal Any after ranging to detect possible mutations.
  203. b2, errMarshal := proto.MarshalOptions{
  204. AllowPartial: true,
  205. Deterministic: true,
  206. }.Marshal(m2.Interface())
  207. if errMarshal != nil {
  208. return true, errMarshal
  209. }
  210. // Mutations detected, write the new sequence of bytes to the Any message.
  211. if !bytes.Equal(b1, b2) {
  212. m.Set(fds.ByNumber(genid.Any_Value_field_number), protoreflect.ValueOfBytes(b2))
  213. }
  214. if err == Break {
  215. err = nil
  216. }
  217. return true, err
  218. }
  219. func (o Options) rangeList(p *protopath.Values, fd protoreflect.FieldDescriptor, ls protoreflect.List, push, pop func(protopath.Values) error) (err error) {
  220. for i := 0; i < ls.Len() && err == nil; i++ {
  221. v := ls.Get(i)
  222. pushStep(p, protopath.ListIndex(i), v)
  223. if push != nil {
  224. err = amendError(err, push(*p))
  225. }
  226. if err == nil && fd.Message() != nil {
  227. err = o.rangeMessage(p, v.Message(), push, pop)
  228. }
  229. if pop != nil {
  230. err = amendError(err, pop(*p))
  231. }
  232. popStep(p)
  233. }
  234. if err == Break {
  235. err = nil
  236. }
  237. return err
  238. }
  239. func (o Options) rangeMap(p *protopath.Values, fd protoreflect.FieldDescriptor, ms protoreflect.Map, push, pop func(protopath.Values) error) (err error) {
  240. keyOrder := order.AnyKeyOrder
  241. if o.Stable {
  242. keyOrder = order.GenericKeyOrder
  243. }
  244. order.RangeEntries(ms, keyOrder, func(k protoreflect.MapKey, v protoreflect.Value) bool {
  245. pushStep(p, protopath.MapIndex(k), v)
  246. if push != nil {
  247. err = amendError(err, push(*p))
  248. }
  249. if err == nil && fd.MapValue().Message() != nil {
  250. err = o.rangeMessage(p, v.Message(), push, pop)
  251. }
  252. if pop != nil {
  253. err = amendError(err, pop(*p))
  254. }
  255. popStep(p)
  256. return err == nil
  257. })
  258. if err == Break {
  259. err = nil
  260. }
  261. return err
  262. }
  263. func pushStep(p *protopath.Values, s protopath.Step, v protoreflect.Value) {
  264. p.Path = append(p.Path, s)
  265. p.Values = append(p.Values, v)
  266. }
  267. func popStep(p *protopath.Values) {
  268. p.Path = p.Path[:len(p.Path)-1]
  269. p.Values = p.Values[:len(p.Values)-1]
  270. }
  271. // amendError amends the previous error with the current error if it is
  272. // considered more serious. The precedence order for errors is:
  273. //
  274. // nil < Break < Terminate < previous non-nil < current non-nil
  275. func amendError(prev, curr error) error {
  276. switch {
  277. case curr == nil:
  278. return prev
  279. case curr == Break && prev != nil:
  280. return prev
  281. case curr == Terminate && prev != nil && prev != Break:
  282. return prev
  283. default:
  284. return curr
  285. }
  286. }