reset.go 767 B

12345678910111213141516171819202122232425262728293031
  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. // TODO: Add fast-path for reset?
  10. resetMessage(m.ProtoReflect())
  11. }
  12. func resetMessage(m protoreflect.Message) {
  13. // Clear all known fields.
  14. fds := m.Descriptor().Fields()
  15. for i := 0; i < fds.Len(); i++ {
  16. m.Clear(fds.Get(i))
  17. }
  18. // Clear extension fields.
  19. m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
  20. m.Clear(fd)
  21. return true
  22. })
  23. // Clear unknown fields.
  24. m.SetUnknown(nil)
  25. }