Documentation ¶
Overview ¶
Package urlshaper creates a normalized shape (and other fields) from a URL.
Summary ¶
Given a URL or a URL Path (eg http://example.com/foo/bar?q=baz or just /foo/bar?q=baz) and an optional list of URL patterns, urlshaper will return an object that has that URL broken up in to its various components and provide a normalized shape of the URL.
Inputs ¶
URL inputs to the urlshaper should be strings. They can be either fully qualified URLs or just the path. (Anything that the net/url parser can parse should be fine.)
Valid URL inputs:
http://example.com/foo/bar https://example.com:8080/foo?q=bar /foo/bar/baz /foo?bar=baz
Patterns should describe only the path section of the URL. Variable portions of the URL should be identified by a preceeding the section name with a colon (":"). To match additional sections after the pattern, include a terminal asterisk ("*")
Valid patterns:
/about matches /about and /about?q=bar /about/:lang matches /about/en and /about/1234?q=bar /about/:lang/page matches /about/en/page and /about/1234/page?q=bar /about/* matches /about/foo/bar/baz and /about/a/b/c?q=bar
Output ¶
If there is no error, the returned Result objected always has URI, Path, and Shape filled in. The remaining fields will have zero values if the corresponding sections of the URL are missing.
Example ¶
prs := Parser{} // Add three sample patterns to our parser. // Patterns are always matched in list order; first match wins for _, pat := range []string{ "/about", "/about/:lang", "/about/:lang/page", } { prs.Patterns = append(prs.Patterns, &Pattern{Pat: pat}) } // Parse and generate the shape for a complex URL urlStr := "http://example.com:8080/about/english?title=Paradise&state=California" result, _ := prs.Parse(urlStr) fmt.Printf(`Original URL: %s URI: %s Path: %s Query: %s QueryFields: %+v PathFields: %+v Shape: %s PathShape: %s QueryShape: %s `, urlStr, result.URI, result.Path, result.Query, result.QueryFields, result.PathFields, result.Shape, result.PathShape, result.QueryShape)
Output: Original URL: http://example.com:8080/about/english?title=Paradise&state=California URI: http://example.com:8080/about/english?title=Paradise&state=California Path: /about/english Query: title=Paradise&state=California QueryFields: map[title:[Paradise] state:[California]] PathFields: map[lang:[english]] Shape: /about/:lang?state=?&title=? PathShape: /about/:lang QueryShape: state=?&title=?
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Parser ¶
type Parser struct {
Patterns []*Pattern
}
Parser contains a list of Patterns to use for parsing URLs, then exposes the functionality to generate a Result.
type Pattern ¶
type Pattern struct { Pat string // contains filtered or unexported fields }
Pattern is an object that represents a URL path pattern you wish to use when creating the shape. After adding a pattern to the Pattern object, you should call Compile on the pattern so it is suitable to use for matching.
If you don't call Compile, the pattern will be automatically compiled, but you won't see any errors that come up during compilation. If the compile fails, the pattern will be silently ignored.
Patterns should not include any query parameters - only the path portion of the URL is tested against the pattern.
type Result ¶
type Result struct { // URI is the original string that is parsed URI string // Path is the unmodified path portion of the URL Path string // Query is the unmodified query portion of the URL Query string // QueryFields is a map of the query parameters to values QueryFields url.Values // PathFields is a map of the keys in the provided pattern to the values // extracted from the Path PathFields url.Values // Shape is the normalized URL, with all variable portions replaced by '?' // and query parameters sorted alphabetically Shape string // PathShape is the path portion of the normalized URL PathShape string // QueryShape is the query portion of the normalized URL QueryShape string }
Result contains the parsed portions of the input URL