• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pace / bricks / 12827718001

17 Jan 2025 10:57AM UTC coverage: 56.909% (-0.3%) from 57.237%
12827718001

Pull #384

github

monstermunchkin
Satisfy linters
Pull Request #384: Extend linting

478 of 946 new or added lines in 109 files covered. (50.53%)

133 existing lines in 53 files now uncovered.

5667 of 9958 relevant lines covered (56.91%)

21.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.78
/http/jsonapi/runtime/marshalling.go
1
// Copyright © 2018 by PACE Telematics GmbH. All rights reserved.
2

3
package runtime
4

5
import (
6
        "errors"
7
        "fmt"
8
        "net"
9
        "net/http"
10
        "reflect"
11

12
        "github.com/pace/bricks/http/jsonapi"
13
        "github.com/pace/bricks/maintenance/log"
14
)
15

16
// Unmarshal processes the request content and fills passed data struct with the
17
// correct jsonapi content. After un-marshaling the struct will be validated with
18
// specified go-validator struct tags.
19
// In case of an error, an jsonapi error message will be directly send to the client.
20
func Unmarshal(w http.ResponseWriter, r *http.Request, data interface{}) bool {
4✔
21
        // don't leak , but error can't be handled
4✔
22
        defer func() {
8✔
23
                _ = r.Body.Close()
4✔
24
        }()
4✔
25

26
        // verify that the client accepts our response
27
        // Note: logically this would be done before marshalling,
28
        //       to prevent stale backend/frontend state we respond before
29
        //                  Additionally, marshal has no access to the request struct
30
        accept := r.Header.Get("Accept")
4✔
31
        if accept != JSONAPIContentType {
5✔
32
                WriteError(w, http.StatusNotAcceptable,
1✔
33
                        fmt.Errorf("request needs to be send with %q header, containing value: %q", "Accept", JSONAPIContentType))
1✔
34
                return false
1✔
35
        }
1✔
36

37
        // if the client didn't send a content type, don't verify
38
        contentType := r.Header.Get("Content-Type")
3✔
39
        if contentType != JSONAPIContentType {
4✔
40
                WriteError(w, http.StatusUnsupportedMediaType,
1✔
41
                        fmt.Errorf("request needs to be send with %q header, containing value: %q", "Content-Type", JSONAPIContentType))
1✔
42
                return false
1✔
43
        }
1✔
44

45
        // parse request
46
        if err := jsonapi.UnmarshalPayload(r.Body, data); err != nil {
3✔
47
                WriteError(w, http.StatusUnprocessableEntity,
1✔
48
                        fmt.Errorf("can't parse content: %w", err))
1✔
49
                return false
1✔
50
        }
1✔
51

52
        // validate request
53
        return ValidateRequest(w, r, data)
1✔
54
}
55

56
// UnmarshalMany processes the request content that has an array of objects and fills passed data struct with the
57
// correct jsonapi content. After un-marshaling the struct will be validated with
58
// specified go-validator struct tags.
59
// In case of an error, an jsonapi error message will be directly send to the client.
60
func UnmarshalMany(w http.ResponseWriter, r *http.Request, t reflect.Type) (bool, []interface{}) {
1✔
61
        // don't leak , but error can't be handled
1✔
62
        defer func() {
2✔
63
                _ = r.Body.Close()
1✔
64
        }()
1✔
65

66
        // verify that the client accepts our response
67
        // Note: logically this would be done before marshalling,
68
        //       to prevent stale backend/frontend state we respond before
69
        //                  Additionally, marshal has no access to the request struct
70
        accept := r.Header.Get("Accept")
1✔
71
        if accept != JSONAPIContentType {
1✔
72
                WriteError(w, http.StatusNotAcceptable,
×
73
                        fmt.Errorf("request needs to be send with %q header, containing value: %q", "Accept", JSONAPIContentType))
×
74
                return false, nil
×
75
        }
×
76

77
        // if the client didn't send a content type, don't verify
78
        contentType := r.Header.Get("Content-Type")
1✔
79
        if contentType != JSONAPIContentType {
1✔
80
                WriteError(w, http.StatusUnsupportedMediaType,
×
81
                        fmt.Errorf("request needs to be send with %q header, containing value: %q", "Content-Type", JSONAPIContentType))
×
82
                return false, nil
×
83
        }
×
84

85
        // parse request
86
        data, err := jsonapi.UnmarshalManyPayload(r.Body, t)
1✔
87
        if err != nil {
1✔
88
                WriteError(w, http.StatusUnprocessableEntity,
×
NEW
89
                        fmt.Errorf("can't parse content: %w", err))
×
90
                return false, nil
×
91
        }
×
92
        // validate request
93
        for _, elem := range data {
3✔
94
                if !ValidateStruct(w, r, elem, "pointer") {
2✔
95
                        return false, nil
×
96
                }
×
97
        }
98

99
        return true, data
1✔
100
}
101

102
// Marshal the given data and writes them into the response writer, sets
103
// the content-type and code as well.
104
func Marshal(w http.ResponseWriter, data interface{}, code int) {
3✔
105
        // write response header
3✔
106
        w.Header().Set("Content-Type", JSONAPIContentType)
3✔
107
        w.WriteHeader(code)
3✔
108

3✔
109
        // write marshaled response body
3✔
110
        if err := jsonapi.MarshalPayload(w, data); err != nil {
5✔
111
                var opErr *net.OpError
2✔
112
                if errors.As(err, &opErr) {
3✔
113
                        log.Errorf("Connection error: %v", err)
1✔
114
                } else {
2✔
115
                        panic(fmt.Errorf("failed to marshal jsonapi response for %#v: %w", data, err))
1✔
116
                }
117
        }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc