github.com/bishtawi/migrate/v4@v4.8.11/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  //
    20  func NewMultiError(errs ...error) MultiError {
    21  	compactErrs := make([]error, 0)
    22  	for _, e := range errs {
    23  		if e != nil {
    24  			compactErrs = append(compactErrs, e)
    25  		}
    26  	}
    27  	return MultiError{compactErrs}
    28  }
    29  
    30  // Error implements error. Multiple errors are concatenated with 'and's.
    31  func (m MultiError) Error() string {
    32  	var strs = make([]string, 0)
    33  	for _, e := range m.Errs {
    34  		if len(e.Error()) > 0 {
    35  			strs = append(strs, e.Error())
    36  		}
    37  	}
    38  	return strings.Join(strs, " and ")
    39  }
    40  
    41  // suint safely converts int to uint
    42  // see https://goo.gl/wEcqof
    43  // see https://goo.gl/pai7Dr
    44  func suint(n int) uint {
    45  	if n < 0 {
    46  		panic(fmt.Sprintf("suint(%v) expects input >= 0", n))
    47  	}
    48  	return uint(n)
    49  }
    50  
    51  // FilterCustomQuery filters all query values starting with `x-`
    52  func FilterCustomQuery(u *nurl.URL) *nurl.URL {
    53  	ux := *u
    54  	vx := make(nurl.Values)
    55  	for k, v := range ux.Query() {
    56  		if len(k) <= 1 || (len(k) > 1 && k[0:2] != "x-") {
    57  			vx[k] = v
    58  		}
    59  	}
    60  	ux.RawQuery = vx.Encode()
    61  	return &ux
    62  }