protobuf-go/proto/reset.go
Joe Tsai 93bccf763e all: scrub all TODOs
TODOs that we do not intend to address have been deleted.
Those that are blocking v2 release are marked with "blocks".

Change-Id: I7efa9e546d0637b562101d0edc7009893d762722
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/218878
Reviewed-by: Damien Neil <dneil@google.com>
2020-02-10 19:28:48 +00:00

35 lines
829 B
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style.
// license that can be found in the LICENSE file.
package proto
import "google.golang.org/protobuf/reflect/protoreflect"
// Reset clears every field in the message.
func Reset(m Message) {
// TODO(blocks): Document memory aliasing guarantees.
if mr, ok := m.(interface{ Reset() }); ok && hasProtoMethods {
mr.Reset()
return
}
resetMessage(m.ProtoReflect())
}
func resetMessage(m protoreflect.Message) {
// Clear all known fields.
fds := m.Descriptor().Fields()
for i := 0; i < fds.Len(); i++ {
m.Clear(fds.Get(i))
}
// Clear extension fields.
m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
m.Clear(fd)
return true
})
// Clear unknown fields.
m.SetUnknown(nil)
}