From f862356fea635a67beb0aa091c96ea7c2676c8f5 Mon Sep 17 00:00:00 2001 From: KaushikiAnand Date: Mon, 3 Mar 2025 11:29:19 +0530 Subject: [PATCH 01/11] workflow updation --- .github/workflows/ci.yml | 28 ++++++++++++++++++----- .golangci.yml | 6 +++++ errors_test.go | 4 +++- examples/handler.go | 5 ++++ examples/handler_test.go | 4 ++-- request.go | 12 +++++++--- request_test.go | 49 ++++++++++++++++++++++++++++++---------- response_test.go | 24 +++++++++++++++----- runtime.go | 5 +++- 9 files changed, 106 insertions(+), 31 deletions(-) create mode 100644 .golangci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e6527955..04a28d2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,8 +2,7 @@ name: CI on: push: branches: - - main - pull_request: + - build-test jobs: unit-test: @@ -12,11 +11,28 @@ jobs: matrix: go: [ '1.21', '1.20', '1.19', '1.18'] steps: - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + - name: Checkout Code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 + - name: Setup Go + uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: go-version: ${{ matrix.go }} - - name: test - run: go test -race . -v + - name: Run golangci-lint + uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 + + - name: run test and generate coverage report + run: go test -race . -v -coverprofile=coverage.out + + - name: Upload coverage report + uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 + with: + path: coverage.out + name: Coverage-report-${{matrix.go}} + + - name: Display coverage report + run: go tool cover -func=coverage.out + + - name: Build Go + run: go build ./... diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..c3cc3d96 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,6 @@ +linters: + enable: + - errcheck + disable: + - unused +output_format: colored-line-number \ No newline at end of file diff --git a/errors_test.go b/errors_test.go index ef07359c..78d1c4e2 100644 --- a/errors_test.go +++ b/errors_test.go @@ -47,7 +47,9 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) { var writer io.Writer = buffer _ = MarshalErrors(writer, testRow.In) - json.Unmarshal(buffer.Bytes(), &output) + if err := json.Unmarshal(buffer.Bytes(), &output); err != nil { + t.Fatalf("failed to unmarshal: %v", err) + } if !reflect.DeepEqual(output, testRow.Out) { t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out) diff --git a/examples/handler.go b/examples/handler.go index f01a3600..b7ba8fd7 100644 --- a/examples/handler.go +++ b/examples/handler.go @@ -69,6 +69,11 @@ func (h *ExampleHandler) updateBlog(w http.ResponseWriter, r *http.Request) { blog := new(Blog) + if r.Body == nil { + http.Error(w, "request body cannot be empty", http.StatusBadRequest) + return + } + if err := jsonapiRuntime.UnmarshalPayload(r.Body, blog); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/examples/handler_test.go b/examples/handler_test.go index 20adc298..0a5a08fa 100644 --- a/examples/handler_test.go +++ b/examples/handler_test.go @@ -112,7 +112,7 @@ func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) { handler := &ExampleHandler{} handler.ServeHTTP(rr, r) - if rr.Code != http.StatusNotFound { - t.Fatal("expected HTTP Status Not Found status error") + if rr.Code != http.StatusBadRequest { + t.Fatalf("expected HTTP Status Bad Request (400), got %d", rr.Code) } } diff --git a/request.go b/request.go index ea78c50d..2ccd05aa 100644 --- a/request.go +++ b/request.go @@ -453,8 +453,12 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) - json.NewEncoder(buf).Encode(data.Relationships[args[1]]) - json.NewDecoder(buf).Decode(relationship) + if err := json.NewEncoder(buf).Encode(data.Relationships[args[1]]); err != nil { + return err + } + if err := json.NewDecoder(buf).Decode(relationship); err != nil { + return err + } data := relationship.Data @@ -483,7 +487,9 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) relDataStr := data.Relationships[args[1]] - json.NewEncoder(buf).Encode(relDataStr) + if err := json.NewEncoder(buf).Encode(relDataStr); err != nil { + return err + } isExplicitNull := false relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship) diff --git a/request_test.go b/request_test.go index 7fa0d3ec..fd47c145 100644 --- a/request_test.go +++ b/request_test.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "log" "reflect" "sort" "strconv" @@ -405,7 +406,9 @@ func TestUnmarshalNullableRelationshipsNonNullValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatalf("failed to encode: %v", err) + } out := new(WithNullableAttrs) @@ -442,7 +445,9 @@ func TestUnmarshalNullableRelationshipsExplicitNullValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatalf("failed to encode: %v", err) + } out := new(WithNullableAttrs) @@ -467,7 +472,9 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + log.Fatalf("failed to encode: %v", err) + } out := new(WithNullableAttrs) @@ -490,7 +497,9 @@ func TestUnmarshalNullableRelationshipsNoRelationships(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatalf("failed to encode: %v", err) + } out := new(WithNullableAttrs) @@ -1621,7 +1630,9 @@ func samplePayload() io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + if err := json.NewEncoder(out).Encode(payload); err != nil { + log.Printf("failed to encode: %v", err) + } return out } @@ -1639,7 +1650,9 @@ func samplePayloadWithID() io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + if err := json.NewEncoder(out).Encode(payload); err != nil { + log.Printf("failed to encode: %v", err) + } return out } @@ -1654,7 +1667,9 @@ func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + if err := json.NewEncoder(out).Encode(payload); err != nil { + log.Printf("failed to encode: %v", err) + } return out } @@ -1669,7 +1684,9 @@ func sampleWithPointerPayload(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + if err := json.NewEncoder(out).Encode(payload); err != nil { + log.Printf("failed to encode: %v", err) + } return out } @@ -1684,7 +1701,9 @@ func samplePayloadWithNullableAttrs(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + if err := json.NewEncoder(out).Encode(payload); err != nil { + log.Printf("failed to encode: %v", err) + } return out } @@ -1761,17 +1780,23 @@ func samplePayloadWithSideloaded() io.Reader { testModel := testModel() out := bytes.NewBuffer(nil) - MarshalPayload(out, testModel) + if err := MarshalPayload(out, testModel); err != nil { + log.Printf("failed to marshal payload: %v", err) + } return out } func sampleSerializedEmbeddedTestModel() *Blog { out := bytes.NewBuffer(nil) - MarshalOnePayloadEmbedded(out, testModel()) + if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil { + log.Printf("failed to marshal one payload embedded: %v", err) + } blog := new(Blog) - UnmarshalPayload(out, blog) + if err := UnmarshalPayload(out, blog); err != nil { + log.Printf("failed to unmarshal payload: %v", err) + } return blog } diff --git a/response_test.go b/response_test.go index 509b656d..9b8711e0 100644 --- a/response_test.go +++ b/response_test.go @@ -18,7 +18,9 @@ func TestMarshalPayload(t *testing.T) { // One out1 := bytes.NewBuffer(nil) - MarshalPayload(out1, book) + if err := MarshalPayload(out1, book); err != nil { + t.Fatal(err) + } if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil { t.Fatal(err) @@ -29,7 +31,9 @@ func TestMarshalPayload(t *testing.T) { // Many out2 := bytes.NewBuffer(nil) - MarshalPayload(out2, books) + if err := MarshalPayload(out2, books); err != nil { + t.Fatal(err) + } if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil { t.Fatal(err) @@ -936,7 +940,9 @@ func TestMarshal_Times(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1017,7 +1023,9 @@ func TestNullableRelationship(t *testing.T) { // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1114,7 +1122,9 @@ func TestNullableAttr_Time(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1184,7 +1194,9 @@ func TestNullableAttr_Bool(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) diff --git a/runtime.go b/runtime.go index db2d9f2f..872f22fe 100644 --- a/runtime.go +++ b/runtime.go @@ -77,10 +77,13 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error { // UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload. func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) { - r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { + err = r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { elems, err = UnmarshalManyPayload(reader, kind) return err }) + if err != nil { + return nil, err + } return } From 9f4cfa9918e4c5274bdefa02afe68a49419e3332 Mon Sep 17 00:00:00 2001 From: KaushikiAnand Date: Mon, 3 Mar 2025 11:30:21 +0530 Subject: [PATCH 02/11] workflow update --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04a28d2e..b81cc538 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 - name: run test and generate coverage report - run: go test -race . -v -coverprofile=coverage.out + run: go test -race ./... -v -coverprofile=coverage.out - name: Upload coverage report uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 From c9ef1d52bd8b46f119c84f3feff658a9c80cb4c6 Mon Sep 17 00:00:00 2001 From: KaushikiAnand Date: Thu, 13 Mar 2025 13:45:23 +0530 Subject: [PATCH 03/11] IND-2585 workflow updated with unit test coverage & linting --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b81cc538..884b9c8f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,8 @@ name: CI on: push: branches: - - build-test + - main + pull_request: jobs: unit-test: From 03a5534cb9aba6ceceb40c6a6ed0ea16c4e0c032 Mon Sep 17 00:00:00 2001 From: Sonam Tenzin Date: Thu, 27 Mar 2025 14:33:33 +0530 Subject: [PATCH 04/11] fix for handler test --- examples/handler.go | 5 ----- examples/handler_test.go | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/examples/handler.go b/examples/handler.go index b7ba8fd7..f01a3600 100644 --- a/examples/handler.go +++ b/examples/handler.go @@ -69,11 +69,6 @@ func (h *ExampleHandler) updateBlog(w http.ResponseWriter, r *http.Request) { blog := new(Blog) - if r.Body == nil { - http.Error(w, "request body cannot be empty", http.StatusBadRequest) - return - } - if err := jsonapiRuntime.UnmarshalPayload(r.Body, blog); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/examples/handler_test.go b/examples/handler_test.go index 0a5a08fa..41f90e77 100644 --- a/examples/handler_test.go +++ b/examples/handler_test.go @@ -102,7 +102,7 @@ func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) { } func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) { - r, err := http.NewRequest(http.MethodPatch, "/blogs", nil) + r, err := http.NewRequest(http.MethodOptions, "/blogs", nil) if err != nil { t.Fatal(err) } @@ -112,7 +112,7 @@ func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) { handler := &ExampleHandler{} handler.ServeHTTP(rr, r) - if rr.Code != http.StatusBadRequest { - t.Fatalf("expected HTTP Status Bad Request (400), got %d", rr.Code) + if rr.Code != http.StatusNotFound { + t.Fatal("expected HTTP Status Not Found status error") } } From 4998b4c415ce843716278dbb172655f7b39c59c2 Mon Sep 17 00:00:00 2001 From: KaushikiAnand Date: Thu, 27 Mar 2025 15:08:30 +0530 Subject: [PATCH 05/11] errcheck corrected as done in previous code --- .golangci.yml | 2 +- request.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c3cc3d96..101140ad 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,4 +3,4 @@ linters: - errcheck disable: - unused -output_format: colored-line-number \ No newline at end of file +output_format: colored-line-number diff --git a/request.go b/request.go index 2ccd05aa..be0c86a3 100644 --- a/request.go +++ b/request.go @@ -454,10 +454,10 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) if err := json.NewEncoder(buf).Encode(data.Relationships[args[1]]); err != nil { - return err + er = err } if err := json.NewDecoder(buf).Decode(relationship); err != nil { - return err + er = err } data := relationship.Data @@ -488,7 +488,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) relDataStr := data.Relationships[args[1]] if err := json.NewEncoder(buf).Encode(relDataStr); err != nil { - return err + er = err } isExplicitNull := false From a62fbeec7886fb54c897767122aff4c27fbfdc2b Mon Sep 17 00:00:00 2001 From: KaushikiAnand Date: Thu, 3 Apr 2025 16:10:03 +0530 Subject: [PATCH 06/11] resolved comment of using t.Fatalf --- request_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request_test.go b/request_test.go index fd47c145..ff493dad 100644 --- a/request_test.go +++ b/request_test.go @@ -473,7 +473,7 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) { outBuf := bytes.NewBuffer(nil) if err := json.NewEncoder(outBuf).Encode(payload); err != nil { - log.Fatalf("failed to encode: %v", err) + t.Fatalf("failed to encode: %v", err) } out := new(WithNullableAttrs) From 808fe9e4fe08f557b8e581c3a900c62b40285b3c Mon Sep 17 00:00:00 2001 From: KaushikiAnand Date: Thu, 10 Apr 2025 17:51:28 +0530 Subject: [PATCH 07/11] break added to newly added errors --- request.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/request.go b/request.go index be0c86a3..ff4b0d32 100644 --- a/request.go +++ b/request.go @@ -455,9 +455,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) if err := json.NewEncoder(buf).Encode(data.Relationships[args[1]]); err != nil { er = err + break } if err := json.NewDecoder(buf).Decode(relationship); err != nil { er = err + break } data := relationship.Data @@ -489,6 +491,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) relDataStr := data.Relationships[args[1]] if err := json.NewEncoder(buf).Encode(relDataStr); err != nil { er = err + break } isExplicitNull := false From fe1c2d5bf2c4ef38cfb2b57230dd33703771c474 Mon Sep 17 00:00:00 2001 From: Sonam Tenzin Date: Mon, 5 May 2025 12:25:59 +0530 Subject: [PATCH 08/11] disables --- .golangci.yml | 4 +--- errors_test.go | 4 +--- request.go | 15 +++------------ request_test.go | 49 ++++++++++++------------------------------------ response_test.go | 24 ++++++------------------ runtime.go | 5 +---- 6 files changed, 24 insertions(+), 77 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 101140ad..e5b4bbc4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,4 @@ linters: - enable: - - errcheck disable: - unused -output_format: colored-line-number + - errcheck diff --git a/errors_test.go b/errors_test.go index 78d1c4e2..ef07359c 100644 --- a/errors_test.go +++ b/errors_test.go @@ -47,9 +47,7 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) { var writer io.Writer = buffer _ = MarshalErrors(writer, testRow.In) - if err := json.Unmarshal(buffer.Bytes(), &output); err != nil { - t.Fatalf("failed to unmarshal: %v", err) - } + json.Unmarshal(buffer.Bytes(), &output) if !reflect.DeepEqual(output, testRow.Out) { t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out) diff --git a/request.go b/request.go index ff4b0d32..ea78c50d 100644 --- a/request.go +++ b/request.go @@ -453,14 +453,8 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) - if err := json.NewEncoder(buf).Encode(data.Relationships[args[1]]); err != nil { - er = err - break - } - if err := json.NewDecoder(buf).Decode(relationship); err != nil { - er = err - break - } + json.NewEncoder(buf).Encode(data.Relationships[args[1]]) + json.NewDecoder(buf).Decode(relationship) data := relationship.Data @@ -489,10 +483,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) relDataStr := data.Relationships[args[1]] - if err := json.NewEncoder(buf).Encode(relDataStr); err != nil { - er = err - break - } + json.NewEncoder(buf).Encode(relDataStr) isExplicitNull := false relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship) diff --git a/request_test.go b/request_test.go index ff493dad..7fa0d3ec 100644 --- a/request_test.go +++ b/request_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "log" "reflect" "sort" "strconv" @@ -406,9 +405,7 @@ func TestUnmarshalNullableRelationshipsNonNullValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - if err := json.NewEncoder(outBuf).Encode(payload); err != nil { - t.Fatalf("failed to encode: %v", err) - } + json.NewEncoder(outBuf).Encode(payload) out := new(WithNullableAttrs) @@ -445,9 +442,7 @@ func TestUnmarshalNullableRelationshipsExplicitNullValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - if err := json.NewEncoder(outBuf).Encode(payload); err != nil { - t.Fatalf("failed to encode: %v", err) - } + json.NewEncoder(outBuf).Encode(payload) out := new(WithNullableAttrs) @@ -472,9 +467,7 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - if err := json.NewEncoder(outBuf).Encode(payload); err != nil { - t.Fatalf("failed to encode: %v", err) - } + json.NewEncoder(outBuf).Encode(payload) out := new(WithNullableAttrs) @@ -497,9 +490,7 @@ func TestUnmarshalNullableRelationshipsNoRelationships(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - if err := json.NewEncoder(outBuf).Encode(payload); err != nil { - t.Fatalf("failed to encode: %v", err) - } + json.NewEncoder(outBuf).Encode(payload) out := new(WithNullableAttrs) @@ -1630,9 +1621,7 @@ func samplePayload() io.Reader { } out := bytes.NewBuffer(nil) - if err := json.NewEncoder(out).Encode(payload); err != nil { - log.Printf("failed to encode: %v", err) - } + json.NewEncoder(out).Encode(payload) return out } @@ -1650,9 +1639,7 @@ func samplePayloadWithID() io.Reader { } out := bytes.NewBuffer(nil) - if err := json.NewEncoder(out).Encode(payload); err != nil { - log.Printf("failed to encode: %v", err) - } + json.NewEncoder(out).Encode(payload) return out } @@ -1667,9 +1654,7 @@ func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - if err := json.NewEncoder(out).Encode(payload); err != nil { - log.Printf("failed to encode: %v", err) - } + json.NewEncoder(out).Encode(payload) return out } @@ -1684,9 +1669,7 @@ func sampleWithPointerPayload(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - if err := json.NewEncoder(out).Encode(payload); err != nil { - log.Printf("failed to encode: %v", err) - } + json.NewEncoder(out).Encode(payload) return out } @@ -1701,9 +1684,7 @@ func samplePayloadWithNullableAttrs(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - if err := json.NewEncoder(out).Encode(payload); err != nil { - log.Printf("failed to encode: %v", err) - } + json.NewEncoder(out).Encode(payload) return out } @@ -1780,23 +1761,17 @@ func samplePayloadWithSideloaded() io.Reader { testModel := testModel() out := bytes.NewBuffer(nil) - if err := MarshalPayload(out, testModel); err != nil { - log.Printf("failed to marshal payload: %v", err) - } + MarshalPayload(out, testModel) return out } func sampleSerializedEmbeddedTestModel() *Blog { out := bytes.NewBuffer(nil) - if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil { - log.Printf("failed to marshal one payload embedded: %v", err) - } + MarshalOnePayloadEmbedded(out, testModel()) blog := new(Blog) - if err := UnmarshalPayload(out, blog); err != nil { - log.Printf("failed to unmarshal payload: %v", err) - } + UnmarshalPayload(out, blog) return blog } diff --git a/response_test.go b/response_test.go index 9b8711e0..509b656d 100644 --- a/response_test.go +++ b/response_test.go @@ -18,9 +18,7 @@ func TestMarshalPayload(t *testing.T) { // One out1 := bytes.NewBuffer(nil) - if err := MarshalPayload(out1, book); err != nil { - t.Fatal(err) - } + MarshalPayload(out1, book) if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil { t.Fatal(err) @@ -31,9 +29,7 @@ func TestMarshalPayload(t *testing.T) { // Many out2 := bytes.NewBuffer(nil) - if err := MarshalPayload(out2, books); err != nil { - t.Fatal(err) - } + MarshalPayload(out2, books) if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil { t.Fatal(err) @@ -940,9 +936,7 @@ func TestMarshal_Times(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - if err := json.Unmarshal(out.Bytes(), &data); err != nil { - t.Fatal(err) - } + json.Unmarshal(out.Bytes(), &data) if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1023,9 +1017,7 @@ func TestNullableRelationship(t *testing.T) { // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - if err := json.Unmarshal(out.Bytes(), &data); err != nil { - t.Fatal(err) - } + json.Unmarshal(out.Bytes(), &data) if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1122,9 +1114,7 @@ func TestNullableAttr_Time(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - if err := json.Unmarshal(out.Bytes(), &data); err != nil { - t.Fatal(err) - } + json.Unmarshal(out.Bytes(), &data) if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1194,9 +1184,7 @@ func TestNullableAttr_Bool(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - if err := json.Unmarshal(out.Bytes(), &data); err != nil { - t.Fatal(err) - } + json.Unmarshal(out.Bytes(), &data) if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) diff --git a/runtime.go b/runtime.go index 872f22fe..db2d9f2f 100644 --- a/runtime.go +++ b/runtime.go @@ -77,13 +77,10 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error { // UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload. func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) { - err = r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { + r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { elems, err = UnmarshalManyPayload(reader, kind) return err }) - if err != nil { - return nil, err - } return } From d9fd63b96f5531bc572cc212cb7bb2d161670ced Mon Sep 17 00:00:00 2001 From: Sonam Tenzin Date: Mon, 5 May 2025 12:33:36 +0530 Subject: [PATCH 09/11] reordering of workflow --- .github/workflows/ci.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 884b9c8f..4b0b2363 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,9 @@ jobs: uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: go-version: ${{ matrix.go }} + + - name: Build Go + run: go build ./... - name: Run golangci-lint uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 @@ -34,6 +37,4 @@ jobs: - name: Display coverage report run: go tool cover -func=coverage.out - - - name: Build Go - run: go build ./... + From 2a7a1f16212a8fb5514fec1fb457593c63416c03 Mon Sep 17 00:00:00 2001 From: Sonam Tenzin Date: Tue, 6 May 2025 11:10:32 +0530 Subject: [PATCH 10/11] resolved comments --- .golangci.yml | 4 --- errors_test.go | 4 ++- examples/handler_test.go | 8 ++++-- models_test.go | 1 - request.go | 6 ++-- request_test.go | 62 +++++++++++++++++++++++++++------------- response_test.go | 24 ++++++++++++---- runtime.go | 2 +- 8 files changed, 73 insertions(+), 38 deletions(-) delete mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml deleted file mode 100644 index e5b4bbc4..00000000 --- a/.golangci.yml +++ /dev/null @@ -1,4 +0,0 @@ -linters: - disable: - - unused - - errcheck diff --git a/errors_test.go b/errors_test.go index ef07359c..78d1c4e2 100644 --- a/errors_test.go +++ b/errors_test.go @@ -47,7 +47,9 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) { var writer io.Writer = buffer _ = MarshalErrors(writer, testRow.In) - json.Unmarshal(buffer.Bytes(), &output) + if err := json.Unmarshal(buffer.Bytes(), &output); err != nil { + t.Fatalf("failed to unmarshal: %v", err) + } if !reflect.DeepEqual(output, testRow.Out) { t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out) diff --git a/examples/handler_test.go b/examples/handler_test.go index 41f90e77..8df53aa6 100644 --- a/examples/handler_test.go +++ b/examples/handler_test.go @@ -12,7 +12,9 @@ import ( func TestExampleHandler_post(t *testing.T) { blog := fixtureBlogCreate(1) requestBody := bytes.NewBuffer(nil) - jsonapi.MarshalOnePayloadEmbedded(requestBody, blog) + if err := jsonapi.MarshalOnePayloadEmbedded(requestBody, blog); err != nil { + t.Fatal(err) + } r, err := http.NewRequest(http.MethodPost, "/blogs?id=1", requestBody) if err != nil { @@ -36,7 +38,9 @@ func TestExampleHandler_put(t *testing.T) { fixtureBlogCreate(3), } requestBody := bytes.NewBuffer(nil) - jsonapi.MarshalPayload(requestBody, blogs) + if err := jsonapi.MarshalPayload(requestBody, blogs); err != nil { + t.Fatal(err) + } r, err := http.NewRequest(http.MethodPut, "/blogs", requestBody) if err != nil { diff --git a/models_test.go b/models_test.go index 0961e1b2..237f93a7 100644 --- a/models_test.go +++ b/models_test.go @@ -246,7 +246,6 @@ type Video struct { type OneOfMedia struct { Image *Image - random int Video *Video RandomStuff *string } diff --git a/request.go b/request.go index ea78c50d..79b983a3 100644 --- a/request.go +++ b/request.go @@ -453,8 +453,8 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) - json.NewEncoder(buf).Encode(data.Relationships[args[1]]) - json.NewDecoder(buf).Decode(relationship) + json.NewEncoder(buf).Encode(data.Relationships[args[1]]) //nolint:errcheck + json.NewDecoder(buf).Decode(relationship) //nolint:errcheck data := relationship.Data @@ -483,7 +483,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) buf := bytes.NewBuffer(nil) relDataStr := data.Relationships[args[1]] - json.NewEncoder(buf).Encode(relDataStr) + json.NewEncoder(buf).Encode(relDataStr) //nolint:errcheck isExplicitNull := false relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship) diff --git a/request_test.go b/request_test.go index 7fa0d3ec..6cf5163e 100644 --- a/request_test.go +++ b/request_test.go @@ -249,7 +249,6 @@ func TestUnmarshalToStructWithPointerAttr_BadType_Struct(t *testing.T) { func TestUnmarshalToStructWithPointerAttr_BadType_IntSlice(t *testing.T) { out := new(WithPointer) - type FooStruct struct{ A, B int } in := map[string]interface{}{ "name": []int{4, 5}, // This is the wrong type. } @@ -405,7 +404,9 @@ func TestUnmarshalNullableRelationshipsNonNullValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatal(err) + } out := new(WithNullableAttrs) @@ -442,7 +443,9 @@ func TestUnmarshalNullableRelationshipsExplicitNullValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatal(err) + } out := new(WithNullableAttrs) @@ -467,7 +470,9 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatal(err) + } out := new(WithNullableAttrs) @@ -490,7 +495,9 @@ func TestUnmarshalNullableRelationshipsNoRelationships(t *testing.T) { } outBuf := bytes.NewBuffer(nil) - json.NewEncoder(outBuf).Encode(payload) + if err := json.NewEncoder(outBuf).Encode(payload); err != nil { + t.Fatal(err) + } out := new(WithNullableAttrs) @@ -1120,7 +1127,10 @@ func TestUnmarshalNestedRelationships(t *testing.T) { } func TestUnmarshalRelationshipsSerializedEmbedded(t *testing.T) { - out := sampleSerializedEmbeddedTestModel() + out, err := sampleSerializedEmbeddedTestModel() + if err != nil { + t.Fatal(err) + } if out.CurrentPost == nil { t.Fatalf("Current post was not materialized") @@ -1169,7 +1179,10 @@ func TestUnmarshalNestedRelationshipsEmbedded(t *testing.T) { } func TestUnmarshalRelationshipsSideloaded(t *testing.T) { - payload := samplePayloadWithSideloaded() + payload, err := samplePayloadWithSideloaded() + if err != nil { + t.Fatal(err) + } out := new(Blog) if err := UnmarshalPayload(payload, out); err != nil { @@ -1190,7 +1203,10 @@ func TestUnmarshalRelationshipsSideloaded(t *testing.T) { } func TestUnmarshalNestedRelationshipsSideloaded(t *testing.T) { - payload := samplePayloadWithSideloaded() + payload, err := samplePayloadWithSideloaded() + if err != nil { + t.Fatal(err) + } out := new(Blog) if err := UnmarshalPayload(payload, out); err != nil { @@ -1621,7 +1637,7 @@ func samplePayload() io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + json.NewEncoder(out).Encode(payload) //nolint:errcheck return out } @@ -1639,7 +1655,7 @@ func samplePayloadWithID() io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + json.NewEncoder(out).Encode(payload) //nolint:errcheck return out } @@ -1654,7 +1670,7 @@ func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + json.NewEncoder(out).Encode(payload) //nolint:errcheck return out } @@ -1669,7 +1685,7 @@ func sampleWithPointerPayload(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + json.NewEncoder(out).Encode(payload) //nolint:errcheck return out } @@ -1684,7 +1700,7 @@ func samplePayloadWithNullableAttrs(m map[string]interface{}) io.Reader { } out := bytes.NewBuffer(nil) - json.NewEncoder(out).Encode(payload) + json.NewEncoder(out).Encode(payload) //nolint:errcheck return out } @@ -1757,23 +1773,29 @@ func testModel() *Blog { } } -func samplePayloadWithSideloaded() io.Reader { +func samplePayloadWithSideloaded() (io.Reader, error) { testModel := testModel() out := bytes.NewBuffer(nil) - MarshalPayload(out, testModel) + if err := MarshalPayload(out, testModel); err != nil { + return nil, err + } - return out + return out, nil } -func sampleSerializedEmbeddedTestModel() *Blog { +func sampleSerializedEmbeddedTestModel() (*Blog, error) { out := bytes.NewBuffer(nil) - MarshalOnePayloadEmbedded(out, testModel()) + if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil { + return nil, err + } blog := new(Blog) - UnmarshalPayload(out, blog) + if err := UnmarshalPayload(out, blog); err != nil { + return nil, err + } - return blog + return blog, nil } func TestUnmarshalNestedStructPtr(t *testing.T) { diff --git a/response_test.go b/response_test.go index 509b656d..9b8711e0 100644 --- a/response_test.go +++ b/response_test.go @@ -18,7 +18,9 @@ func TestMarshalPayload(t *testing.T) { // One out1 := bytes.NewBuffer(nil) - MarshalPayload(out1, book) + if err := MarshalPayload(out1, book); err != nil { + t.Fatal(err) + } if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil { t.Fatal(err) @@ -29,7 +31,9 @@ func TestMarshalPayload(t *testing.T) { // Many out2 := bytes.NewBuffer(nil) - MarshalPayload(out2, books) + if err := MarshalPayload(out2, books); err != nil { + t.Fatal(err) + } if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil { t.Fatal(err) @@ -936,7 +940,9 @@ func TestMarshal_Times(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1017,7 +1023,9 @@ func TestNullableRelationship(t *testing.T) { // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1114,7 +1122,9 @@ func TestNullableAttr_Time(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) @@ -1184,7 +1194,9 @@ func TestNullableAttr_Bool(t *testing.T) { } // Use the standard JSON library to traverse the genereated JSON payload. data := map[string]interface{}{} - json.Unmarshal(out.Bytes(), &data) + if err := json.Unmarshal(out.Bytes(), &data); err != nil { + t.Fatal(err) + } if tc.verification != nil { if err := tc.verification(data); err != nil { t.Fatal(err) diff --git a/runtime.go b/runtime.go index db2d9f2f..eaa65094 100644 --- a/runtime.go +++ b/runtime.go @@ -77,7 +77,7 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error { // UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload. func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) { - r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { + r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { //nolint:errcheck elems, err = UnmarshalManyPayload(reader, kind) return err }) From b08a14f4071865a86421d4679ed611f0dad3a994 Mon Sep 17 00:00:00 2001 From: Sonam Tenzin Date: Tue, 6 May 2025 11:51:21 +0530 Subject: [PATCH 11/11] test fix --- request_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request_test.go b/request_test.go index 6cf5163e..2b712d2a 100644 --- a/request_test.go +++ b/request_test.go @@ -1035,7 +1035,7 @@ func Test_choiceStructMapping(t *testing.T) { t.Errorf("expected \"images\" to be the first field, but got %d", imageField.FieldNum) } videoField, ok := result["videos"] - if !ok || videoField.FieldNum != 2 { + if !ok || videoField.FieldNum != 1 { t.Errorf("expected \"videos\" to be the third field, but got %d", videoField.FieldNum) } }