forked from Mirrors/jsonapi
commit
d0428f63eb
|
@ -12,10 +12,7 @@ import (
|
||||||
// http://jsonapi.org/format/#document-top-level
|
// http://jsonapi.org/format/#document-top-level
|
||||||
// and here: http://jsonapi.org/format/#error-objects.
|
// and here: http://jsonapi.org/format/#error-objects.
|
||||||
func MarshalErrors(w io.Writer, errorObjects []*ErrorObject) error {
|
func MarshalErrors(w io.Writer, errorObjects []*ErrorObject) error {
|
||||||
if err := json.NewEncoder(w).Encode(&ErrorsPayload{Errors: errorObjects}); err != nil {
|
return json.NewEncoder(w).Encode(&ErrorsPayload{Errors: errorObjects})
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorsPayload is a serializer struct for representing a valid JSON API errors payload.
|
// ErrorsPayload is a serializer struct for representing a valid JSON API errors payload.
|
||||||
|
|
19
response.go
19
response.go
|
@ -68,10 +68,7 @@ func MarshalPayload(w io.Writer, models interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
return json.NewEncoder(w).Encode(payload)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal does the same as MarshalPayload except it just returns the payload
|
// Marshal does the same as MarshalPayload except it just returns the payload
|
||||||
|
@ -128,10 +125,7 @@ func MarshalPayloadWithoutIncluded(w io.Writer, model interface{}) error {
|
||||||
}
|
}
|
||||||
payload.clearIncluded()
|
payload.clearIncluded()
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
return json.NewEncoder(w).Encode(payload)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// marshalOne does the same as MarshalOnePayload except it just returns the
|
// marshalOne does the same as MarshalOnePayload except it just returns the
|
||||||
|
@ -195,11 +189,7 @@ func MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error {
|
||||||
|
|
||||||
payload := &OnePayload{Data: rootNode}
|
payload := &OnePayload{Data: rootNode}
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
return json.NewEncoder(w).Encode(payload)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func visitModelNode(model interface{}, included *map[string]*Node,
|
func visitModelNode(model interface{}, included *map[string]*Node,
|
||||||
|
@ -280,6 +270,9 @@ func visitModelNode(model interface{}, included *map[string]*Node,
|
||||||
// We had a JSON float (numeric), but our field was not one of the
|
// We had a JSON float (numeric), but our field was not one of the
|
||||||
// allowed numeric types
|
// allowed numeric types
|
||||||
er = ErrBadJSONAPIID
|
er = ErrBadJSONAPIID
|
||||||
|
}
|
||||||
|
|
||||||
|
if er != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
runtime.go
26
runtime.go
|
@ -8,35 +8,58 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Event represents a lifecycle event in the marshaling or unmarshalling
|
||||||
|
// process.
|
||||||
type Event int
|
type Event int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// UnmarshalStart is the Event that is sent when deserialization of a payload
|
||||||
|
// begins.
|
||||||
UnmarshalStart Event = iota
|
UnmarshalStart Event = iota
|
||||||
|
|
||||||
|
// UnmarshalStop is the Event that is sent when deserialization of a payload
|
||||||
|
// ends.
|
||||||
UnmarshalStop
|
UnmarshalStop
|
||||||
|
|
||||||
|
// MarshalStart is the Event that is sent sent when serialization of a payload
|
||||||
|
// begins.
|
||||||
MarshalStart
|
MarshalStart
|
||||||
|
|
||||||
|
// MarshalStop is the Event that is sent sent when serialization of a payload
|
||||||
|
// ends.
|
||||||
MarshalStop
|
MarshalStop
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Runtime has the same methods as jsonapi package for serialization and
|
||||||
|
// deserialization but also has a ctx, a map[string]interface{} for storing
|
||||||
|
// state, designed for instrumenting serialization timings.
|
||||||
type Runtime struct {
|
type Runtime struct {
|
||||||
ctx map[string]interface{}
|
ctx map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Events is the func type that provides the callback for handling event timings.
|
||||||
type Events func(*Runtime, Event, string, time.Duration)
|
type Events func(*Runtime, Event, string, time.Duration)
|
||||||
|
|
||||||
|
// Instrumentation is a a global Events variable. This is the handler for all
|
||||||
|
// timing events.
|
||||||
var Instrumentation Events
|
var Instrumentation Events
|
||||||
|
|
||||||
|
// NewRuntime creates a Runtime for use in an application.
|
||||||
func NewRuntime() *Runtime { return &Runtime{make(map[string]interface{})} }
|
func NewRuntime() *Runtime { return &Runtime{make(map[string]interface{})} }
|
||||||
|
|
||||||
|
// WithValue adds custom state variables to the runtime context.
|
||||||
func (r *Runtime) WithValue(key string, value interface{}) *Runtime {
|
func (r *Runtime) WithValue(key string, value interface{}) *Runtime {
|
||||||
r.ctx[key] = value
|
r.ctx[key] = value
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value returns a state variable in the runtime context.
|
||||||
func (r *Runtime) Value(key string) interface{} {
|
func (r *Runtime) Value(key string) interface{} {
|
||||||
return r.ctx[key]
|
return r.ctx[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Instrument is deprecated.
|
||||||
func (r *Runtime) Instrument(key string) *Runtime {
|
func (r *Runtime) Instrument(key string) *Runtime {
|
||||||
return r.WithValue("instrument", key)
|
return r.WithValue("instrument", key)
|
||||||
}
|
}
|
||||||
|
@ -45,12 +68,14 @@ func (r *Runtime) shouldInstrument() bool {
|
||||||
return Instrumentation != nil
|
return Instrumentation != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalPayload has docs in request.go for UnmarshalPayload.
|
||||||
func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
|
func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
|
||||||
return r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
|
return r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
|
||||||
return UnmarshalPayload(reader, model)
|
return UnmarshalPayload(reader, model)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
|
||||||
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
|
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
|
||||||
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
|
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
|
||||||
elems, err = UnmarshalManyPayload(reader, kind)
|
elems, err = UnmarshalManyPayload(reader, kind)
|
||||||
|
@ -60,6 +85,7 @@ func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (ele
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalPayload has docs in response.go for MarshalPayload.
|
||||||
func (r *Runtime) MarshalPayload(w io.Writer, model interface{}) error {
|
func (r *Runtime) MarshalPayload(w io.Writer, model interface{}) error {
|
||||||
return r.instrumentCall(MarshalStart, MarshalStop, func() error {
|
return r.instrumentCall(MarshalStart, MarshalStop, func() error {
|
||||||
return MarshalPayload(w, model)
|
return MarshalPayload(w, model)
|
||||||
|
|
Loading…
Reference in New Issue