forked from enosi-rl/gtrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_gtrace.go
108 lines (101 loc) · 2.29 KB
/
main_gtrace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Code generated by gtrace. DO NOT EDIT.
package main
import (
"context"
"net/http"
)
// Compose returns a new PingTrace which has functional fields composed
// both from t and x.
func (t PingTrace) Compose(x PingTrace) (ret PingTrace) {
switch {
case t.OnRequest == nil:
ret.OnRequest = x.OnRequest
case x.OnRequest == nil:
ret.OnRequest = t.OnRequest
default:
h1 := t.OnRequest
h2 := x.OnRequest
ret.OnRequest = func(p PingTraceRequestStart) func(PingTraceRequestDone) {
r1 := h1(p)
r2 := h2(p)
switch {
case r1 == nil:
return r2
case r2 == nil:
return r1
default:
return func(p PingTraceRequestDone) {
r1(p)
r2(p)
}
}
}
}
return ret
}
type pingTraceContextKey struct{}
// WithPingTrace returns context which has associated PingTrace with it.
func WithPingTrace(ctx context.Context, t PingTrace) context.Context {
return context.WithValue(ctx,
pingTraceContextKey{},
ContextPingTrace(ctx).Compose(t),
)
}
// ContextPingTrace returns PingTrace associated with ctx.
// If there is no PingTrace associated with ctx then zero value
// of PingTrace is returned.
func ContextPingTrace(ctx context.Context) PingTrace {
t, _ := ctx.Value(pingTraceContextKey{}).(PingTrace)
return t
}
func (t PingTrace) onRequest(ctx context.Context, p PingTraceRequestStart) func(PingTraceRequestDone) {
c := ContextPingTrace(ctx)
var fn func(PingTraceRequestStart) func(PingTraceRequestDone)
switch {
case t.OnRequest == nil:
fn = c.OnRequest
case c.OnRequest == nil:
fn = t.OnRequest
default:
h1 := t.OnRequest
h2 := c.OnRequest
fn = func(p PingTraceRequestStart) func(PingTraceRequestDone) {
r1 := h1(p)
r2 := h2(p)
switch {
case r1 == nil:
return r2
case r2 == nil:
return r1
default:
return func(p PingTraceRequestDone) {
r1(p)
r2(p)
}
}
}
}
if fn == nil {
return func(PingTraceRequestDone) {
return
}
}
res := fn(p)
if res == nil {
return func(PingTraceRequestDone) {
return
}
}
return res
}
func pingTraceOnRequest(ctx context.Context, t PingTrace, r *http.Request) func(*http.Response, error) {
var p PingTraceRequestStart
p.Request = r
res := t.onRequest(ctx, p)
return func(r *http.Response, e error) {
var p PingTraceRequestDone
p.Response = r
p.Error = e
res(p)
}
}