From 962d83affd9e8cd2eeb606348facaf952aac56d5 Mon Sep 17 00:00:00 2001 From: Leon Silcott Date: Sat, 4 Sep 2021 10:31:52 -0400 Subject: [PATCH] Fix item merging bug --- replier.go | 17 +++++++---------- replier_test.go | 6 +++++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/replier.go b/replier.go index 26339c6..71a648d 100644 --- a/replier.go +++ b/replier.go @@ -70,7 +70,7 @@ func NewReplier(manifests []ErrorManifest, options ...Option) *Replier { activeTransferObject := &defaultReplyTransferObject{} replier := Replier{ - errorManifest: mergeManifestCollections(manifests...), + errorManifest: mergeManifestCollections(manifests), transferObject: activeTransferObject, } @@ -220,27 +220,24 @@ func sendHTTPResponse(writer http.ResponseWriter, transferObject TransferObject) // mergeManifestCollections handles merges the passed manifests into a singular // map -func mergeManifestCollections(manifests ...ErrorManifest) ErrorManifest { +func mergeManifestCollections(manifests []ErrorManifest) ErrorManifest { mergedManifests := make(ErrorManifest) for _, manifest := range manifests { - key, value := getManifestAttributes(manifest) - mergedManifests[key] = *value + getManifestItems(manifest, mergedManifests) } return mergedManifests } -// getManifestAttributes returns key and value for pass manifest -func getManifestAttributes(manifest ErrorManifest) (key string, value *ErrorManifestItem) { +// getManifestItems pulls the key and items from the manifest and inserts into final manifest +func getManifestItems(manifest ErrorManifest, finalManifest ErrorManifest) { - for k, v := range manifest { - key = k - value = &v + for key, item := range manifest { + finalManifest[key] = item } - return key, value } // getInternalServertErrorManifestItem returns typical 500 error with text and message diff --git a/replier_test.go b/replier_test.go index 1f752fe..6b0a0f4 100644 --- a/replier_test.go +++ b/replier_test.go @@ -185,9 +185,13 @@ func TestReplier_NewHTTPResponseForError(t *testing.T) { }{ { name: "Success - Resource not found", - manifests: []reply.ErrorManifest{ + manifests: append([]reply.ErrorManifest{ {"test-404-error": reply.ErrorManifestItem{Message: "resource not found", StatusCode: http.StatusNotFound}}, }, + reply.ErrorManifest{ + "test-401-error": reply.ErrorManifestItem{Message: "unauthorized", StatusCode: http.StatusUnauthorized}, + }, + ), err: errors.New("test-404-error"), expectedStatusCode: http.StatusNotFound, assertResponse: func(w *httptest.ResponseRecorder, t *testing.T) {