Skip to content

Commit f862356

Browse files
committed
workflow updation
1 parent 81a76b6 commit f862356

File tree

9 files changed

+106
-31
lines changed

9 files changed

+106
-31
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ name: CI
22
on:
33
push:
44
branches:
5-
- main
6-
pull_request:
5+
- build-test
76

87
jobs:
98
unit-test:
@@ -12,11 +11,28 @@ jobs:
1211
matrix:
1312
go: [ '1.21', '1.20', '1.19', '1.18']
1413
steps:
15-
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
14+
- name: Checkout Code
15+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
1616

17-
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
17+
- name: Setup Go
18+
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
1819
with:
1920
go-version: ${{ matrix.go }}
2021

21-
- name: test
22-
run: go test -race . -v
22+
- name: Run golangci-lint
23+
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5
24+
25+
- name: run test and generate coverage report
26+
run: go test -race . -v -coverprofile=coverage.out
27+
28+
- name: Upload coverage report
29+
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
30+
with:
31+
path: coverage.out
32+
name: Coverage-report-${{matrix.go}}
33+
34+
- name: Display coverage report
35+
run: go tool cover -func=coverage.out
36+
37+
- name: Build Go
38+
run: go build ./...

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters:
2+
enable:
3+
- errcheck
4+
disable:
5+
- unused
6+
output_format: colored-line-number

errors_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
4747
var writer io.Writer = buffer
4848

4949
_ = MarshalErrors(writer, testRow.In)
50-
json.Unmarshal(buffer.Bytes(), &output)
50+
if err := json.Unmarshal(buffer.Bytes(), &output); err != nil {
51+
t.Fatalf("failed to unmarshal: %v", err)
52+
}
5153

5254
if !reflect.DeepEqual(output, testRow.Out) {
5355
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)

examples/handler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (h *ExampleHandler) updateBlog(w http.ResponseWriter, r *http.Request) {
6969

7070
blog := new(Blog)
7171

72+
if r.Body == nil {
73+
http.Error(w, "request body cannot be empty", http.StatusBadRequest)
74+
return
75+
}
76+
7277
if err := jsonapiRuntime.UnmarshalPayload(r.Body, blog); err != nil {
7378
http.Error(w, err.Error(), http.StatusInternalServerError)
7479
return

examples/handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) {
112112
handler := &ExampleHandler{}
113113
handler.ServeHTTP(rr, r)
114114

115-
if rr.Code != http.StatusNotFound {
116-
t.Fatal("expected HTTP Status Not Found status error")
115+
if rr.Code != http.StatusBadRequest {
116+
t.Fatalf("expected HTTP Status Bad Request (400), got %d", rr.Code)
117117
}
118118
}

request.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,12 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
453453

454454
buf := bytes.NewBuffer(nil)
455455

456-
json.NewEncoder(buf).Encode(data.Relationships[args[1]])
457-
json.NewDecoder(buf).Decode(relationship)
456+
if err := json.NewEncoder(buf).Encode(data.Relationships[args[1]]); err != nil {
457+
return err
458+
}
459+
if err := json.NewDecoder(buf).Decode(relationship); err != nil {
460+
return err
461+
}
458462

459463
data := relationship.Data
460464

@@ -483,7 +487,9 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
483487

484488
buf := bytes.NewBuffer(nil)
485489
relDataStr := data.Relationships[args[1]]
486-
json.NewEncoder(buf).Encode(relDataStr)
490+
if err := json.NewEncoder(buf).Encode(relDataStr); err != nil {
491+
return err
492+
}
487493

488494
isExplicitNull := false
489495
relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship)

request_test.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"log"
910
"reflect"
1011
"sort"
1112
"strconv"
@@ -405,7 +406,9 @@ func TestUnmarshalNullableRelationshipsNonNullValue(t *testing.T) {
405406
}
406407

407408
outBuf := bytes.NewBuffer(nil)
408-
json.NewEncoder(outBuf).Encode(payload)
409+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
410+
t.Fatalf("failed to encode: %v", err)
411+
}
409412

410413
out := new(WithNullableAttrs)
411414

@@ -442,7 +445,9 @@ func TestUnmarshalNullableRelationshipsExplicitNullValue(t *testing.T) {
442445
}
443446

444447
outBuf := bytes.NewBuffer(nil)
445-
json.NewEncoder(outBuf).Encode(payload)
448+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
449+
t.Fatalf("failed to encode: %v", err)
450+
}
446451

447452
out := new(WithNullableAttrs)
448453

@@ -467,7 +472,9 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) {
467472
}
468473

469474
outBuf := bytes.NewBuffer(nil)
470-
json.NewEncoder(outBuf).Encode(payload)
475+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
476+
log.Fatalf("failed to encode: %v", err)
477+
}
471478

472479
out := new(WithNullableAttrs)
473480

@@ -490,7 +497,9 @@ func TestUnmarshalNullableRelationshipsNoRelationships(t *testing.T) {
490497
}
491498

492499
outBuf := bytes.NewBuffer(nil)
493-
json.NewEncoder(outBuf).Encode(payload)
500+
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
501+
t.Fatalf("failed to encode: %v", err)
502+
}
494503

495504
out := new(WithNullableAttrs)
496505

@@ -1621,7 +1630,9 @@ func samplePayload() io.Reader {
16211630
}
16221631

16231632
out := bytes.NewBuffer(nil)
1624-
json.NewEncoder(out).Encode(payload)
1633+
if err := json.NewEncoder(out).Encode(payload); err != nil {
1634+
log.Printf("failed to encode: %v", err)
1635+
}
16251636

