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