go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/protowalk/processor_output_only.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/genproto/googleapis/api/annotations" 19 "google.golang.org/protobuf/proto" 20 "google.golang.org/protobuf/reflect/protoreflect" 21 "google.golang.org/protobuf/types/descriptorpb" 22 ) 23 24 // OutputOnlyProcessor implements the recommended behavior of 25 // aip.dev/203#output-only, namely that fields marked like: 26 // 27 // type name = <tag> [(google.api.field_behavior) = OUTPUT_ONLY]; 28 // 29 // Should be cleared by the server (without raising an error) if 30 // a user request includes a value for them. 31 type OutputOnlyProcessor struct{} 32 33 var _ FieldProcessor = (*OutputOnlyProcessor)(nil) 34 35 // Process implements FieldProcessor. 36 // 37 // This will clear (zero) the field in `msg`. 38 func (OutputOnlyProcessor) Process(field protoreflect.FieldDescriptor, msg protoreflect.Message) (data ResultData, applied bool) { 39 msg.Clear(field) 40 return ResultData{Message: "cleared OUTPUT_ONLY field"}, true 41 } 42 43 func init() { 44 RegisterFieldProcessor(&OutputOnlyProcessor{}, func(field protoreflect.FieldDescriptor) ProcessAttr { 45 if fo := field.Options().(*descriptorpb.FieldOptions); fo != nil { 46 for _, fb := range proto.GetExtension(fo, annotations.E_FieldBehavior).([]annotations.FieldBehavior) { 47 if fb == annotations.FieldBehavior_OUTPUT_ONLY { 48 return ProcessIfSet 49 } 50 } 51 } 52 return ProcessNever 53 }) 54 }