Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 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 | |
| 5 | package proto |
| 6 | |
| 7 | import "google.golang.org/protobuf/reflect/protoreflect" |
| 8 | |
| 9 | // Reset clears every field in the message. |
| 10 | func Reset(m Message) { |
| 11 | // TODO: Document memory aliasing guarantees. |
| 12 | // TODO: Add fast-path for reset? |
| 13 | resetMessage(m.ProtoReflect()) |
| 14 | } |
| 15 | |
| 16 | func resetMessage(m protoreflect.Message) { |
Joe Tsai | ef75bec | 2019-06-20 00:03:14 -0700 | [diff] [blame] | 17 | // 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 Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 24 | m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool { |
| 25 | m.Clear(fd) |
| 26 | return true |
| 27 | }) |
Joe Tsai | ef75bec | 2019-06-20 00:03:14 -0700 | [diff] [blame] | 28 | |
| 29 | // Clear unknown fields. |
| 30 | m.SetUnknown(nil) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 31 | } |