Exemplo n.º 1
0
// MarshalWithCodec marshals the specified object with the specified codec and options.
// If the object implements the Facade interface, the PublicData object should be
// marshalled instead.
func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options map[string]interface{}) ([]byte, error) {

	// make sure we have at least one codec
	s.assertCodecs()

	// get the public data
	publicData, err := codecs.PublicData(object, options)

	// if there was an error - return it
	if err != nil {
		return nil, err
	}

	// let the codec do its work
	return codec.Marshal(publicData, options)
}
Exemplo n.º 2
0
// Responds to the Context with the specified status, data and errors.
func (a *GowebAPIResponder) Respond(ctx context.Context, status int, data interface{}, errors []string) error {

	if data != nil {

		var dataErr error
		data, dataErr = codecs.PublicData(data, nil)

		if dataErr != nil {
			return dataErr
		}

	}

	// make the standard response object
	if (a.AlwaysEnvelopResponse && ctx.QueryValue("envelop") != "false") || ctx.QueryValue("envelop") == "true" {
		sro := map[string]interface{}{
			a.StandardFieldStatusKey: status,
		}

		if data != nil {
			sro[a.StandardFieldDataKey] = data
		}

		if len(errors) > 0 {
			sro[a.StandardFieldErrorsKey] = errors
		}

		data = sro
	}

	// transform the object
	var transformErr error
	data, transformErr = a.TransformStandardResponseObject(ctx, data)

	if transformErr != nil {
		return transformErr
	}

	return a.WriteResponseObject(ctx, status, data)

}