hal

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2014 License: MIT Imports: 1 Imported by: 2

README

Hal

GoDoc

Go implementation of HAL standard.

This is a work in progress... Everything might/will change.

Usage

Gohal gives a way to translate structs into HAL format, which can be easily translated into Json or Xml.

It provides the interface Mapper which, when implemented by a struct, returns Json/Xml Marshaler-friendly structure:

type Mapper interface {
	GetMap() Entry
}

For a given Product struct:

type Product struct {
	Code int
	Name string
	Price int
}

Implementint the Mapper interface:

func (p Product) GetMap() hal.Entry {
	return hal.Entry{
		"name":  p.Name,
		"price": p.Price,
	}
}

This way you define which fields you want translated and which ones not (notice "Code" is not there).

Then you can just create a HAL Resource for a Product object by:

p := Product{
	Code: 1,
	Name: "Some Product",
	Price: 10
}

pr := hal.NewResource(p, "http://rest.api/products/1")

And when you need the Json encoded, you can do json.Marsal:

j, err := json.MarshalIndent(pr, "", "  ")
if err != nil {
	fmt.Printf("%s", err)
}

fmt.Printf("%s", j)

Output:

{
	"_links": {
		"self": {"href": "http://rest.api/products/1"}
	},
	"name": "Some product",
	"price": 10
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry map[string]interface{}
type Link LinkAttr
func NewLink(href string, attr LinkAttr) Link

NewLink returns a new Link object.

type LinkAttr

type LinkAttr map[string]string

Link types that store hyperlinks and its attributes.

type LinkCollection

type LinkCollection map[LinkRel]Link

type LinkRel

type LinkRel string

type Mapper

type Mapper interface {
	GetMap() Entry
}

Mapper is the interface implemented by the objects that can be converted into HAL format.

type Resource

type Resource struct {
	Payload  Mapper
	Links    LinkCollection
	Embedded []*Resource
}

Resource is a struct that stores a resource data. It represents a converted object in the HAL spec by containing all its fields and also a set of related links and a sub-set of recursively related resources.

func NewResource

func NewResource(p Mapper, selfUri string) *Resource

NewResource creates a Resource object for a given struct and its link.

func (r *Resource) AddLink(rel LinkRel, l Link)

AddLink appends a Link to the resource.

func (r *Resource) AddNewLink(rel LinkRel, href string)

AddNewLink appends a new Link object based on the rel and href params.

func (*Resource) Embed

func (r *Resource) Embed(er *Resource)

Embed appends a Resource to the array of embedded resources.

func (Resource) GetMap

func (r Resource) GetMap() Entry

Map implements the interface Mapper.

func (Resource) MarshalJSON

func (r Resource) MarshalJSON() ([]byte, error)

MarshalJSON is a Marshaler interface implementation for Resource struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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