hypercat

package module
v0.0.0-...-c1a61a2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2016 License: MIT Imports: 3 Imported by: 0

README

Hypercat Go

This project contains a simple library for working with Hypercat from Go.

GoDoc wercker status

Hypercat

For more information about Hypercat please see: http://www.hypercat.io/standard.html

Usage

The library is only compatible with the upcoming Hypercat 3.0 release, so will not work properly with either Hypercat 1.1 or 2.0 documents.

To import the library use:

import "github.com/thingful/hypercat-go"

Which will import the library and make available a hypercat namespace to your application.

To create a new Hypercat catalogue use:

cat := hypercat.NewHypercat("Catalog Name")

This creates a new catalogue and sets the standard hasDescription rel to the given name.

we can then add metadata relations to it like this:

cat.AddRel(hypercat.SupportsSearchRel, hypercat.SimpleSearchVal)

The package defines rels and vals for the standard rels/vals contained within the Hypercat standard, but to add custom metadata you can simply do this:

cat.AddRel("uniqueID", "123abc")

To create a new resource item use:

item := hypercat.NewItem("/resource1", "Resource 1")

This sets the href of the item and the required hasDescription metadata rel. You can add more metadata like this:

item.AddRel(hypercat.ContentTypeRel, "application/json")
item.AddRel("hasUniqueId", "abc123")

This item can then be added to the catalogue like this:

cat.AddItem(item)

The cat item can then be marshalled to JSON using the standard encoding/json package:

bytes, err := json.Marshal(cat)

Similarly we can use Unmarshal to convert a supplied JSON document into a struct to work with:

cat := hypercat.Hypercat{}
err := json.Unmarshal(jsonBlob, &cat)

There is a convenience method that allows you to parse a Hypercat struct directly from any io.Reader, e.g.:

cat, err := hypercat.Parse(strings.NewReader(jsonString))

In addition to this the library provides some additional methods for manipulating and return metadata and items from catalogues, but for full details please see the full documentation.

License

See LICENSE

Documentation

Overview

