...

Source file src/github.com/ory/go-convenience/urlx/join.go

Documentation: github.com/ory/go-convenience/urlx

     1  package urlx
     2  
     3  import (
     4  	"net/url"
     5  	"path"
     6  )
     7  
     8  // AppendPaths appends the provided paths to the url.
     9  func AppendPaths(u *url.URL, paths ...string) (ep *url.URL) {
    10  	ep = Copy(u)
    11  	if len(paths) == 0 {
    12  		return ep
    13  	}
    14  
    15  	ep.Path = path.Join(append([]string{ep.Path}, paths...)...)
    16  
    17  	last := paths[len(paths)-1]
    18  	if last[len(last)-1] == '/' {
    19  		ep.Path = ep.Path + "/"
    20  	}
    21  
    22  	return ep
    23  }
    24  
    25  // SetQuery appends the provided url values to the URL's query string.
    26  func SetQuery(u *url.URL, query url.Values) (ep *url.URL) {
    27  	ep = Copy(u)
    28  	q := ep.Query()
    29  
    30  	for k := range query {
    31  		q.Set(k, query.Get(k))
    32  	}
    33  
    34  	ep.RawQuery = q.Encode()
    35  	return ep
    36  }
    37  

View as plain text