...

Source file src/github.com/ory/x/urlx/join.go

Documentation: github.com/ory/x/urlx

     1  package urlx
     2  
     3  import (
     4  	"net/url"
     5  	"path"
     6  
     7  	"github.com/ory/x/cmdx"
     8  )
     9  
    10  // MustJoin joins the paths of two URLs. Fatals if first is not a DSN.
    11  func MustJoin(first string, parts ...string) string {
    12  	u, err := url.Parse(first)
    13  	if err != nil {
    14  		cmdx.Fatalf("Unable to parse %s: %s", first, err)
    15  	}
    16  	return AppendPaths(u, parts...).String()
    17  }
    18  
    19  // AppendPaths appends the provided paths to the url.
    20  func AppendPaths(u *url.URL, paths ...string) (ep *url.URL) {
    21  	ep = Copy(u)
    22  	if len(paths) == 0 {
    23  		return ep
    24  	}
    25  
    26  	ep.Path = path.Join(append([]string{ep.Path}, paths...)...)
    27  
    28  	last := paths[len(paths)-1]
    29  	if last[len(last)-1] == '/' {
    30  		ep.Path = ep.Path + "/"
    31  	}
    32  
    33  	return ep
    34  }
    35  
    36  // SetQuery appends the provided url values to the DSN's query string.
    37  func SetQuery(u *url.URL, query url.Values) (ep *url.URL) {
    38  	ep = Copy(u)
    39  	q := ep.Query()
    40  
    41  	for k := range query {
    42  		q.Set(k, query.Get(k))
    43  	}
    44  
    45  	ep.RawQuery = q.Encode()
    46  	return ep
    47  }
    48  

View as plain text