Skip to content

Add header array parameter support#1

Merged
inkel merged 3 commits into
inkel:masterfrom
nicosommi:master
Oct 29, 2017
Merged

Add header array parameter support#1
inkel merged 3 commits into
inkel:masterfrom
nicosommi:master

Conversation

@nicosommi

Copy link
Copy Markdown
Contributor

Also

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

@inkel

inkel commented Oct 28, 2017

Copy link
Copy Markdown
Owner

write header should be done after other headers

How so? 😸

@inkel inkel left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread main.go Outdated
"strings"
)

type stringArray []string

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread main.go Outdated

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. Changed.

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

for _, h := range headerFlags {

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done 😸

@inkel

inkel commented Oct 28, 2017

Copy link
Copy Markdown
Owner

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

@nicosommi

Copy link
Copy Markdown
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).

@inkel inkel left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you!

Comment thread main.go Outdated
}

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

Copy link
Copy Markdown
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 😊

Comment thread main.go Outdated
}

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Updated

@inkel inkel left a comment

Copy link
Copy Markdown
Owner

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