chore: refactor httpx.SetOkHandler (#3373)

master
Kevin Wan 1 year ago committed by GitHub
parent 92e5819e91
commit 40e7a4cd07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,8 +15,8 @@ import (
var ( var (
errorHandler func(context.Context, error) (int, any) errorHandler func(context.Context, error) (int, any)
errorLock sync.RWMutex errorLock sync.RWMutex
respHandler func(context.Context, any) any okHandler func(context.Context, any) any
respLock sync.RWMutex okLock sync.RWMutex
) )
// Error writes err into w. // Error writes err into w.
@ -40,9 +40,9 @@ func Ok(w http.ResponseWriter) {
// OkJson writes v into w with 200 OK. // OkJson writes v into w with 200 OK.
func OkJson(w http.ResponseWriter, v any) { func OkJson(w http.ResponseWriter, v any) {
respLock.RLock() okLock.RLock()
handler := respHandler handler := okHandler
respLock.RUnlock() okLock.RUnlock()
if handler != nil { if handler != nil {
v = handler(context.Background(), v) v = handler(context.Background(), v)
} }
@ -51,9 +51,9 @@ func OkJson(w http.ResponseWriter, v any) {
// OkJsonCtx writes v into w with 200 OK. // OkJsonCtx writes v into w with 200 OK.
func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any) { func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any) {
respLock.RLock() okLock.RLock()
handlerCtx := respHandler handlerCtx := okHandler
respLock.RUnlock() okLock.RUnlock()
if handlerCtx != nil { if handlerCtx != nil {
v = handlerCtx(ctx, v) v = handlerCtx(ctx, v)
} }
@ -80,11 +80,11 @@ func SetErrorHandlerCtx(handlerCtx func(context.Context, error) (int, any)) {
errorHandler = handlerCtx errorHandler = handlerCtx
} }
// SetResponseHandler sets the response handler, which is called on calling OkJson and OkJsonCtx. // SetOkHandler sets the response handler, which is called on calling OkJson and OkJsonCtx.
func SetResponseHandler(handler func(context.Context, any) any) { func SetOkHandler(handler func(context.Context, any) any) {
respLock.Lock() okLock.Lock()
defer respLock.Unlock() defer okLock.Unlock()
respHandler = handler okHandler = handler
} }
// WriteJson writes v as json string into w with code. // WriteJson writes v as json string into w with code.

@ -141,16 +141,16 @@ func TestOkJson(t *testing.T) {
}) })
t.Run("with handler", func(t *testing.T) { t.Run("with handler", func(t *testing.T) {
respLock.RLock() okLock.RLock()
prev := respHandler prev := okHandler
respLock.RUnlock() okLock.RUnlock()
t.Cleanup(func() { t.Cleanup(func() {
respLock.Lock() okLock.Lock()
respHandler = prev okHandler = prev
respLock.Unlock() okLock.Unlock()
}) })
SetResponseHandler(func(_ context.Context, v interface{}) any { SetOkHandler(func(_ context.Context, v interface{}) any {
return fmt.Sprintf("hello %s", v.(message).Name) return fmt.Sprintf("hello %s", v.(message).Name)
}) })
w := tracedResponseWriter{ w := tracedResponseWriter{
@ -175,16 +175,16 @@ func TestOkJsonCtx(t *testing.T) {
}) })
t.Run("with handler", func(t *testing.T) { t.Run("with handler", func(t *testing.T) {
respLock.RLock() okLock.RLock()
prev := respHandler prev := okHandler
respLock.RUnlock() okLock.RUnlock()
t.Cleanup(func() { t.Cleanup(func() {
respLock.Lock() okLock.Lock()
respHandler = prev okHandler = prev
respLock.Unlock() okLock.Unlock()
}) })
SetResponseHandler(func(_ context.Context, v interface{}) any { SetOkHandler(func(_ context.Context, v interface{}) any {
return fmt.Sprintf("hello %s", v.(message).Name) return fmt.Sprintf("hello %s", v.(message).Name)
}) })
w := tracedResponseWriter{ w := tracedResponseWriter{

Loading…
Cancel
Save