go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/prjpb/logreason.go (about) 1 // Copyright 2021 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 prjpb 16 17 import ( 18 "sort" 19 "strings" 20 ) 21 22 // SortAndDedupeLogReasons does what its name says without modifying input. 23 func SortAndDedupeLogReasons(in []LogReason) []LogReason { 24 var out []LogReason 25 if len(in) == 0 { 26 return out 27 } 28 out = append(out, in...) // copy 29 sort.Slice(out, func(i, j int) bool { return out[i] < out[j] }) 30 prevUniqIndex := 0 31 for _, o := range out { 32 if out[prevUniqIndex] == o { 33 continue 34 } 35 prevUniqIndex++ 36 out[prevUniqIndex] = o 37 } 38 return out[:prevUniqIndex+1] 39 } 40 41 // FormatLogReasons produces "[HUMAN, READABLE]" string for a list of statuses. 42 func FormatLogReasons(in []LogReason) string { 43 sb := strings.Builder{} 44 sb.WriteRune('[') 45 for i, r := range in { 46 if i > 0 { 47 sb.WriteString(", ") 48 } 49 sb.WriteString(r.String()) 50 } 51 sb.WriteRune(']') 52 return sb.String() 53 }