respond

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2021 License: MIT Imports: 3 Imported by: 8

README

Go Respond

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

Install

$ go get github.com/mrjosh/respond.go

Usage

import package

import "github.com/mrjosh/respond.go"

create respond instance

var jspon = respond.Default

// or if you want to use custom languages
var jspon = respond.DefaultWithLang("fa")

Some are shown below:

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

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

When deletion action succeeds:

jspon.DeleteSucceeded()

When updating succeeds:

jspon.UpdateSucceeded()

When insertion succeeds:

jspon.InsertSucceeded()

When deletion action fails:

jspon.DeleteFaild()

When updating fails:

jspon.UpdateFaild()

when insertion fails:

jspon.InsertFaild()

Not Found Error:

jspon.NotFound()

When parameters entered are wrong:

jspon.WrongParameters()

When requested method is not allowed:

jspon.MethodNotAllowed()
jspon.RequestFieldNotFound()

Validation errors:

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

###customization You can do more:

jspon.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