resource

package
v0.0.0-...-9034458 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2016 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package resource rest-ish-ly provides game data similar to http://jsonapi.org.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dict

type Dict map[string]interface{}

Dict provides json-like map serialization.

type Document

type Document struct {
	// data can either be an Object or an array of Objects
	// (the lack of omitempty vs omitnil is super-annoying.)
	Data     interface{} `json:"data"`
	Errors   []Error     `json:"error,omitempty"`
	Meta     Dict        `json:"meta,omitempty"`
	Included []*Object   `json:"included,omitempty"`
	Links    Links       `json:"links,omitempty"`
}

Document provides json-data roughly following the description at http://jsonapi.org/format/#document-structure. DocumentBuilder provides an API for generating these structures properly.

type DocumentBuilder

type DocumentBuilder struct {
	// contains filtered or unexported fields
}

DocumentBuilder provides an api for creating json-api document structures.

func NewDocumentBuilder

func NewDocumentBuilder(doc *Document) DocumentBuilder

NewDocumentBuilder creates an api-object to hepl populate the passed document.

func (DocumentBuilder) AddError

func (build DocumentBuilder) AddError(err error) DocumentBuilder

AddError appends an error to the list of errors included by this document. NOTE: if ever needed, could return or take error builder to which the other bits of the jsonapi error structure could be added

func (DocumentBuilder) AddObject

func (build DocumentBuilder) AddObject(obj *Object)

func (DocumentBuilder) NewIncludes

func (build DocumentBuilder) NewIncludes() ObjectList

NewIncludes returns a builder which can add objects (or object identifiers) to a compound document.

func (DocumentBuilder) NewObject

func (build DocumentBuilder) NewObject(id, class string) *Object

NewObject sets the document data to the passed object identifier. Returns an object builder to populate data about the identified object.

func (DocumentBuilder) NewObjects

func (build DocumentBuilder) NewObjects() ObjectsBuilder

NewObjects returns a builder to add an array of objects ( or object identifiers ) to the document.

func (DocumentBuilder) SetIncluded

func (build DocumentBuilder) SetIncluded(objects ObjectList) DocumentBuilder

SetIncluded sets the document's compound/include data to the passed object list. ( To comply with jsonapi, objects in the list should be referenced by the primary object. )

func (build DocumentBuilder) SetLink(key string, link Link) DocumentBuilder

SetLink to add the named link to the document's list of links.

func (DocumentBuilder) SetMeta

func (build DocumentBuilder) SetMeta(key string, value interface{}) DocumentBuilder

SetMeta to add a key-value the document's metadata.

func (DocumentBuilder) Sets

func (build DocumentBuilder) Sets(objects ObjectList) DocumentBuilder

Sets the document data to an existing array of objects or object identifiers. Returns a builder to add objects to that array.

type Error

type Error struct {
	//Id string `json:"id,omitempty"`
	// status, code, title, detail,
	// links, source,
	Code string `json:"meta,omitempty"`
}

type IBuildObjects

type IBuildObjects interface {
	NewObject(id, class string) *Object
	AddObject(obj *Object)
}

DocumentBuilder, ObjectsBuilder, and ObjectList satisfy this interface, which allows for either one object or multiple objects to be added depending on the context.

type IResource

type IResource interface {
	// Return the named sub-resource
	Find(string) (IResource, bool)
	// Return the resource
	Query() Document
	// Post to the resource
	Post(io.Reader) (Document, error)
}

IResource interfaces with a rest-ish endpoint. See also, Wrapper, which provides a function-based adapter.

func FindResource

func FindResource(res IResource, path string) (ret IResource, err error)

FindResource expands the passed resource, using each element of the passed path in turn. Returns an error, PathError, describing the extent of the matched path.

type Link string
type Links map[string]Link

type MultiDocument

type MultiDocument struct {
	Data     []Object `json:"data"`
	Meta     Dict     `json:"meta,omitempty"`
	Included []Object `json:"included,omitempty"`
}

for deserializing

type Object

type Object struct {
	Id            string                  `json:"id,omitempty"`
	Class         string                  `json:"type,omitempty"`
	Attributes    Dict                    `json:"attributes,omitempty"`
	Relationships map[string]Relationship `json:"relationships,omitempty"`
	Meta          Dict                    `json:"meta,omitempty"`
}

func NewObject

func NewObject(id, class string) *Object

NewObject creation.

func (*Object) SetAttr

func (obj *Object) SetAttr(key string, value interface{}) *Object

SetAttr sets the attribute "key" to "value".

func (*Object) SetMeta

func (obj *Object) SetMeta(key string, value interface{}) *Object

SetMeta sets the metada "key" to "value".

func (*Object) SetRel

func (obj *Object) SetRel(key string, data interface{}) *Object

SetRel adds object relations. FUTURE: use a builder for metadata support, etc.

type ObjectBuilder

type ObjectBuilder struct {
	Object *Object
}

func (*ObjectBuilder) AddObject

func (o *ObjectBuilder) AddObject(obj *Object)

func (*ObjectBuilder) NewObject

func (o *ObjectBuilder) NewObject(id, class string) *Object

type ObjectDocument

type ObjectDocument struct {
	Data     Object   `json:"data"`
	Meta     Dict     `json:"meta,omitempty"`
	Included []Object `json:"included,omitempty"`
}

for deserializing

type ObjectList

type ObjectList struct {
	// contains filtered or unexported fields
}

By default the object list does not have storage. An empty list acts as a "null device".

func NewObjectList

func NewObjectList() ObjectList

Create a new object list with backing storage

func (ObjectList) AddObject

func (l ObjectList) AddObject(obj *Object)

func (ObjectList) NewObject

func (l ObjectList) NewObject(id, class string) *Object

Add an object identifier to the list of document objects. Returns a builder to turn the identifier into a full object.

func (ObjectList) Objects

func (l ObjectList) Objects() (ret []*Object)

Return the array of added objects, if any.

type ObjectsBuilder

type ObjectsBuilder struct {
	// contains filtered or unexported fields
}

func (ObjectsBuilder) AddObject

func (o ObjectsBuilder) AddObject(obj *Object)

func (ObjectsBuilder) NewObject

func (o ObjectsBuilder) NewObject(id, class string) *Object

Add an object identifier to the list of document objects. Return a builder to turn the identifier into a full object.

type PathError

type PathError struct {
	Parts       []string
	FailedIndex int
}

func (PathError) Error

func (err PathError) Error() string

type Relationship

type Relationship struct {
	// data can either be an Object or an array of Objects
	// (the lack of omitempty vs omitnil is super-annoying.)
	Data interface{} `json:"data,omitempty"`
	Meta Dict        `json:"meta,omitempty"`
}

type Wrapper

type Wrapper struct {
	Finds   func(string) (IResource, bool)
	Queries func(DocumentBuilder)
	Posts   func(io.Reader, DocumentBuilder) error
}

Turn one or more IResource compatible functions into a full interface implementation.

func (Wrapper) Find

func (w Wrapper) Find(child string) (ret IResource, okay bool)

func (Wrapper) Post

func (w Wrapper) Post(reader io.Reader) (ret Document, err error)

func (Wrapper) Query

func (w Wrapper) Query() (ret Document)

Jump to

Keyboard shortcuts

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