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
- Variables
- type Hypercat
- func (h *Hypercat) AddItem(item *Item) error
- func (h *Hypercat) AddRel(rel, val string)
- func (h *Hypercat) MarshalJSON() ([]byte, error)
- func (h *Hypercat) Rels() []string
- func (h *Hypercat) ReplaceItem(newItem *Item) error
- func (h *Hypercat) ReplaceRel(rel, val string)
- func (h *Hypercat) UnmarshalJSON(b []byte) error
- func (h *Hypercat) Vals(key string) []string
- type Item
- type Items
- type Metadata
- type Rel
Constants ¶
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:Xhypercat: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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
MarshalJSON returns the JSON encoding of a Hypercat. This function is the implementation of the Marshaler interface.
func (*Hypercat) ReplaceItem ¶
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 ¶
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 ¶
UnmarshalJSON is the required function for structs that implement the Unmarshaler interface.
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 (*Item) AddRel ¶
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 ¶
IsCatalogue returns true if the Item is a Hypercat catalogue, false otherwise.
func (*Item) MarshalJSON ¶
MarshalJSON returns the JSON encoding of an Item. This function is the the required function for structs that implement the Marshaler interface.
func (*Item) ReplaceRel ¶
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 ¶
UnmarshalJSON is the required function for structs that implement the Unmarshaler interface.