Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct not recognized as http.Flusher after conversion to http.ResponseWriter interface #1600

Open
okkero opened this issue Dec 7, 2023 · 0 comments

Comments

@okkero
Copy link

okkero commented Dec 7, 2023

The following program sample.go triggers an unexpected result

package main

import (
	"fmt"
	"net/http"
	"reflect"
)

type responseWriter struct {
	http.ResponseWriter
}

func (w *responseWriter) Flush() {
	fmt.Println("Flushing...")
	if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
		flusher.Flush()
	}
}

type dummy struct{}

func (d *dummy) Header() http.Header {
	fmt.Println("Dummy Header")
	return nil
}

func (d *dummy) Write(data []byte) (int, error) {
	fmt.Println("Dummy Write ", data)
	return 0, nil
}

func (d *dummy) WriteHeader(responseCode int) {
	fmt.Println("Dummy WriteHeader ", responseCode)
}

func foo(rw http.ResponseWriter) {
	var respCodeOverrideWriter = &responseWriter{ResponseWriter: rw}

	var flusher http.Flusher = respCodeOverrideWriter
	fmt.Println("responseWriter is http.Flusher. We can call Flush manually:")
	flusher.Flush()

	var httpResponseWriter http.ResponseWriter = respCodeOverrideWriter
	_, ok := httpResponseWriter.(http.Flusher)
	fmt.Println("Is responseWriter http.Flusher?", ok)
}

func main() {
	foo(&dummy{})
}

Expected result

responseWriter is http.Flusher. We can call Flush manually:
Flushing...
Is responseWriter http.Flusher? true

Got

responseWriter is http.Flusher. We can call Flush manually:
Flushing...
Is responseWriter http.Flusher? false

Yaegi Version

v0.15.1

Additional Notes

See this issue in the Traefik repo for context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant