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

[Feature] Add Unwrap to custom response writer. #3253

Closed
kvii opened this issue Mar 16, 2024 · 3 comments · Fixed by #3265
Closed

[Feature] Add Unwrap to custom response writer. #3253

kvii opened this issue Mar 16, 2024 · 3 comments · Fixed by #3265
Labels

Comments

@kvii
Copy link
Contributor

kvii commented Mar 16, 2024

Please see the FAQ in our main README.md before submitting your issue.

What problem is the feature used to solve?

Go added a new feature called NewResponseController in go 1.20. Pay attention to the doc blow:

The ResponseWriter should be the original value passed to the [Handler.ServeHTTP] method, or have an Unwrap method returning the original ResponseWriter.

As it said, custom ResponseWriter should add an Unwrap method to return underlaying ResponseWriter.

Currently, there is a custom ResponseWriter in transport/http. And it is used in http.Context:

type responseWriter struct {
code int
w http.ResponseWriter
}
func (w *responseWriter) reset(res http.ResponseWriter) {
w.w = res
w.code = http.StatusOK
}
func (w *responseWriter) Header() http.Header { return w.w.Header() }
func (w *responseWriter) WriteHeader(statusCode int) { w.code = statusCode }
func (w *responseWriter) Write(data []byte) (int, error) {
w.w.WriteHeader(w.code)
return w.w.Write(data)
}
type wrapper struct {
router *Router
req *http.Request
res http.ResponseWriter
w responseWriter
}

Shall we support this feature, so that we can use some code likes below:

_ = http.NewServer(
	http.ResponseEncoder(func(w http.ResponseWriter, r *http.Request, i any) error {
		rc := nt.NewResponseController(w)
		bs, _ := encoding.GetCodec("json").Marshal(i)

		n := len(bs) / 2

		n, _ = w.Write(bs[:n])    // 1. write half of the response
		_ = rc.Flush()            // 2. flush response first
		_, err := w.Write(bs[n:]) // 3. write the rest
		return err
	}),
)

Requirements description of the feature

Add an "Unwrap" method to transport/http.responseWriter.

func (w *responseWriter) Unwrap() http.ResponseWriter { return w.w }

References

@kvii kvii added the feature label Mar 16, 2024
Copy link

dosubot bot commented Mar 16, 2024

It's great to see your interest in adding the Unwrap method to the custom ResponseWriter. Your proposed solution and the requirements description look good. We encourage you to go ahead and create a pull request with your changes. Your contribution will be valuable to the project. Thank you for your willingness to improve the codebase!

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@shenqidebaozi
Copy link
Sponsor Member

It's great to see your interest in adding the Unwrap method to the custom ResponseWriter. Your proposed solution and the requirements description look good. We encourage you to go ahead and create a pull request with your changes. Your contribution will be valuable to the project. Thank you for your willingness to improve the codebase!

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@kvii

Copy link

dosubot bot commented Mar 22, 2024

Sure, I can guide you through the process of creating a pull request to add an "Unwrap" method to the custom ResponseWriter in the Kratos repository. Here are the steps:

  1. Fork the Repository: First, you need to fork the Kratos repository to your own GitHub account.

  2. Clone the Forked Repository: After forking, clone the repository to your local machine using the following commands:

    git clone https://github.com/your-username/kratos.git
    cd kratos
  3. Create a New Branch: It's a good practice to create a new branch for your changes. You can do this using the following command:

    git checkout -b add-unwrap-method
  4. Make Your Changes: Now, you can add the "Unwrap" method to the custom ResponseWriter in the transport/http package. Your proposed code looks correct. Just make sure to add it in the correct file and location.

  5. Commit Your Changes: After making your changes, you need to commit them. You can do this using the following commands:

    git add .
    git commit -m "Add Unwrap method to custom ResponseWriter"
  6. Push Your Changes: Next, you need to push your changes to your forked repository. You can do this using the following command:

    git push origin add-unwrap-method
  7. Create a Pull Request: Finally, you can create a pull request. Go to your forked repository on GitHub, select your branch, and click on "New pull request". Then, fill in the necessary details and submit it.

Please note that after submitting your pull request, it will be reviewed by the maintainers of the Kratos repository. They might ask you to make some changes or improvements to your code. So, be prepared for that.

I hope this helps! If you have any other questions, feel free to ask.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

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

Successfully merging a pull request may close this issue.

2 participants