urlvars

package module
v0.0.0-...-4934efd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2020 License: MIT Imports: 3 Imported by: 0

README

urlvars

Package urlvars implements utilities for parsing elements of an URL into named values.

Example

UrlVars
 template := https://www.example.com/:root/:sub/:file
 rawurl := https://www.example.com/users/vedran/.listfiles.sh?action=list#listing
 values, err := Path(template, rawurl)
 
 // values will be:
 // map[string]string{"root": "users", "sub": "vedran", "file": ".listfiles.sh"}
Expander
// 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

Documentation

Godoc

License

See included LICENSE.

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

func Expand(template, rawurl string) (string, error)

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

func Path(template, rawurl string) (map[string]string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL