这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@danielgtaylor
Copy link
Owner

@danielgtaylor danielgtaylor commented Feb 22, 2024

This PR does the following:

  1. Adds a cookie field tag for cookie parameters. Fields can be any of the normal parameter types, plus http.Cookie is supported to get the entire parsed cookie.
  2. A new function huma.ReadCookies(ctx huma.Context, filter string) is provided for reading/parsing cookies from one or more Cookie headers in a router-agnostic way and without needing to modify the huma.Context interface. This function can be used in middleware to read cookie values.
  3. If a response header type supports fmt.Stringer (aka a String() string method), then that is used to serialize the header.
  4. If a response header type is a slice, then huma.Context().AppendHeader(...) is used to write multiple headers with the same name.
  5. As a result of 3 and 4 above, http.Cookie and []http.Cookie are supported for response headers using the Set-Cookie header. They basically just work.
  6. There are examples and docs for each scenario. See the diff for details.

Fixes #227, #253.

@codecov
Copy link

codecov bot commented Feb 22, 2024

Codecov Report

Attention: Patch coverage is 98.27586% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 94.65%. Comparing base (15f15fe) to head (2bbc838).

Files Patch % Lines
huma.go 98.27% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #258      +/-   ##
==========================================
+ Coverage   94.52%   94.65%   +0.13%     
==========================================
  Files          18       18              
  Lines        2665     2694      +29     
==========================================
+ Hits         2519     2550      +31     
+ Misses        107      106       -1     
+ Partials       39       38       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@x-user
Copy link
Contributor

x-user commented Feb 22, 2024

Looks good for me only minor annoyance in needing to extract single cookie from slice when using huma.ReadCookies and filter kinda strange name to get single result.

@danielgtaylor
Copy link
Owner Author

@x-user this was just copied from the standard library, see https://github.com/golang/go/blob/master/src/net/http/cookie.go#L273-L310. I'm open to changing our exported function signature though.

@x-user
Copy link
Contributor

x-user commented Feb 22, 2024

@danielgtaylor i understand this, but standard library uses different public signature

func (r *Request) Cookies() []*Cookie
func (r *Request) Cookie(name string) (*Cookie, error)

@danielgtaylor
Copy link
Owner Author

@x-user take a look at the latest and let me know what you think.

@x-user
Copy link
Contributor

x-user commented Feb 23, 2024

@danielgtaylor, i have no complaints.

@danielgtaylor danielgtaylor merged commit 95a3021 into main Feb 23, 2024
@danielgtaylor danielgtaylor deleted the cookies branch February 23, 2024 17:00
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.

Best practices for managing cookies

3 participants