hashuri

package module
v0.0.0-...-3b9017c Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2018 License: MIT Imports: 6 Imported by: 0

README

go-hashuri

Package hashuri parses Hash URIs, for the Go programming language.

And works seemlessly with the Go built-in packages: "database/sql", "fmt", "encoding/json".

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-hashuri

GoDoc

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

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

func Parse

func Parse(dest *Type, value string) error

hashuri.Parse parses value into a hashuri.Type structure.

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

func (receiver Type) MarshalJSON() ([]byte, error)

MarshalJSON is to make it so hashuri.Type fits the encoding/json.Marshaler interface.

func (*Type) Scan

func (receiver *Type) Scan(src interface{}) error

Scan is to make it so hashuri.Type fits the database/sql.Scanner interface.

func (Type) String

func (receiver Type) String() 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

func (receiver *Type) UnmarshalJSON(src []byte) error

UnmarshalJSON is to make it so hashuri.Type fits the encoding/json.Unmarshaler interface.

func (Type) Value

func (receiver Type) Value() (driver.Value, error)

Value is to make it so hashuri.Type fits the database/sql/driver.Valuer interface.

Jump to

Keyboard shortcuts

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