Source File
defaults.go
Belonging Package
github.com/golang/protobuf/proto
// 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 (
)
// 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 ( Message) {
if != nil {
setDefaults(MessageReflect())
}
}
func ( protoreflect.Message) {
:= .Descriptor().Fields()
for := 0; < .Len(); ++ {
:= .Get()
if !.Has() {
if .HasDefault() && .ContainingOneof() == nil {
:= .Default()
if .Kind() == protoreflect.BytesKind {
= protoreflect.ValueOf(append([]byte(nil), .Bytes()...)) // copy the default bytes
}
.Set(, )
}
continue
}
}
.Range(func( protoreflect.FieldDescriptor, protoreflect.Value) bool {
switch {
// Handle singular message.
case .Cardinality() != protoreflect.Repeated:
if .Message() != nil {
(.Get().Message())
}
// Handle list of messages.
case .IsList():
if .Message() != nil {
:= .Get().List()
for := 0; < .Len(); ++ {
(.Get().Message())
}
}
// Handle map of messages.
case .IsMap():
if .MapValue().Message() != nil {
:= .Get().Map()
.Range(func( protoreflect.MapKey, protoreflect.Value) bool {
(.Message())
return true
})
}
}
return true
})
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)