• 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

0.0
/http/jsonapi/runtime.go
1
// This file is originating from https://github.com/google/jsonapi/
2
// To this file the license conditions of LICENSE apply.
3

4
package jsonapi
5

6
import (
7
        "crypto/rand"
8
        "fmt"
9
        "io"
10
        "reflect"
11
        "time"
12
)
13

14
// Event represents a lifecycle event in the marshaling or unmarshalling
15
// process.
16
type Event int
17

18
const (
19
        // UnmarshalStart is the Event that is sent when deserialization of a payload
20
        // begins.
21
        UnmarshalStart Event = iota
22

23
        // UnmarshalStop is the Event that is sent when deserialization of a payload
24
        // ends.
25
        UnmarshalStop
26

27
        // MarshalStart is the Event that is sent sent when serialization of a payload
28
        // begins.
29
        MarshalStart
30

31
        // MarshalStop is the Event that is sent sent when serialization of a payload
32
        // ends.
33
        MarshalStop
34
)
35

36
// Runtime has the same methods as jsonapi package for serialization and
37
// deserialization but also has a ctx, a map[string]interface{} for storing
38
// state, designed for instrumenting serialization timings.
39
type Runtime struct {
40
        ctx map[string]interface{}
41
}
42

43
// Events is the func type that provides the callback for handling event timings.
44
type Events func(*Runtime, Event, string, time.Duration)
45

46
// Instrumentation is a a global Events variable.  This is the handler for all
47
// timing events.
48
var Instrumentation Events
49

50
// NewRuntime creates a Runtime for use in an application.
51
func NewRuntime() *Runtime { return &Runtime{make(map[string]interface{})} }
×
52

53
// WithValue adds custom state variables to the runtime context.
54
func (r *Runtime) WithValue(key string, value interface{}) *Runtime {
×
55
        r.ctx[key] = value
×
56

×
57
        return r
×
58
}
×
59

60
// Value returns a state variable in the runtime context.
61
func (r *Runtime) Value(key string) interface{} {
×
62
        return r.ctx[key]
×
63
}
×
64

65
// Instrument is deprecated.
66
func (r *Runtime) Instrument(key string) *Runtime {
×
67
        return r.WithValue("instrument", key)
×
68
}
×
69

70
func (r *Runtime) shouldInstrument() bool {
×
71
        return Instrumentation != nil
×
72
}
×
73

74
// UnmarshalPayload has docs in request.go for UnmarshalPayload.
75
func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
×
76
        return r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
×
77
                return UnmarshalPayload(reader, model)
×
78
        })
×
79
}
80

81
// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
82
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elements []interface{}, err error) {
×
83
        err2 := r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
×
84
                elements, err = UnmarshalManyPayload(reader, kind)
×
85
                return err
×
86
        })
×
87

88
        return elements, err2
×
89
}
90

91
// MarshalPayload has docs in response.go for MarshalPayload.
92
func (r *Runtime) MarshalPayload(w io.Writer, model interface{}) error {
×
93
        return r.instrumentCall(MarshalStart, MarshalStop, func() error {
×
94
                return MarshalPayload(w, model)
×
95
        })
×
96
}
97

98
func (r *Runtime) instrumentCall(start Event, stop Event, c func() error) error {
×
99
        if !r.shouldInstrument() {
×
100
                return c()
×
101
        }
×
102

103
        instrumentationGUID, err := newUUID()
×
104
        if err != nil {
×
105
                return err
×
106
        }
×
107

108
        begin := time.Now()
×
NEW
109

×
110
        Instrumentation(r, start, instrumentationGUID, time.Duration(0))
×
111

×
112
        if err := c(); err != nil {
×
113
                return err
×
114
        }
×
115

116
        diff := time.Duration(time.Now().UnixNano() - begin.UnixNano())
×
117
        Instrumentation(r, stop, instrumentationGUID, diff)
×
118

×
119
        return nil
×
120
}
121

122
// citation: http://play.golang.org/p/4FkNSiUDMg
123
func newUUID() (string, error) {
×
124
        uuid := make([]byte, 16)
×
125
        if _, err := io.ReadFull(rand.Reader, uuid); err != nil {
×
126
                return "", err
×
127
        }
×
128
        // variant bits; see section 4.1.1
129
        uuid[8] = uuid[8]&^0xc0 | 0x80
×
130
        // version 4 (pseudo-random); see section 4.1.3
×
131
        uuid[6] = uuid[6]&^0xf0 | 0x40
×
NEW
132

×
UNCOV
133
        return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
×
134
}
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