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) { |
| 17 | m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool { |
| 18 | m.Clear(fd) |
| 19 | return true |
| 20 | }) |
| 21 | if m.GetUnknown() != nil { |
| 22 | m.SetUnknown(nil) |
| 23 | } |
| 24 | } |