reset.go 821 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2019 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 proto
  5. import "google.golang.org/protobuf/reflect/protoreflect"
  6. // Reset clears every field in the message.
  7. func Reset(m Message) {
  8. // TODO: Document memory aliasing guarantees.
  9. if mr, ok := m.(interface{ Reset() }); ok && hasProtoMethods {
  10. mr.Reset()
  11. return
  12. }
  13. resetMessage(m.ProtoReflect())
  14. }
  15. func resetMessage(m protoreflect.Message) {
  16. // Clear all known fields.
  17. fds := m.Descriptor().Fields()
  18. for i := 0; i < fds.Len(); i++ {
  19. m.Clear(fds.Get(i))
  20. }
  21. // Clear extension fields.
  22. m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
  23. m.Clear(fd)
  24. return true
  25. })
  26. // Clear unknown fields.
  27. m.SetUnknown(nil)
  28. }