Skip to content

Conversation

@nicosommi
Copy link
Contributor

Also

  • fix content type issue (write header should be done after other headers)
  • adds docs on readme

@inkel
Copy link
Owner

inkel commented Oct 28, 2017

write header should be done after other headers

How so? 😸

Copy link
Owner

@inkel inkel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! I've left some comments, I'd like for us to discuss them if you are not convinced by them.

main.go Outdated
"strings"
)

type stringArray []string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using a map[string][]string instead? Also the type name, while correct, is too generic, maybe headersSomething?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to MIMEHeader from textproto which is map[string][]string. Also renamed. I put that name before because I thought of this as a generic string array argument parser. But this makes more sense.

main.go Outdated

for _, h := range headerFlags {
ha := strings.Split(h, ": ")
if i := w.Header().Get(ha[0]); i != "" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that whether using a slice or a map, we could always use w.Header().Add. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Changed.

main.go Outdated
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(*code)

for _, h := range headerFlags {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice benefit of defining the type as a map is that here you have for key, values := range …, which are more expressive than h and ha 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 😸

@inkel
Copy link
Owner

inkel commented Oct 28, 2017

On an unrelated note, net/textproto is a very interesting package.

@nicosommi
Copy link
Contributor Author

I used net/textproto for the type.
About the write header being after setting other headers I found this

// Changing the header map after a call to WriteHeader (or
// Write) has no effect unless the modified headers are
// trailers.
https://golang.org/pkg/net/http/#ResponseWriter

So I guess that is not that you can't but it doesn't work out of the box (content type is not working on the original version).

Copy link
Owner

@inkel inkel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

main.go Outdated
}

func (i *headersMap) Set(value string) error {
v := strings.Split(value, ": ")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last change request: use strings.SplitN instead, as strings.Split would fail to set the proper value in Foo: Bar:Baz (it would return a slice of len 3). I forgot about it in the previous review 😊

main.go Outdated
}

func main() {
var headerFlags headersMap = make(map[string][]string)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just realized, why not headers instead of headerFlags?

@nicosommi
Copy link
Contributor Author

Updated

Copy link
Owner

@inkel inkel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@inkel inkel merged commit 1c5e510 into inkel:master Oct 29, 2017
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

Successfully merging this pull request may close these issues.

2 participants