go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/protowalk/result.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  	"fmt"
    19  
    20  	"go.chromium.org/luci/common/proto/reflectutil"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  )
    24  
    25  // ResultData is the data produced from the FieldProcessor.
    26  type ResultData struct {
    27  	// Message is a human-readable message from FieldProcessor.Process describing
    28  	// the nature of the violation or correction (depends on the FieldProcessor
    29  	// implementation).
    30  	Message string
    31  
    32  	// IsErr is a flag for if the processed result should be considered as an error.
    33  	IsErr bool
    34  }
    35  
    36  func (d ResultData) String() string {
    37  	return d.Message
    38  }
    39  
    40  // Result is a singluar outcome from a FieldProcessor.Process which acted on
    41  // a message field.
    42  type Result struct {
    43  	// Path is the path to the field where the FieldProcessor acted.
    44  	Path reflectutil.Path
    45  
    46  	Data ResultData
    47  }
    48  
    49  func (r Result) String() string {
    50  	return fmt.Sprintf("%s: %s", r.Path, r.Data)
    51  }
    52  
    53  func (r Result) Err() error {
    54  	if r.Data.IsErr {
    55  		return errors.New(r.String())
    56  	}
    57  	return nil
    58  }
    59  
    60  // Results holds a slice of Result structs for each FieldProcessor passed in.
    61  type Results [][]Result
    62  
    63  // Strings returns a flat list of strings for all processors.
    64  func (r Results) Strings() []string {
    65  	num := 0
    66  	for _, v := range r {
    67  		num += len(v)
    68  	}
    69  
    70  	if num == 0 {
    71  		return nil
    72  	}
    73  
    74  	ret := make([]string, 0, num)
    75  
    76  	for _, v := range r {
    77  		for _, rslt := range v {
    78  			ret = append(ret, rslt.String())
    79  		}
    80  	}
    81  
    82  	return ret
    83  }
    84  
    85  // Err returns the error for all processors.
    86  func (r Results) Err() error {
    87  	merr := make(errors.MultiError, 0)
    88  	for _, v := range r {
    89  		for _, rslt := range v {
    90  			merr.MaybeAdd(rslt.Err())
    91  		}
    92  	}
    93  	return merr.AsError()
    94  }