respond

package module
v0.0.0-...-25d4d18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2019 License: MIT Imports: 3 Imported by: 0

README

Build Status Go Report License

Go Respond

This package is provided to be used on golang and it gives clean methods to handle json response with specific predetermined messages.

Requirement

  • Golang 1.10
  • gopkg.in/yaml.v2

The package is in process.

Install

Run this commands

$ go get gopkg.in/yaml.v2

$ go get github.com/iamalirezaj/go-respond

Usage

You can use these methods in deffernt ways:

There are hot ones for quick usage, besides some provided to manage outputs on your own way

import package

import "github.com/iamalirezaj/go-respond"

create respond instance

var respond josh.Respond

Some are shown below:

When request succeeds and contains data to return as a result:

respond.Succeed(map[string] interface{} {
    "some_key": "some_data"
})

When deletion action succeeds:

respond.DeleteSucceeded()

When updating succeeds:

respond.UpdateSucceeded()

When insertion succeeds:

respond.InsertSucceeded()

When deletion action fails:

respond.DeleteFaild()

When updating fails:

respond.UpdateFaild()

when insertion fails:

respond.InsertFaild()

Not Found Error:

respond.NotFound()

When parameters entered are wrong:

respond.WrongParameters()

When requested method is not allowed:

respond.MethodNotAllowed()
respond.RequestFieldNotFound()

Validation errors:

respond.ValidationErrors(map[string] interface{} {
    "some_key": "some_validation_errors_data"
})

###customization You can do more:

respond.SetStatusCode(200).setStatusText('Success.').RespondWithMessage('Your custom message')

License

The MIT License (MIT). Please see License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = &Respond{}
View Source
var DefaultWithLang = func(lang string) *Respond {
	return &Respond{lang: lang}
}

Functions

This section is empty.

Types

type Message

type Message struct {

	// language of response
	Lang string

	// success field of response
	Success string

	// failed field of response
	Failed string

	// respond error messages
	Errors map[string]map[string]interface{}
}

func (*Message) LoadConfig

func (message *Message) LoadConfig() *Message

Load config of response language

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return *Message

type Respond

type Respond struct {
	// contains filtered or unexported fields
}

func (Respond) DeleteFailed

func (respond Respond) DeleteFailed() (int, interface{})

Delete action is failed

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) DeleteSucceeded

func (respond Respond) DeleteSucceeded() (int, interface{})

Delete action is succeed

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) Error

func (respond Respond) Error(statusCode int, errorCode int) (int, interface{})

The error message

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param statusCode int,errorCode string @return (statuscode int, result interface{})

func (Respond) InsertFailed

func (respond Respond) InsertFailed() (int, interface{})

Insert action is failed

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) InsertSucceeded

func (respond Respond) InsertSucceeded() (int, interface{})

Insert action is succeed

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) Message

func (respond Respond) Message() *Message

Get message type

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return *Message

func (Respond) MethodNotAllowed

func (respond Respond) MethodNotAllowed() (int, interface{})

Wrong parameters are entered

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) NotFound

func (respond Respond) NotFound() (int, interface{})

return notfound result

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) RequestFieldDuplicated

func (respond Respond) RequestFieldDuplicated() (int, interface{})

The request field is duplicated

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) RequestFieldNotfound

func (respond Respond) RequestFieldNotfound() (int, interface{})

The request field is not found

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) RespondWithMessage

func (respond Respond) RespondWithMessage(message interface{}) (int, interface{})

Pass response with message text as string

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param message interface{} @return (statuscode int, result interface{})

func (Respond) RespondWithResult

func (respond Respond) RespondWithResult(result interface{}) (int, interface{})

Pass response with result data like this array

array := map[string]interface{} {
        "status": respond.statusText,
        "result": result,
}

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param result map[string]interface{} @return (statuscode int, result interface{})

func (Respond) SetErrorCode

func (respond Respond) SetErrorCode(code int) Respond

Set status code of response and set default value as 0

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return Respond @param code int

func (Respond) SetStatusCode

func (respond Respond) SetStatusCode(code int) Respond

Set status code of response and set default value as 0

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return Respond @param code int

func (Respond) SetStatusText

func (respond Respond) SetStatusText(text string) Respond

Set status text of response

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return Respond @param text string

func (Respond) Succeed

func (respond Respond) Succeed(data interface{}) (int, interface{})

return success result with data

data := map[string]interface{} {
        "data": "somedata"
}

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param data map[string]interface{} @return (statuscode int, result interface{})

func (Respond) UpdateFailed

func (respond Respond) UpdateFailed() (int, interface{})

Update action is failed

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) UpdateSucceeded

func (respond Respond) UpdateSucceeded() (int, interface{})

Update action is succeed

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

func (Respond) ValidationErrors

func (respond Respond) ValidationErrors(errors interface{}) (int, interface{})

There ara validation translations

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param translations map[string]interface{} @return (statuscode int, result interface{})

func (Respond) WrongParameters

func (respond Respond) WrongParameters() (int, interface{})

Wrong parameters are entered

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @return (statuscode int, result interface{})

Directories

Path Synopsis
translations
en
fa

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL