Documentation ¶
Index ¶
- Variables
- func Jsonify(s string) string
- func Marshal(data interface{}) ([]byte, error)
- func MarshalWithURLs(data interface{}, information ServerInformation) ([]byte, error)
- func Pluralize(word string) string
- func Unmarshal(data []byte, target interface{}) error
- type Data
- type DataContainer
- type Document
- type EditToManyRelations
- type EntityNamer
- type Links
- type MarshalIdentifier
- type MarshalIncludedRelations
- type MarshalLinkedRelations
- type MarshalReferences
- type Reference
- type ReferenceID
- type Relationship
- type RelationshipData
- type RelationshipDataContainer
- type ServerInformation
- type UnmarshalIdentifier
- type UnmarshalToManyRelations
- type UnmarshalToOneRelations
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInterface occurs if the target struct did not implement the UnmarshalIdentifier interface ErrInterface = errors.New("target must implement UnmarshalIdentifier interface") // ErrNoType occurs if the JSON payload did not have a type field ErrNoType = errors.New("invalid record, no type was specified") )
Functions ¶
func Marshal ¶
Marshal thats the input from `data` which can be a struct, a slice, or a pointer of it. Any struct in `data`or data itself, must at least implement the `MarshalIdentifier` interface. If so, it will generate a map[string]interface{} matching the jsonapi specification.
func MarshalWithURLs ¶
func MarshalWithURLs(data interface{}, information ServerInformation) ([]byte, error)
MarshalWithURLs can be used to include the generation of `related` and `self` links
Types ¶
type Data ¶
type Data struct { Type string `json:"type"` ID string `json:"id"` Attributes json.RawMessage `json:"attributes"` Relationships map[string]Relationship `json:"relationships,omitempty"` Links *Links `json:"links,omitempty"` }
Data for top level and included data
type DataContainer ¶
DataContainer is needed to either keep "data" contents as array or object.
func (*DataContainer) MarshalJSON ¶
func (c *DataContainer) MarshalJSON() ([]byte, error)
MarshalJSON either Marshals an array or object of data
func (*DataContainer) UnmarshalJSON ¶
func (c *DataContainer) UnmarshalJSON(payload []byte) error
UnmarshalJSON implements Unmarshaler because we have to detect if payload is an object or an array
type Document ¶
type Document struct { Links *Links `json:"links,omitempty"` Data *DataContainer `json:"data"` Included []Data `json:"included,omitempty"` Meta map[string]interface{} `json:"meta,omitempty"` }
Document represents a JSONAPI document like specified: http://jsonapi.org
func MarshalToStruct ¶
func MarshalToStruct(data interface{}, information ServerInformation) (Document, error)
MarshalToStruct marshals an api2go compatible struct into a jsonapi Document structure which then can be marshaled to JSON. You only need this method if you want to extract or extend parts of the document. You should directly use Marshal to get a []byte with JSON in it.
type EditToManyRelations ¶
type EditToManyRelations interface { AddToManyIDs(name string, IDs []string) error DeleteToManyIDs(name string, IDs []string) error }
The EditToManyRelations interface can be optionally implemented to add and delete to-many relationships on a already unmarshalled struct. These methods are used by our API for the to-many relationship update routes.
There are 3 HTTP Methods to edit to-many relations:
PATCH /v1/posts/1/comments Content-Type: application/vnd.api+json Accept: application/vnd.api+json { "data": [ { "type": "comments", "id": "2" }, { "type": "comments", "id": "3" } ] }
this replaces all of the comments that belong to post with ID 1 and the SetToManyReferenceIDs method will be called
POST /v1/posts/1/comments Content-Type: application/vnd.api+json Accept: application/vnd.api+json { "data": [ { "type": "comments", "id": "123" } ] }
adds a new comment to the post with ID 1. The AddToManyIDs methid will be called.
DELETE /v1/posts/1/comments Content-Type: application/vnd.api+json Accept: application/vnd.api+json { "data": [ { "type": "comments", "id": "12" }, { "type": "comments", "id": "13" } ] }
deletes comments that belong to post with ID 1. The DeleteToManyIDs method will be called.
type EntityNamer ¶
type EntityNamer interface {
GetName() string
}
The EntityNamer interface can be opionally implemented to rename a struct. The name returned by GetName will be used for the route generation as well as the "type" field in all responses
type Links ¶
type Links struct { Self string `json:"self,omitempty"` Related string `json:"related,omitempty"` First string `json:"first,omitempty"` Previous string `json:"prev,omitempty"` Next string `json:"next,omitempty"` Last string `json:"last,omitempty"` }
Links is general links struct for top level and relationships
type MarshalIdentifier ¶
type MarshalIdentifier interface {
GetID() string
}
MarshalIdentifier interface is necessary to give an element a unique ID. This interface must be implemented for marshal and unmarshal in order to let them store elements
type MarshalIncludedRelations ¶
type MarshalIncludedRelations interface { MarshalReferences MarshalIdentifier GetReferencedStructs() []MarshalIdentifier }
MarshalIncludedRelations must be implemented if referenced structs should be included
type MarshalLinkedRelations ¶
type MarshalLinkedRelations interface { MarshalReferences MarshalIdentifier GetReferencedIDs() []ReferenceID }
MarshalLinkedRelations must be implemented if there are references and the reference IDs should be included
type MarshalReferences ¶
type MarshalReferences interface {
GetReferences() []Reference
}
MarshalReferences must be implemented if the struct to be serialized has relations. This must be done because jsonapi needs information about relations even if many to many relations or many to one relations are empty
type Reference ¶
Reference information about possible references of a struct If IsNotLoaded is set to true, the `data` field will be omitted and only the `links` object will be generated. You should do this if there are some references, but you do not want to load them. Otherwise, if IsNotLoaded is false and GetReferencedIDs() returns no IDs for this reference name, an empty `data` field will be added which means that there are no references.
type ReferenceID ¶
ReferenceID contains all necessary information in order to reference another struct in jsonapi
type Relationship ¶
type Relationship struct { Links *Links `json:"links,omitempty"` Data *RelationshipDataContainer `json:"data,omitempty"` Meta map[string]interface{} `json:"meta,omitempty"` }
Relationship contains reference IDs to the related structs
type RelationshipData ¶
RelationshipData represents one specific reference ID
type RelationshipDataContainer ¶
type RelationshipDataContainer struct { DataObject *RelationshipData DataArray []RelationshipData }
RelationshipDataContainer is needed to either keep relationship "data" contents as array or object.
func (*RelationshipDataContainer) MarshalJSON ¶
func (c *RelationshipDataContainer) MarshalJSON() ([]byte, error)
MarshalJSON either Marshals an array or object of relationship data
func (*RelationshipDataContainer) UnmarshalJSON ¶
func (c *RelationshipDataContainer) UnmarshalJSON(payload []byte) error
UnmarshalJSON implements Unmarshaler and also detects array/object type
type ServerInformation ¶
ServerInformation can be passed to MarshalWithURLs to generate the `self` and `related` urls inside `links`
type UnmarshalIdentifier ¶
UnmarshalIdentifier interface to set ID when unmarshalling
type UnmarshalToManyRelations ¶
UnmarshalToManyRelations must be implemented to unmarshal to-many relations
type UnmarshalToOneRelations ¶
UnmarshalToOneRelations must be implemented to unmarshal to-one relations