diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e652795..4b0b236 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,11 +12,29 @@ 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: Build Go + run: go build ./... - - 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 + diff --git a/errors_test.go b/errors_test.go index ef07359..78d1c4e 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 20adc29..8df53aa 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 { @@ -102,7 +106,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) } diff --git a/models_test.go b/models_test.go index 0961e1b..237f93a 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 ea78c50..79b983a 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 7fa0d3e..2b712d2 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) @@ -1028,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) } } @@ -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 509b656..9b8711e 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 db2d9f2..eaa6509 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 })