Package hypercat provides a minimal library for working with Hypercat documents (see http://www.hypercat.io).

It is only compatible with the upcoming Hypercat 3.0 release, so will not work properly with either Hypercat 1.1 or 2.0 documents. The package is documented at:

http://godoc.org/github.com/thingful/hypercat-go

To import the library use:

import "github.com/thingful/hypercat-go"

Which will import the library and make available a `hypercat` namespace to your application.

To create a new Hypercat catalogue use:

cat := hypercat.NewHypercat("Catalog Name")

This creates a new catalogue and sets the standard `hasDescription` rel to the given name.

we can then add metadata relations to it like this:

cat.AddRel(hypercat.SupportsSearchRel, hypercat.SimpleSearchVal)

The package defines rels and vals for the standard rels/vals contained within the Hypercat standard, but to add custom metadata you can simply do this:

cat.AddRel("uniqueID", "123abc")

To create a new resource item use:

item := hypercat.NewItem("/resource1", "Resource 1")

This sets the `href` of the item and the required `hasDescription` metadata rel. You can add more metadata like this:

item.AddRel(hypercat.ContentTypeRel, "application/json")
item.AddRel("hasUniqueId", "abc123")

This item can then be added to the catalogue like this:

cat.AddItem(item)

The `cat` item can then be marshalled to JSON using the standard `encoding/json` package:

bytes, err := json.Marshal(cat)

Similarly we can use `Unmarshal` to convert a supplied JSON document into a struct to work with:

cat := hypercat.Hypercat{}
err := json.Unmarshal(jsonBytes, &cat)

There is a convenience method that allows you to parse a Hypercat struct directly from any io.Reader, e.g.:

cat, err := hypercat.Parse(strings.NewReader(jsonString))

Index

Constants

View Source
const (
	// HypercatVersion is the version of Hypercat this library currently supports
	HypercatVersion = "3.0"

	// HypercatMediaType is the default mime type of Hypercat resources
	HypercatMediaType = "application/vnd.hypercat.catalogue+json"

	// DescriptionRel is the URI for the hasDescription relationship
	DescriptionRel = "urn:X-hypercat:rels:hasDescription:en"

	// ContentTypeRel is the URI for the isContentType relationship
	ContentTypeRel = "urn:X-hypercat:rels:isContentType"

	// HomepageRel is the URI for hasHomepage relationship
	HomepageRel = "urn:X-hypercat:rels:hasHomepage"

	// ContainsContentTypeRel is the URI for the containsContentType relationship
	ContainsContentTypeRel = "urn:X-hypercat:rels:containsContentType"

	// SupportsSearchRel is the URI for the supportsSearch relationship
	SupportsSearchRel = "urn:X-hypercat:rels:supportsSearch"

	// SimpleSearchVal is the required value for catalogues that support Hypercat simple search.
	SimpleSearchVal = "urn:X­hypercat:search:simple"

	// GeoBoundSearchVal is the required value for catalogues that support geographic bounding box search
	GeoBoundSearchVal = "urn:X-hypercat:search:geobound"

	// LexicographicSearchVal is the required value for catalogues that support lexicographic searching
	LexicographicSearchVal = "urn:X-hypercat:search:lexrange"

	// MultiSearchVal is the required value for catalogues that support multi-search
	MultiSearchVal = "urn:X-hypercat:search:multi"

	// PrefixSearchVal is the required value for catalogues that support prefix search (formerly substring search)
	PrefixSearchVal = "urn:X-hypercat:search:prefix"

	// LongitudeRel is the standard URI indicating an WGS84 latitude relationship
	LongitudeRel = "http://www.w3.org/2003/01/geo/wgs84_pos#long"

	// LatitudeRel is the standard URI indicating an WGS84 longitude relationship
	LatitudeRel = "http://www.w3.org/2003/01/geo/wgs84_pos#lat"
)

Variables

View Source
var (
	// ErrDuplicateHref is returned when a user attempts to add an item with a
	// Href that already exists within the catalogue.
	ErrDuplicateHref = errors.New("An item with that href already exists within the catalogue")

	// ErrHrefNotFound is returned when the user attempts to replace an item
	// within a catalogue but no item with that href is defined
	ErrHrefNotFound = errors.New("An item with that href does not exist within the catalogue")

	// ErrMissingDescriptionRel is returned if we fail to find the required
	// description rel when unmarshalling from a JSON string.
	ErrMissingDescriptionRel = errors.New(`"` + DescriptionRel + `" is a mandatory metadata relation`)

	// ErrMissingContentTypeRel is returned if we fail to find the required
	// contenttype rel when unmarshalling from a JSON string
	ErrMissingContentTypeRel = errors.New(`"` + ContentTypeRel + `" is a mandatory metadata element`)

	// ErrMissingHref is returned if the href for an item is not defined when
	// unmarshalling from a JSON string
	ErrMissingHref = errors.New(`"href" is a mandatory attribute`)
)

Functions

This section is empty.

Types

type Hypercat

type Hypercat struct {
	Items       Items    `json:"items"`
	Metadata    Metadata `json:"catalogue-metadata"`
	Description string   `json:"-"` // Hypercat spec is fuzzy about whether there can be more than one description. We assume not.
	ContentType string   `json:"-"`
}

Hypercat is the representation of the Hypercat catalogue object, which is the parent element of each catalogue instance.

func NewHypercat

func NewHypercat(description string) *Hypercat

NewHypercat is a constructor function that creates and returns a Hypercat instance. Accepts the description as a parameter.

Initializes Metadata to an empty slice, and ContentType to the default Hypercat content type.

func Parse

func Parse(r io.Reader) (*Hypercat, error)

Parse is a function that takes as input an io.Reader instance, which must return a valid Hypercat document when read. This function then passes this reader to a JSON Decoder, which attempts to parse and instantiate a valid Hypercat struct.

func (*Hypercat) AddItem

func (h *Hypercat) AddItem(item *Item) error

AddItem is a function for adding an Item to a catalogue. Returns an error if we try to add an Item whose href is already defined within the catalogue.

func (*Hypercat) AddRel

func (h *Hypercat) AddRel(rel, val string)

AddRel is a function for adding a Rel object to a catalogue. This may result in duplicated Rel keys as this is permitted by the Hypercat spec. TODO: this code is duplicated in item

func (*Hypercat) MarshalJSON

func (h *Hypercat) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of a Hypercat. This function is the implementation of the Marshaler interface.

func (*Hypercat) Rels

func (h *Hypercat) Rels() []string

Rels returns a slice containing all the Rel values of catalogue's metadata.

func (*Hypercat) ReplaceItem

func (h *Hypercat) ReplaceItem(newItem *Item) error

ReplaceItem is a function for replacing an item within a catalogue. Returns an error if we try to replace an Item that isn't defined within the catalogue.

func (*Hypercat) ReplaceRel

func (h *Hypercat) ReplaceRel(rel, val string)

ReplaceRel is a function that attempts to replace the value of a specific Rel object if it is attached to this Catalogue. If the Rel key isn't found this will have no effect.

func (*Hypercat) UnmarshalJSON

func (h *Hypercat) UnmarshalJSON(b []byte) error

UnmarshalJSON is the required function for structs that implement the Unmarshaler interface.

func (*Hypercat) Vals

func (h *Hypercat) Vals(key string) []string

Vals returns a slice of all values that match the given rel value.

type Item

type Item struct {
	Href        string   `json:"href"`
	Metadata    Metadata `json:"item-metadata"`
	Description string   `json:"-"` // Spec is unclear about whether there can be more than one description. We assume not.
}

Item is the representation of the Hypercat item object, which is the main object stored within a catalogue instance.

func NewItem

func NewItem(href, description string) *Item

NewItem is a constructor function that creates and returns an Item instance.

func (*Item) AddRel

func (item *Item) AddRel(rel, val string)

AddRel is a function for adding a Rel object to an item. This may result in duplicated Rel keys as this is permitted by the Hypercat spec.

func (*Item) IsCatalogue

func (item *Item) IsCatalogue() bool

IsCatalogue returns true if the Item is a Hypercat catalogue, false otherwise.

func (*Item) MarshalJSON

func (item *Item) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of an Item. This function is the the required function for structs that implement the Marshaler interface.

func (*Item) Rels

func (item *Item) Rels() []string

Rels returns a slice containing all the Rel values of this item.

func (*Item) ReplaceRel

func (item *Item) ReplaceRel(rel, val string)

ReplaceRel is a function that attempts to replace the value of a specific Rel object if it is attached to this Item. If the Rel key isn't found this will have no effect.

func (*Item) UnmarshalJSON

func (item *Item) UnmarshalJSON(b []byte) error

UnmarshalJSON is the required function for structs that implement the Unmarshaler interface.

func (*Item) Vals

func (item *Item) Vals(key string) []string

Vals returns a slice of all values that match the given rel value.

type Items

type Items []Item

Items is a simple type alias for a slice of Item structs.

type Metadata

type Metadata []Rel

Metadata is a simple type alias for a slice of Rel structs.

type Rel

type Rel struct {
	Rel string `json:"rel"`
	Val string `json:"val"`
}

Rel is the representation of the Hypercat individual metadata object which is used to describe a relationship between an entity and some other entity or concept.

func NewRel

func NewRel(rel, val string) *Rel

NewRel is a constructor function that creates and returns a Rel instance.

Jump to

Keyboard shortcuts

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