Ejemplo n.º 1
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 {

	// make the standard response object
	sro := map[string]interface{}{a.StandardFieldStatusKey: status}

	if data != nil {

		var dataErr error
		sro[a.StandardFieldDataKey], dataErr = codecs.PublicData(data, nil)

		if dataErr != nil {
			return dataErr
		}

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

	// transofm the object
	var transformErr error
	sro, transformErr = a.TransformStandardResponseObject(ctx, sro)

	if transformErr != nil {
		return transformErr
	}

	return a.WriteResponseObject(ctx, status, sro)

}
Ejemplo n.º 2
0
func TestCredentials_PublicData(t *testing.T) {

	creds := &Credentials{objx.MSI("auth", "ABC123", CredentialsKeyID, 123)}

	publicData, _ := codecs.PublicData(creds, nil)

	if assert.NotNil(t, publicData) {
		assert.Equal(t, "ABC123", publicData.(objx.Map)["auth"])
		assert.Equal(t, "123", publicData.(objx.Map)[CredentialsKeyID], "CredentialsKeyID ("+CredentialsKeyID+") must be turned into a string.")
	}

}
Ejemplo n.º 3
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)
}
Ejemplo n.º 4
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)

}