Skip to content

Commit

Permalink
Update package url, adding go mod file
Browse files Browse the repository at this point in the history
Adding github action for tests
  • Loading branch information
Alireza Josheghani committed Jun 4, 2021
1 parent 25d4d18 commit d86e479
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 239 deletions.
18 changes: 0 additions & 18 deletions .circleci/config.yml

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Run tests on all branches

on:
push:
branches:
- "*"
tags:
- "*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Run tests
run: go test -v -race
58 changes: 20 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,92 @@
[![Build Status](https://circleci.com/gh/iamalirezaj/go-respond.svg?&style=shield)](https://circleci.com/gh/iamalirezaj/go-respond)
[![Go Report](https://goreportcard.com/badge/github.com/iamalirezaj/go-respond)](https://goreportcard.com/report/github.com/iamalirezaj/go-respond)
[![License](https://img.shields.io/:license-mit-blue.svg?style=flat-square)](#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
$ go get github.com/mrjosh/respond.go

## 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
```go
import "github.com/iamalirezaj/go-respond"
import "github.com/mrjosh/respond.go"
```

create respond instance
```go
var respond josh.Respond
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:
```go
respond.Succeed(map[string] interface{} {
jspon.Succeed(map[string] interface{} {
"some_key": "some_data"
})
```

When deletion action succeeds:
```go
respond.DeleteSucceeded()
jspon.DeleteSucceeded()
```

When updating succeeds:
```go
respond.UpdateSucceeded()
jspon.UpdateSucceeded()
```

When insertion succeeds:
```go
respond.InsertSucceeded()
jspon.InsertSucceeded()
```

When deletion action fails:
```go
respond.DeleteFaild()
jspon.DeleteFaild()
```

When updating fails:
```go
respond.UpdateFaild()
jspon.UpdateFaild()
```

when insertion fails:
```go
respond.InsertFaild()
jspon.InsertFaild()
```

Not Found Error:
```go
respond.NotFound()
jspon.NotFound()
```

When parameters entered are wrong:
```go
respond.WrongParameters()
jspon.WrongParameters()
```

When requested method is not allowed:
```go
respond.MethodNotAllowed()
jspon.MethodNotAllowed()
```

```go
respond.RequestFieldNotFound()
jspon.RequestFieldNotFound()
```

Validation errors:
```go
respond.ValidationErrors(map[string] interface{} {
"some_key": "some_validation_errors_data"
jspon.ValidationErrors(map[string] interface{} {
"some_key": "some_validation_errors_data"
})
```

###customization
You can do more:
```go
respond.SetStatusCode(200).setStatusText('Success.').RespondWithMessage('Your custom message')
jspon.SetStatusCode(200).setStatusText('Success.').RespondWithMessage('Your custom message')
```

## License
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/mrjosh/respond.go

go 1.16

require github.com/stretchr/testify v1.7.0 // indirect
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 5 additions & 5 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package respond

import (
"github.com/MrJoshLab/go-respond/translations/en"
"github.com/MrJoshLab/go-respond/translations/fa"
"github.com/mrjosh/respond.go/translations/en"
"github.com/mrjosh/respond.go/translations/fa"
)

type Message struct {
Expand All @@ -17,7 +17,7 @@ type Message struct {
Failed string

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

// Load config of response language
Expand All @@ -27,7 +27,7 @@ type Message struct {
// @return *Message
func (message *Message) LoadConfig() *Message {

var translation map[string] interface{}
var translation map[string]interface{}

switch message.Lang {
case "fa":
Expand All @@ -36,7 +36,7 @@ func (message *Message) LoadConfig() *Message {
translation = en.Errors
}

message.Errors = translation["errors"].(map[string] map[string] interface{})
message.Errors = translation["errors"].(map[string]map[string]interface{})
message.Success = translation["success"].(string)
message.Failed = translation["failed"].(string)

Expand Down
37 changes: 19 additions & 18 deletions respond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package respond

import (
"testing"

"github.com/stretchr/testify/assert"
)

Expand All @@ -10,7 +11,7 @@ func TestNotFound(test *testing.T) {
statusCode, result := Default.NotFound()

assert.Equal(test, 404, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "Oops... The requested page not found!",
"status": "failed",
"error": 5404,
Expand All @@ -19,14 +20,14 @@ func TestNotFound(test *testing.T) {

func TestSucceed(test *testing.T) {

statusCode, result := Default.Succeed(map[string] interface{} {
statusCode, result := Default.Succeed(map[string]interface{}{
"data": "Test",
})

assert.Equal(test, 200, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"status": "success",
"result": map[string] interface{} {
"result": map[string]interface{}{
"data": "Test",
},
}, result)
Expand All @@ -37,7 +38,7 @@ func TestInsertSucceeded(test *testing.T) {
statusCode, result := Default.InsertSucceeded()

assert.Equal(test, 200, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "The requested parameter is added successfully!",
"status": "success",
}, result)
Expand All @@ -48,7 +49,7 @@ func TestInsertFailed(test *testing.T) {
statusCode, result := Default.InsertFailed()

assert.Equal(test, 448, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "The requested parameter is not added!",
"status": "failed",
}, result)
Expand All @@ -59,7 +60,7 @@ func TestDeleteSucceeded(test *testing.T) {
statusCode, result := Default.DeleteSucceeded()

assert.Equal(test, 200, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "The requested parameter is deleted successfully!",
"status": "success",
}, result)
Expand All @@ -70,7 +71,7 @@ func TestDeleteFailed(test *testing.T) {
statusCode, result := Default.DeleteFailed()

assert.Equal(test, 447, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "The requested parameter is not deleted!",
"status": "failed",
}, result)
Expand All @@ -81,7 +82,7 @@ func TestUpdateSucceeded(test *testing.T) {
statusCode, result := Default.UpdateSucceeded()

assert.Equal(test, 200, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "The requested parameter is updated successfully!",
"status": "success",
}, result)
Expand All @@ -92,7 +93,7 @@ func TestUpdateFailed(test *testing.T) {
statusCode, result := Default.UpdateFailed()

assert.Equal(test, 449, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "The requested parameter is not updated!",
"status": "failed",
}, result)
Expand All @@ -103,7 +104,7 @@ func TestWrongParameters(test *testing.T) {
statusCode, result := Default.WrongParameters()

assert.Equal(test, 406, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "Oops... The parameters you entered are wrong!",
"status": "failed",
"error": 5406,
Expand All @@ -115,7 +116,7 @@ func TestMethodNotAllowed(test *testing.T) {
statusCode, result := Default.MethodNotAllowed()

assert.Equal(test, 405, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"message": "Oops... The method you requested is not allowed!",
"status": "failed",
"error": 5405,
Expand All @@ -124,14 +125,14 @@ func TestMethodNotAllowed(test *testing.T) {

func TestValidationErrors(test *testing.T) {

statusCode, result := Default.ValidationErrors(map[string] interface{} {
statusCode, result := Default.ValidationErrors(map[string]interface{}{
"data": "Test",
})

assert.Equal(test, 420, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"status": "failed",
"result": map[string] interface{} {
"result": map[string]interface{}{
"data": "Test",
},
}, result)
Expand All @@ -142,7 +143,7 @@ func TestRequestFieldNotfound(test *testing.T) {
statusCode, result := Default.RequestFieldNotfound()

assert.Equal(test, 446, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"status": "failed",
"error": 1001,
"message": "Oops... Requested field is not found!",
Expand All @@ -154,7 +155,7 @@ func TestRequestFieldDuplicated(test *testing.T) {
statusCode, result := Default.RequestFieldDuplicated()

assert.Equal(test, 400, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"status": "failed",
"error": 1004,
"message": "Failed because of duplicate",
Expand All @@ -166,7 +167,7 @@ func TestDefaultWithLang(test *testing.T) {
statusCode, result := DefaultWithLang("fa").NotFound()

assert.Equal(test, 404, statusCode)
assert.Equal(test, map[string] interface{} {
assert.Equal(test, map[string]interface{}{
"status": "نا موفق",
"error": 5404,
"message": ".صفحه درخواست شده پیدا نمیشود",
Expand Down
Loading

0 comments on commit d86e479

Please sign in to comment.