respond

package module
v0.0.0-...-8cb44bc Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: MIT Imports: 6 Imported by: 0

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/Briofy/respond.go

Usage

import package

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

create respond instance


// argument rw is http.ResponseWriter or 
// gin.Writer if youre using gin-gonic
var jspon = respond.NewWithWriter(rw)

// or if you want to use custom languages
var jspon = respond.NewWithWriter(rw).Language("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(http.StatusOK).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

This section is empty.

Functions

This section is empty.

Types

type Messages

type Messages struct {
	Lang      string
	Success   string
	Failed    string
	Errors    map[string]map[string]interface{}
	Languages map[string]map[string]interface{}
	sync.RWMutex
}

func NewMessages

func NewMessages() *Messages

func (*Messages) AddLanguageTranslation

func (m *Messages) AddLanguageTranslation(lang string, messages map[string]interface{})

type Respond

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

func NewWithWriter

func NewWithWriter(w http.ResponseWriter) *Respond

New respond type with custom writer

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 6 Jun 2021 @return *Respond

func (*Respond) DeleteFailed

func (r *Respond) DeleteFailed()

Delete action is failed

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

func (*Respond) DeleteSucceeded

func (r *Respond) DeleteSucceeded()

Delete action is succeed

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

func (*Respond) Error

func (r *Respond) Error(statusCode int, errorCode int)

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 (r *Respond) InsertFailed()

Insert action is failed

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

func (*Respond) InsertSucceeded

func (r *Respond) InsertSucceeded()

Insert action is succeed

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

func (*Respond) Language

func (r *Respond) Language(lang string) *Respond

Set language of responses

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 6 Jun 2021 @return *Respond

func (*Respond) Messages

func (r *Respond) Messages() *Messages

Get message type

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

func (*Respond) MethodNotAllowed

func (r *Respond) MethodNotAllowed()

Wrong parameters are entered

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

func (*Respond) NotFound

func (r *Respond) NotFound()

return notfound result

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

func (*Respond) RequestFieldDuplicated

func (r *Respond) RequestFieldDuplicated()

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 (r *Respond) RequestFieldNotfound()

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 (r *Respond) RespondWithMessage(message interface{})

Pass response with message text as string

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param message interface{} @return error

func (*Respond) RespondWithResult

func (r *Respond) RespondWithResult(result 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 error

func (*Respond) SetErrorCode

func (r *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 (r *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 (r *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 (r *Respond) Succeed(data 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{}

func (*Respond) UpdateFailed

func (r *Respond) UpdateFailed()

Update action is failed

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

func (*Respond) UpdateSucceeded

func (r *Respond) UpdateSucceeded()

Update action is succeed

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

func (*Respond) ValidationErrors

func (r *Respond) ValidationErrors(errors interface{})

There ara validation translations

@author Alireza Josheghani <josheghani.dev@gmail.com> @since 15 Mar 2018 @param translations map[string]interface{}

func (*Respond) WrongParameters

func (r *Respond) WrongParameters()

Wrong parameters are entered

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

Directories

Path Synopsis
translations
en
fa

Jump to

Keyboard shortcuts

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