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 protoimport ()// 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 setDefaults( 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.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |