github.com/morigs/migrate/v4@v4.15.2-0.20221123151732-2fdcfbe124f3/util.go (about)

     1  package migrate
     2  
     3  import (
     4  	"fmt"
     5  	nurl "net/url"
     6  	"strings"
     7  )
     8  
     9  // MultiError holds multiple errors.
    10  //
    11  // Deprecated: Use github.com/hashicorp/go-multierror instead
    12  type MultiError struct {
    13  	Errs []error
    14  }
    15  
    16  // NewMultiError returns an error type holding multiple errors.
    17  //
    18  // Deprecated: Use github.com/hashicorp/go-multierror instead
    19  func NewMultiError(errs ...error) MultiError {
    20  	compactErrs := make([]error, 0)
    21  	for _, e := range errs {
    22  		if e != nil {
    23  			compactErrs = append(compactErrs, e)
    24  		}
    25  	}
    26  	return MultiError{compactErrs}
    27  }
    28  
    29  // Error implements error. Multiple errors are concatenated with 'and's.
    30  func (m MultiError) Error() string {
    31  	var strs = make([]string, 0)
    32  	for _, e := range m.Errs {
    33  		if len(e.Error()) > 0 {
    34  			strs = append(strs, e.Error())
    35  		}
    36  	}
    37  	return strings.Join(strs, " and ")
    38  }
    39  
    40  // suint safely converts int to uint
    41  // see https://goo.gl/wEcqof
    42  // see https://goo.gl/pai7Dr
    43  func suint(n int) uint {
    44  	if n < 0 {
    45  		panic(fmt.Sprintf("suint(%v) expects input >= 0", n))
    46  	}
    47  	return uint(n)
    48  }
    49  
    50  // FilterCustomQuery filters all query values starting with `x-`
    51  func FilterCustomQuery(u *nurl.URL) *nurl.URL {
    52  	ux := *u
    53  	vx := make(nurl.Values)
    54  	for k, v := range ux.Query() {
    55  		if len(k) <= 1 || k[0:2] != "x-" {
    56  			vx[k] = v
    57  		}
    58  	}
    59  	ux.RawQuery = vx.Encode()
    60  	return &ux
    61  }