diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 2adb1b6..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,18 +0,0 @@ -# Golang CircleCI 2.0 configuration file - -version: 2 -jobs: - build: - docker: - # specify the version - - image: circleci/golang:1.10 - - working_directory: /go/src/github.com/iamalirezaj/go-respond - steps: - - checkout - - # install gopkg.in/yaml.v2 package - - run: go get gopkg.in/yaml.v2 - - # run test - - run: go test \ No newline at end of file diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml new file mode 100644 index 0000000..7993eed --- /dev/null +++ b/.github/workflows/go-tests.yml @@ -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 diff --git a/README.md b/README.md index 6fae135..02295ca 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b6c94c4 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/mrjosh/respond.go + +go 1.16 + +require github.com/stretchr/testify v1.7.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b380ae4 --- /dev/null +++ b/go.sum @@ -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= diff --git a/message.go b/message.go index 9e44ef3..05e4c74 100644 --- a/message.go +++ b/message.go @@ -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 { @@ -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 @@ -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": @@ -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) diff --git a/respond_test.go b/respond_test.go index 230f353..ef588e0 100644 --- a/respond_test.go +++ b/respond_test.go @@ -2,6 +2,7 @@ package respond import ( "testing" + "github.com/stretchr/testify/assert" ) @@ -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, @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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, @@ -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, @@ -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) @@ -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!", @@ -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", @@ -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": ".صفحه درخواست شده پیدا نمیشود", diff --git a/translations/en/en.go b/translations/en/en.go index e6d142b..754ff57 100644 --- a/translations/en/en.go +++ b/translations/en/en.go @@ -1,152 +1,151 @@ package en -var Errors = map[string] interface{} { +var Errors = map[string]interface{}{ "success": "success", - "failed": "failed", + "failed": "failed", - "errors": map[string] map[string] interface{} { - "success":{ + "errors": map[string]map[string]interface{}{ + "success": { "insert": "The requested parameter is added successfully!", "delete": "The requested parameter is deleted successfully!", "update": "The requested parameter is updated successfully!", }, - "failed":{ + "failed": { "insert": "The requested parameter is not added!", "delete": "The requested parameter is not deleted!", "update": "The requested parameter is not updated!", }, - "1001":{ + "1001": { "message": "Oops... Requested field is not found!", - "type": "error", + "type": "error", }, - "1002":{ + "1002": { "message": "Oops... Requested User does not exists!", - "type": "error", + "type": "error", }, - "1003":{ + "1003": { "message": "Oops... Client type is not entered!", - "type": "error", + "type": "error", }, - "1004":{ + "1004": { "message": "Failed because of duplicate", - "type": "error", + "type": "error", }, - "1005":{ + "1005": { "message": "Failed because of dablicated user role", - "type": "error", + "type": "error", }, - "3001":{ + "3001": { "message": "You are not logged on", - "type": "error", - "cat": "auth", - "short": "not-logged-on", + "type": "error", + "cat": "auth", + "short": "not-logged-on", }, - "3002":{ + "3002": { "message": "Application token did not generated successfully", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3003":{ + "3003": { "message": "User token did not generated successfully", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3005":{ + "3005": { "message": "Request token did not contains user information", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3006":{ + "3006": { "message": "Did not set request token", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3007":{ + "3007": { "message": "can not decode the token", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3008":{ + "3008": { "message": "can not generate token for authentication", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3009":{ + "3009": { "message": "can not create token", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3010":{ + "3010": { "message": "Token expired!", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3011":{ + "3011": { "message": "Token is invalid!", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3012":{ + "3012": { "message": "Token Blacklisted", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3013":{ + "3013": { "message": "Payload invalid!", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3014":{ + "3014": { "message": "Claim Invalid", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3015":{ + "3015": { "message": "An error occurred on token validation", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "5401":{ + "5401": { "message": "Authentication unauthorized...", - "type": "error", + "type": "error", }, - "5404":{ + "5404": { "message": "Oops... The requested page not found!", - "type": "error", + "type": "error", }, - "5405":{ + "5405": { "message": "Oops... The method you requested is not allowed!", - "type": "error", + "type": "error", }, - "5406":{ + "5406": { "message": "Oops... The parameters you entered are wrong!", - "type": "error", + "type": "error", }, - "5420":{ + "5420": { "message": "Validation Error", - "type": "error", + "type": "error", }, - "5422":{ + "5422": { "message": "Token is not valid", - "type": "error", + "type": "error", }, - "5445":{ + "5445": { "message": "Oops... Database connection refused", - "type": "error", + "type": "error", }, - "5447":{ + "5447": { "message": "Oops... Delete action was not successfully executed", - "type": "error", + "type": "error", }, - "5448":{ + "5448": { "message": "Oops... Insert action was not successfully executed", - "type": "error", + "type": "error", }, - "5449":{ + "5449": { "message": "Oops... Update action was not successfully executed", - "type": "error", + "type": "error", }, }, - -} \ No newline at end of file +} diff --git a/translations/fa/fa.go b/translations/fa/fa.go index 2204396..ab916b7 100644 --- a/translations/fa/fa.go +++ b/translations/fa/fa.go @@ -1,152 +1,151 @@ package fa -var Errors = map[string] interface{} { +var Errors = map[string]interface{}{ "success": "موفق", - "failed": "نا موفق", + "failed": "نا موفق", - "errors": map[string] map[string] interface{} { - "success":{ + "errors": map[string]map[string]interface{}{ + "success": { "insert": ".درخواست با موفقیت درج شده است", "delete": ".درخواست با موفقیت پاک شده است", "update": ".درخواست با موفقیت ویرایش شده است", }, - "failed":{ + "failed": { "insert": ".درخواست با موفقیت درج نشد", "delete": ".درخواست با موفقیت پاک نشد", "update": ".درخواست با موفقیت ویرایش نشد", }, - "1001":{ + "1001": { "message": ".درخواست مورد نظر پیدا نشده است", - "type": "error", + "type": "error", }, - "1002":{ + "1002": { "message": ".کاربر مورد نظر موجود نیست", - "type": "error", + "type": "error", }, - "1003":{ + "1003": { "message": ".نوع کاربری وارد نشده است", - "type": "error", + "type": "error", }, - "1004":{ + "1004": { "message": ".ورودی مورد نظر تکراری است", - "type": "error", + "type": "error", }, - "1005":{ + "1005": { "message": ".نقش کاربر مورد نظر تکراری است", - "type": "error", + "type": "error", }, - "3001":{ + "3001": { "message": ".شما به سیستم وارد نشده اید", - "type": "error", - "cat": "auth", - "short": "not-logged-on", + "type": "error", + "cat": "auth", + "short": "not-logged-on", }, - "3002":{ + "3002": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3003":{ + "3003": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3005":{ + "3005": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3006":{ + "3006": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3007":{ + "3007": { "message": ".نشان شناسایی شما نمایش داده نمیشود", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3008":{ + "3008": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3009":{ + "3009": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3010":{ + "3010": { "message": ".زمان استفاده از نشان شناسایی شما گذشته است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3011":{ + "3011": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3012":{ + "3012": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3013":{ + "3013": { "message": ".Payload معتبر نیست", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3014":{ + "3014": { "message": ".Claim معتبر نیست", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "3015":{ + "3015": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", - "cat": "auth", + "type": "error", + "cat": "auth", }, - "5401":{ + "5401": { "message": ".شناسایی کاربر نامعتبر است", - "type": "error", + "type": "error", }, - "5404":{ + "5404": { "message": ".صفحه درخواست شده پیدا نمیشود", - "type": "error", + "type": "error", }, - "5405":{ + "5405": { "message": ".شما به درخواستی که داده اید دسترسی ندارید", - "type": "error", + "type": "error", }, - "5406":{ + "5406": { "message": ".پارامترهایی که شما وارد کرده اید نا معتبر است", - "type": "error", + "type": "error", }, - "5420":{ + "5420": { "message": ".خطای اعتبار سنجی", - "type": "error", + "type": "error", }, - "5422":{ + "5422": { "message": ".نشان شناسایی شما نامعتبر است", - "type": "error", + "type": "error", }, - "5445":{ + "5445": { "message": ".ارتباط با پایگاه داده مشکل دارد", - "type": "error", + "type": "error", }, - "5447":{ + "5447": { "message": ".عملیات پاک کردن درست اجرا نشده است", - "type": "error", + "type": "error", }, - "5448":{ + "5448": { "message": ".عملیات درج درست اجرا نشده است", - "type": "error", + "type": "error", }, - "5449":{ + "5449": { "message": ".عملیات ویرایش درست اجرا نشده است", - "type": "error", + "type": "error", }, }, - -} \ No newline at end of file +}