github.com/pion/webrtc/v3@v3.2.24/internal/util/util.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 // Package util provides auxiliary functions internally used in webrtc package 5 package util 6 7 import ( 8 "errors" 9 "strings" 10 11 "github.com/pion/randutil" 12 ) 13 14 const ( 15 runesAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 16 ) 17 18 // Use global random generator to properly seed by crypto grade random. 19 var globalMathRandomGenerator = randutil.NewMathRandomGenerator() // nolint:gochecknoglobals 20 21 // MathRandAlpha generates a mathmatical random alphabet sequence of the requested length. 22 func MathRandAlpha(n int) string { 23 return globalMathRandomGenerator.GenerateString(n, runesAlpha) 24 } 25 26 // RandUint32 generates a mathmatical random uint32. 27 func RandUint32() uint32 { 28 return globalMathRandomGenerator.Uint32() 29 } 30 31 // FlattenErrs flattens multiple errors into one 32 func FlattenErrs(errs []error) error { 33 errs2 := []error{} 34 for _, e := range errs { 35 if e != nil { 36 errs2 = append(errs2, e) 37 } 38 } 39 if len(errs2) == 0 { 40 return nil 41 } 42 return multiError(errs2) 43 } 44 45 type multiError []error //nolint:errname 46 47 func (me multiError) Error() string { 48 var errstrings []string 49 50 for _, err := range me { 51 if err != nil { 52 errstrings = append(errstrings, err.Error()) 53 } 54 } 55 56 if len(errstrings) == 0 { 57 return "multiError must contain multiple error but is empty" 58 } 59 60 return strings.Join(errstrings, "\n") 61 } 62 63 func (me multiError) Is(err error) bool { 64 for _, e := range me { 65 if errors.Is(e, err) { 66 return true 67 } 68 if me2, ok := e.(multiError); ok { //nolint:errorlint 69 if me2.Is(err) { 70 return true 71 } 72 } 73 } 74 return false 75 }