go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/protowalk/processor_deprecated.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package protowalk
    16  
    17  import (
    18  	"google.golang.org/protobuf/reflect/protoreflect"
    19  	"google.golang.org/protobuf/types/descriptorpb"
    20  )
    21  
    22  // DeprecatedProcessor will find, and produce a Result for every
    23  // deprecated field which is set. Deprecated fields are marked like:
    24  //
    25  //	type name = <tag> [deprecated = true];
    26  //
    27  // You may also optionally clear the data in deprecated fields.
    28  type DeprecatedProcessor struct {
    29  	// If true, clear the data out of all deprecated fields.
    30  	ClearField bool
    31  }
    32  
    33  var _ FieldProcessor = &DeprecatedProcessor{}
    34  
    35  // Process implements FieldProcessor.
    36  //
    37  // This will optionally clear (zero) the field in `msg` to avoid accidental
    38  // usage of deprecated fields
    39  func (d *DeprecatedProcessor) Process(field protoreflect.FieldDescriptor, msg protoreflect.Message) (data ResultData, applied bool) {
    40  	if d.ClearField {
    41  		msg.Clear(field)
    42  	}
    43  	return ResultData{Message: "deprecated"}, true
    44  }
    45  
    46  func init() {
    47  	RegisterFieldProcessor(&DeprecatedProcessor{}, func(field protoreflect.FieldDescriptor) ProcessAttr {
    48  		if fo := field.Options().(*descriptorpb.FieldOptions); fo != nil {
    49  			if fo.Deprecated != nil && *fo.Deprecated {
    50  				return ProcessIfSet
    51  			}
    52  		}
    53  		return ProcessNever
    54  	})
    55  }