Documentation ¶
Overview ¶
Package hashuri parses Hash URIs.
And works seemlessly with the Go built-in packages: "database/sql", "fmt", "encoding/json".
Hash URI Examples ¶
Hash URIs often look like this:
hash://sha256/0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8 \__/ \____/ \______________________________________________________________/ | | | scheme algorithm hash
But, more generally, can look like this:
hash://sha256/0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8?apple=one&banana=۲&cherry=3#over-there \__/ \____/ \______________________________________________________________/ \_________________________/ \________/ | | | | | scheme algorithm hash query fragment
Go Example ¶
Basic usage of this package looks like this:
import "github.com/reiver/go-hashuri" var hashURI string = "hash://sha256/0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8" var parsedHashURI hashuri.Type if err := hashuri.Parse(&parsedHashURI, hashURI); nil != err { return err } fmt.Printf("Hash URI algorithm: %q \n", parsedHashURI.Algorithm) // Hash URI algorithm: "sha256" fmt.Printf("Hash URI hash: %q \n", parsedHashURI.Hash) // Hash URI hash: "0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8" fmt.Printf("Hash URI raw query: %q \n", parsedHashURI.RawQuery) // Hash URI raw query: "" fmt.Printf("Hash URI fragment: %q \n", parsedHashURI.Fragment) // Hash URI fragment: ""
See Also ¶
The Hash URI specification can be found at: https://github.com/hash-uri/hash-uri
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Type ¶
type Type struct { Algorithm string // hash function algorithm Hash string // hash, encoded as hexadecimal RawQuery string // encoded query values, without '?' Fragment string // fragment, without '#' }
A hashuri.Type represents a parsed Hash URI.
The general for represented is:
hash://sha256/9f86d081884c7d659a2feaa0?type=text/plain#top \__/ \____/ \______________________/ \_____________/ \_/ | | | | | Scheme Algorithm Hash RawQuery Fragment
Example (Parsequery) ¶
var hashURI string = "hash://sha256/0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8?apple=one&banana=۲&cherry=3#over-there" var res hashuri.Type if err := hashuri.Parse(&res, hashURI); nil != err { panic(err) return } query, err := url.ParseQuery(res.RawQuery) if nil != err { panic(err) return } fmt.Printf("Hash URI raw query: %q\n", res.RawQuery) for i, key := range []string{"apple", "banana", "cherry"} { value := query.Get(key) fmt.Printf("key-value pair #%d: %q -> %q\n", 1+i, key, value) }
Output: Hash URI raw query: "apple=one&banana=۲&cherry=3" key-value pair #1: "apple" -> "one" key-value pair #2: "banana" -> "۲" key-value pair #3: "cherry" -> "3"
func (Type) MarshalJSON ¶
MarshalJSON is to make it so hashuri.Type fits the encoding/json.Marshaler interface.
func (Type) String ¶
String reassembles the Hash URI into a valid Hash URI string. The general form of the result is:
hash://sha256/9f86d081884c7d659a2feaa0?type=text/plain#top \__/ \____/ \______________________/ \_____________/ \_/ | | | | | scheme algorithm hash query fragment
Example ¶
var res hashuri.Type res.Algorithm = "sha256" res.Hash = "0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8" res.RawQuery = "apple=one&banana=۲&cherry=3" res.Fragment = "over-there" fmt.Println(res.String())
Output: hash://sha256/0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8?apple=one&banana=۲&cherry=3#over-there
func (*Type) UnmarshalJSON ¶
UnmarshalJSON is to make it so hashuri.Type fits the encoding/json.Unmarshaler interface.