Documentation ¶
Overview ¶
Package jsonapi is simple JSON API implementation for GO. To read more about JSON API format please visit http://jsonapi.org/
Example:
import ( "fmt" "github.com/vtg/jsonapi" ) type Post struct { ID uint64 `jsonapi:"id,users"` Name string `jsonapi:"attr,name"` Age uint32 `jsonapi:"attr,age,string,readonly"` // this will be marshalled as string and will be ignored on unmarshal SelfLink string `jsonapi:"link,self"` Comments jsonapi.Links `jsonapi:"rellink,comments"` } // BeforeMarshalJSONAPI will be executed before marshalling func (p *Post) BeforeMarshalJSONAPI() error { p.SelfLink = fmt.Sprintf("/api/posts/%d", p.ID) p.Comments.Related = fmt.Sprintf("/api/posts/%d/comments", p.ID) return nil } // AfterUnmarshalJSONAPI will be executed after unmarshalling func (p *Post) AfterUnmarshalJSONAPI() error { v := jsonapi.Validator{} v.Present(p.Name, "name") return v.Verify() }
Index ¶
- Variables
- func Marshal(i interface{}) ([]byte, error)
- func MarshalWithScope(i interface{}, scope string) ([]byte, error)
- func Unmarshal(b []byte, i interface{}) error
- func UnmarshalWithScope(b []byte, i interface{}, scope string) error
- type AfterUnmarshaler
- type BeforeMarshaler
- type Change
- type Changes
- type Error
- type ErrorSource
- type Errors
- type Keymap
- type Keymaps
- type Links
- type Marshaler
- type MetaData
- type Query
- type Relation
- type Request
- type Response
- type Unmarshaler
- type Validator
- func (v *Validator) Float32(value float32, pointer string, min, max float32)
- func (v *Validator) Float64(value float64, pointer string, min, max float64)
- func (v *Validator) Format(value, pointer, reg string)
- func (v *Validator) Int(value int, pointer string, min, max int)
- func (v *Validator) Int64(value int64, pointer string, min, max int64)
- func (v *Validator) Int64Present(value int64, pointer string)
- func (v *Validator) Present(value, pointer string)
- func (v *Validator) StringLength(value, pointer string, min, max int)
- func (v *Validator) Uint64(value uint64, pointer string, min, max int)
- func (v *Validator) Uint64Present(value uint64, pointer string)
- func (v Validator) Verify() error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorRecordNotFound returns Error for record not found behaviour ErrorRecordNotFound = Error{ Status: "404", Title: "Record Not Found", Detail: "The record you are looking for does not exist", } // ErrorPageNotFound returns Error for page not found behaviour ErrorPageNotFound = Error{ Status: "404", Title: "Page Not Found", Detail: "The page you are looking for does not exist", } ErrorUnauthorized = Error{ Status: "401", Title: "Unauthorized Request", Detail: "You are forbidden from accessing this page", } )
Functions ¶
func MarshalWithScope ¶
MarshalWithScope item to json api format
func UnmarshalWithScope ¶
UnmarshalWithScope decoding json api compatible request
Types ¶
type AfterUnmarshaler ¶
type AfterUnmarshaler interface {
AfterUnmarshalJSONAPI() error
}
AfterUnmarshaler interface
type BeforeMarshaler ¶
type BeforeMarshaler interface {
BeforeMarshalJSONAPI() error
}
BeforeMarshaler interface
func (p *Post) BeforeMarshalJSONAPI() error { p.SelfLink = fmt.Sprintf("/api/posts/%d", p.ID) p.Comments.Related = fmt.Sprintf("/api/posts/%d/comments", p.ID) return nil }
type Changes ¶
type Changes []Change
Changes store
func UnmarshalWithChanges ¶
UnmarshalWithChanges decoding json api compatible request into structure and returning changes
func UnmarshalWithChangesWithScope ¶
UnmarshalWithChangesWithScope decoding json api compatible request into structure and returning changes
type Error ¶
type Error struct { Code string `json:"code,omitempty"` Status string `json:"status,omitempty"` Source *ErrorSource `json:"source,omitempty"` Title string `json:"title,omitempty"` Detail string `json:"detail,omitempty"` }
Error type
func ErrorBadRequest ¶
ErrorBadRequest creating Error for inprocessible entries
func ErrorForbidden ¶
ErrorForbidden creating Error for forbidden entries
func ErrorInternal ¶
ErrorInternal creating Error for internal error
func ErrorInvalidAttribute ¶
ErrorInvalidAttribute creating Error for invalid attributes
type Errors ¶
type Errors struct {
Errors []Error `json:"errors,omitempty"`
}
Errors type
func (Errors) StatusCode ¶
StatusCode returns first error status code or success
type Marshaler ¶
Marshaler interface example
func (p *Post) MarshalJSONAPI() ([]byte, error) { return []byte(`{"custom":"return"}`), nil }
type MetaData ¶
type MetaData struct { Total int `json:"total"` Limit int `json:"limit"` Offset int `json:"offset"` Data interface{} `json:"data,omitempty"` }
MetaData struct
type Query ¶
type Query struct { Limit int Offset int Format string Sort []string Filters Keymaps Queries Keymaps Include string Start *time.Time End *time.Time }
Query contains parsed url query params
func QueryParams ¶
QueryParams takes URL params and returns parsed params for jsonapi
func (*Query) DefaultSort ¶
DefaultSort set default sort column
type Relation ¶
type Relation struct { Links Links Data interface{} }
Relation structure
func (Relation) MarshalJSON ¶
MarshalJSON marshaller
type Request ¶
type Request struct { Data struct { ID json.Number `json:"id"` Type string `json:"type"` Attributes map[string]json.RawMessage `json:"attributes"` } `json:"data"` }
Request structure for unmarshaling
type Response ¶
type Response struct { Data interface{} `json:"data,omitempty"` Included interface{} `json:"included,omitempty"` Meta *MetaData `json:"meta,omitempty"` Scope string `json:"-"` Errors }
Response structure for json api response
func (*Response) MarshalJSON ¶
MarshalJSON marshaller
type Unmarshaler ¶
Unmarshaler interface
type Validator ¶
type Validator struct {
Errors
}
Validator structure
func (*Validator) Float32 ¶
Float32 validates int min, max. -1 for any
v.Float32(Float32Value, "number", -1, 11) // max 18
func (*Validator) Float64 ¶
Float64 validates int min, max. -1 for any
v.Float64(Float64Value, "number", -1, 11) // max 18
func (*Validator) Format ¶
Format validates string format with regex string
v.Format(StringValue,"ip address", `\A(\d{1,3}\.){3}\d{1,3}\z`)
func (*Validator) Int ¶
Int validates int min, max. -1 for any
v.Int(IntValue, "number", -1, 11) // max 18
func (*Validator) Int64 ¶
Int64 validates int min, max. -1 for any
v.Int64(Int64Value, "number", -1, 11) // max 18
func (*Validator) Int64Present ¶
Int64Present validates if value is not 0
v.Int64Present(Int64Value, "number")
func (*Validator) StringLength ¶
StringLength validates string min, max length. -1 for any
v.StringLength(SomeVariable, "password", 6, 18) // min 6, max 18
func (*Validator) Uint64 ¶
Uint64 validates int min, max. -1 for any
v.Uint64(Uint64Value, "number", -1, 11) // max 18
func (*Validator) Uint64Present ¶
Uint64Present validates if value is not 0
v.Uint64Present(Uint64Value, "number")