mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 13:07:07 +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>
64 lines
1.6 KiB
Go
64 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"
|
|
)
|
|
|
|
// SetDefaults sets unpopulated scalar fields to their default values.
|
|
// Fields within a oneof are not set even if they have a default value.
|
|
// SetDefaults is recursively called upon any populated message fields.
|
|
func SetDefaults(m Message) {
|
|
if m != nil {
|
|
setDefaults(MessageReflect(m))
|
|
}
|
|
}
|
|
|
|
func setDefaults(m protoreflect.Message) {
|
|
fds := m.Descriptor().Fields()
|
|
for i := 0; i < fds.Len(); i++ {
|
|
fd := fds.Get(i)
|
|
if !m.Has(fd) {
|
|
if fd.HasDefault() && fd.ContainingOneof() == nil {
|
|
v := fd.Default()
|
|
if fd.Kind() == protoreflect.BytesKind {
|
|
v = protoreflect.ValueOf(append([]byte(nil), v.Bytes()...)) // copy the default bytes
|
|
}
|
|
m.Set(fd, v)
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
|
|
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
|
|
switch {
|
|
// Handle singular message.
|
|
case fd.Cardinality() != protoreflect.Repeated:
|
|
if fd.Message() != nil {
|
|
setDefaults(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++ {
|
|
setDefaults(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 {
|
|
setDefaults(v.Message())
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|