Source File
discard.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 (
)
// 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 ( Message) {
if != nil {
discardUnknown(MessageReflect())
}
}
func ( protoreflect.Message) {
.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
})
// Discard unknown fields.
if len(.GetUnknown()) > 0 {
.SetUnknown(nil)
}
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)