blob: f14d9aed62dc8821875c8be4a6c433ee89fd62f0 [file] [log] [blame]
Joe Tsai378c1322019-04-25 23:48:08 -07001// 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 "google.golang.org/protobuf/reflect/protoreflect"
8
9// Reset clears every field in the message.
10func Reset(m Message) {
11 // TODO: Document memory aliasing guarantees.
12 // TODO: Add fast-path for reset?
13 resetMessage(m.ProtoReflect())
14}
15
16func resetMessage(m protoreflect.Message) {
Joe Tsaief75bec2019-06-20 00:03:14 -070017 // Clear all known fields.
18 fds := m.Descriptor().Fields()
19 for i := 0; i < fds.Len(); i++ {
20 m.Clear(fds.Get(i))
21 }
22
23 // Clear extension fields.
Joe Tsai378c1322019-04-25 23:48:08 -070024 m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
25 m.Clear(fd)
26 return true
27 })
Joe Tsaief75bec2019-06-20 00:03:14 -070028
29 // Clear unknown fields.
30 m.SetUnknown(nil)
Joe Tsai378c1322019-04-25 23:48:08 -070031}