16261637
return out
16271638
}
@@ -1639,7 +1650,9 @@ func samplePayloadWithID() io.Reader {
16391650
}
16401651

16411652
out := bytes.NewBuffer(nil)
1642-
json.NewEncoder(out).Encode(payload)
1653+
if err := json.NewEncoder(out).Encode(payload); err != nil {
1654+
log.Printf("failed to encode: %v", err)
1655+
}
16431656

16441657
return out
16451658
}
@@ -1654,7 +1667,9 @@ func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader {
16541667
}
16551668

16561669
out := bytes.NewBuffer(nil)
1657-
json.NewEncoder(out).Encode(payload)
1670+
if err := json.NewEncoder(out).Encode(payload); err != nil {
1671+
log.Printf("failed to encode: %v", err)
1672+
}
16581673

16591674
return out
16601675
}
@@ -1669,7 +1684,9 @@ func sampleWithPointerPayload(m map[string]interface{}) io.Reader {
16691684
}
16701685

16711686
out := bytes.NewBuffer(nil)
1672-
json.NewEncoder(out).Encode(payload)
1687+
if err := json.NewEncoder(out).Encode(payload); err != nil {
1688+
log.Printf("failed to encode: %v", err)
1689+
}
16731690

16741691
return out
16751692
}
@@ -1684,7 +1701,9 @@ func samplePayloadWithNullableAttrs(m map[string]interface{}) io.Reader {
16841701
}
16851702

16861703
out := bytes.NewBuffer(nil)
1687-
json.NewEncoder(out).Encode(payload)
1704+
if err := json.NewEncoder(out).Encode(payload); err != nil {
1705+
log.Printf("failed to encode: %v", err)
1706+
}
16881707

16891708
return out
16901709
}
@@ -1761,17 +1780,23 @@ func samplePayloadWithSideloaded() io.Reader {
17611780
testModel := testModel()
17621781

17631782
out := bytes.NewBuffer(nil)
1764-
MarshalPayload(out, testModel)
1783+
if err := MarshalPayload(out, testModel); err != nil {
1784+
log.Printf("failed to marshal payload: %v", err)
1785+
}
17651786

17661787
return out
17671788
}
17681789

17691790
func sampleSerializedEmbeddedTestModel() *Blog {
17701791
out := bytes.NewBuffer(nil)
1771-
MarshalOnePayloadEmbedded(out, testModel())
1792+
if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil {
1793+
log.Printf("failed to marshal one payload embedded: %v", err)
1794+
}
17721795

17731796
blog := new(Blog)
1774-
UnmarshalPayload(out, blog)
1797+
if err := UnmarshalPayload(out, blog); err != nil {
1798+
log.Printf("failed to unmarshal payload: %v", err)
1799+
}
17751800

17761801
return blog
17771802
}

response_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func TestMarshalPayload(t *testing.T) {
1818

1919
// One
2020
out1 := bytes.NewBuffer(nil)
21-
MarshalPayload(out1, book)
21+
if err := MarshalPayload(out1, book); err != nil {
22+
t.Fatal(err)
23+
}
2224

2325
if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil {
2426
t.Fatal(err)
@@ -29,7 +31,9 @@ func TestMarshalPayload(t *testing.T) {
2931

3032
// Many
3133
out2 := bytes.NewBuffer(nil)
32-
MarshalPayload(out2, books)
34+
if err := MarshalPayload(out2, books); err != nil {
35+
t.Fatal(err)
36+
}
3337

3438
if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil {
3539
t.Fatal(err)
@@ -936,7 +940,9 @@ func TestMarshal_Times(t *testing.T) {
936940
}
937941
// Use the standard JSON library to traverse the genereated JSON payload.
938942
data := map[string]interface{}{}
939-
json.Unmarshal(out.Bytes(), &data)
943+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
944+
t.Fatal(err)
945+
}
940946
if tc.verification != nil {
941947
if err := tc.verification(data); err != nil {
942948
t.Fatal(err)
@@ -1017,7 +1023,9 @@ func TestNullableRelationship(t *testing.T) {
10171023

10181024
// Use the standard JSON library to traverse the genereated JSON payload.
10191025
data := map[string]interface{}{}
1020-
json.Unmarshal(out.Bytes(), &data)
1026+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
1027+
t.Fatal(err)
1028+
}
10211029
if tc.verification != nil {
10221030
if err := tc.verification(data); err != nil {
10231031
t.Fatal(err)
@@ -1114,7 +1122,9 @@ func TestNullableAttr_Time(t *testing.T) {
11141122
}
11151123
// Use the standard JSON library to traverse the genereated JSON payload.
11161124
data := map[string]interface{}{}
1117-
json.Unmarshal(out.Bytes(), &data)
1125+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
1126+
t.Fatal(err)
1127+
}
11181128
if tc.verification != nil {
11191129
if err := tc.verification(data); err != nil {
11201130
t.Fatal(err)
@@ -1184,7 +1194,9 @@ func TestNullableAttr_Bool(t *testing.T) {
11841194
}
11851195
// Use the standard JSON library to traverse the genereated JSON payload.
11861196
data := map[string]interface{}{}
1187-
json.Unmarshal(out.Bytes(), &data)
1197+
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
1198+
t.Fatal(err)
1199+
}
11881200
if tc.verification != nil {
11891201
if err := tc.verification(data); err != nil {
11901202
t.Fatal(err)

runtime.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
7777

7878
// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
7979
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
80-
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
80+
err = r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
8181
elems, err = UnmarshalManyPayload(reader, kind)
8282
return err
8383
})
84+
if err != nil {
85+
return nil, err
86+
}
8487

8588
return
8689
}

0 commit comments

Comments
 (0)