blob: 3d7f894362f5cd65f5b78db8d2ae14c68fcdd844 [file] [log] [blame]
David K. Bainbridgebd6b2882021-08-26 13:31:02 +00001// 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
5package proto
6
7import (
8 "fmt"
9
10 "google.golang.org/protobuf/reflect/protoreflect"
11)
12
13// Reset clears every field in the message.
14// The resulting message shares no observable memory with its previous state
15// other than the memory for the message itself.
16func Reset(m Message) {
17 if mr, ok := m.(interface{ Reset() }); ok && hasProtoMethods {
18 mr.Reset()
19 return
20 }
21 resetMessage(m.ProtoReflect())
22}
23
24func resetMessage(m protoreflect.Message) {
25 if !m.IsValid() {
26 panic(fmt.Sprintf("cannot reset invalid %v message", m.Descriptor().FullName()))
27 }
28
29 // Clear all known fields.
30 fds := m.Descriptor().Fields()
31 for i := 0; i < fds.Len(); i++ {
32 m.Clear(fds.Get(i))
33 }
34
35 // Clear extension fields.
36 m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
37 m.Clear(fd)
38 return true
39 })
40
41 // Clear unknown fields.
42 m.SetUnknown(nil)
43}