...
1
2
3
4
5 package proto
6
7 import (
8 "fmt"
9
10 "google.golang.org/protobuf/reflect/protoreflect"
11 )
12
13
14
15
16 func Reset(m Message) {
17 if mr, ok := m.(interface{ Reset() }); ok && hasProtoMethods {
18 mr.Reset()
19 return
20 }
21 resetMessage(m.ProtoReflect())
22 }
23
24 func resetMessage(m protoreflect.Message) {
25 if !m.IsValid() {
26 panic(fmt.Sprintf("cannot reset invalid %v message", m.Descriptor().FullName()))
27 }
28
29
30 fds := m.Descriptor().Fields()
31 for i := 0; i < fds.Len(); i++ {
32 m.Clear(fds.Get(i))
33 }
34
35
36 m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
37 m.Clear(fd)
38 return true
39 })
40
41
42 m.SetUnknown(nil)
43 }
44
View as plain text