-
Notifications
You must be signed in to change notification settings - Fork 2
Add header array parameter support #1
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
Conversation
How so? 😸 |
inkel
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 != "" { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done 😸
|
On an unrelated note, |
|
I used net/textproto for the type.
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
left a comment
There was a problem hiding this 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, ": ") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
|
Updated |
inkel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Also