-
Notifications
You must be signed in to change notification settings - Fork 13
/
reset.go
26 lines (23 loc) · 724 Bytes
/
reset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package csproto
import (
"fmt"
"google.golang.org/protobuf/proto"
)
// Reset clears all fields of m.
//
// If m is not a supported message type (either generated by google.golang.org/protobuf/cmd/protoc-gen-go
// or has a Reset() method) this function panics
func Reset(m any) {
// as of 2022-12-20, both Google (V1 and V2) and Gogo emit a Reset() method on generated types
if r, ok := m.(interface{ Reset() }); ok {
r.Reset()
return
}
// in case the above changes for Google V2 types, delegate to proto.Reset()
if MsgType(m) == MessageTypeGoogle {
proto.Reset(m.(proto.Message))
return
}
// anything else is unsupported
panic(fmt.Errorf("unsupported message type (%T) passed to csproto.Reset()", m))
}