Documentation
¶
Overview ¶
Package urlvars implements utilities for parsing elements of an URL into named values.
Index ¶
Constants ¶
View Source
const (
// MatchAllTemplate is a template that matches all standard parts of an URL.
MatchAllTemplate = "{scheme}{userinfo}{hostname}{port}{path}{query}{fragment}"
)
Variables ¶
View Source
var ( // ErrURLVars is the base error of urlvars package. ErrURLVars = errorex.New("urlvars") // ErrParse is a parse error. ErrParse = ErrURLVars.Wrap("parse error") // ErrDupKey is returned when a duplicate key is specified. ErrDupKey = ErrURLVars.WrapFormat("duplicate key '%s'") // ErrInvalidTemplate is returned when an invalid template is specified. ErrInvalidTemplate = ErrURLVars.Wrap("invalid template") )
Functions ¶
func Expand ¶
Expand expands the rawurl according to template.
URL keys are enclosed in single curly braces. Non-paired braces, extra braces and unrecognized keys in braces are passed to output as-is.
Given an example url:
https://user:pass@www.example.com:80/users/vedran/file.ext?action=view&mode=quick#top
The following supported keys would return the following values:
scheme: scheme part of URL, "https://" userinfo: userinfo part of URL, "user:pass@" host: host part of URL, "www.example.com:80" hostname: host part of URL, "www.example.com" port: port part of URL, ":80" path: path part of URL, "/users/vedran/file.ext" query: query part of URL, "?action=view&mode=quick" fragment: query part of URL, "#top"
Example:
rawurl = https://user:pass@www.example.com:80/users/vedran/file.ext?action=view&mode=quick#top
template = {scheme}{userinfo}{hostname}{port}{path}{query}{fragment}
Expand(template, exampleurl)
Output: https://user:pass@www.example.com:80/users/vedran/file.ext?action=view&mode=quick#top
func Path ¶
Path extracts specific path elements into a map of named variables.
Path elements are marked as variables by prefixing an element with a colon.
Example:
template := https://www.example.com/:root/:sub/:file rawurl := https://www.example.com/users/vedran/.listfiles.sh?action=list#listing
returns a map with following values:
{"root": "users", "sub": "vedran", "file": ".listfiles.sh"}
Extra path elements not matched by template are ignored.
If an error occurs it is returned with a nil map.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.