mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
pin dependency github.com/beorn7/perks from v1.0.0 to v1.0.1 pin dependency github.com/golang/protobuf from v1.3.3 to v1.4.2 pin denpendency github.com/json-iterator/go from v1.1.8 to v1.1.9 pin dependency github.com/prometheus/common from v0.4.1 to v0.9.1 pin dependency github.com/prometheus/procfs from v0.0.5 to v0.0.11 pin dependency github.com/alecthomas/template from v0.0.0-20160405071501-a0175ee3bccc to v0.0.0-20190718012654-fb15b899a751 pin dependency github.com/alecthomas/units from v0.0.0-20151022065526-2efee857e7cf to v0.0.0-20190717042225-c3de453c63f4 pin dependency github.com/go-kit/kit from v0.8.0 to v0.9.0 pin dependency github.com/go-logfmt/logfmt from v0.3.0 to v0.4.0 Co-Authored-By: Jordan Liggitt <jordan@liggitt.net>
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
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"
|
|
)
|
|
|
|
// DiscardUnknown recursively discards all unknown fields from this message
|
|
// and all embedded messages.
|
|
//
|
|
// When unmarshaling a message with unrecognized fields, the tags and values
|
|
// of such fields are preserved in the Message. This allows a later call to
|
|
// marshal to be able to produce a message that continues to have those
|
|
// unrecognized fields. To avoid this, DiscardUnknown is used to
|
|
// explicitly clear the unknown fields after unmarshaling.
|
|
func DiscardUnknown(m Message) {
|
|
if m != nil {
|
|
discardUnknown(MessageReflect(m))
|
|
}
|
|
}
|
|
|
|
func discardUnknown(m protoreflect.Message) {
|
|
m.Range(func(fd protoreflect.FieldDescriptor, val protoreflect.Value) bool {
|
|
switch {
|
|
// Handle singular message.
|
|
case fd.Cardinality() != protoreflect.Repeated:
|
|
if fd.Message() != nil {
|
|
discardUnknown(m.Get(fd).Message())
|
|
}
|
|
// Handle list of messages.
|
|
case fd.IsList():
|
|
if fd.Message() != nil {
|
|
ls := m.Get(fd).List()
|
|
for i := 0; i < ls.Len(); i++ {
|
|
discardUnknown(ls.Get(i).Message())
|
|
}
|
|
}
|
|
// Handle map of messages.
|
|
case fd.IsMap():
|
|
if fd.MapValue().Message() != nil {
|
|
ms := m.Get(fd).Map()
|
|
ms.Range(func(_ protoreflect.MapKey, v protoreflect.Value) bool {
|
|
discardUnknown(v.Message())
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
|
|
// Discard unknown fields.
|
|
if len(m.GetUnknown()) > 0 {
|
|
m.SetUnknown(nil)
|
|
}
|
|
}
|