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

WIP: refactor(*): return FileHeader in the File method #436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type ValueContainer interface {
// or "multipart/form-data".
Form(key string) ([]string, bool)
// File returns a file reader when "Content-Type" is "multipart/form-data".
File(key string) (multipart.File, bool)
File(key string) (multipart.File, *multipart.FileHeader, bool)
// Body returns a reader to read data from request body.
// The reader only can read once.
Body() (reader io.ReadCloser, contentType string, ok bool)
Expand Down Expand Up @@ -152,9 +152,9 @@ func (c *container) removeEmpties(values []string) ([]string, bool) {
}

// File returns a file reader when "Content-Type" is "multipart/form-data".
func (c *container) File(key string) (multipart.File, bool) {
file, _, err := c.request.FormFile(key)
return file, err == nil
func (c *container) File(key string) (multipart.File, *multipart.FileHeader, bool) {
file, fileHeader, err := c.request.FormFile(key)
return file, fileHeader, err == nil
}

// Body returns a reader to read data from request body.
Expand Down
2 changes: 1 addition & 1 deletion service/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (g *FileParameterGenerator) Validate(name string, defaultValue interface{},
// Generate generates an object by data from value container.
func (g *FileParameterGenerator) Generate(ctx context.Context, vc ValueContainer, consumers []Consumer,
name string, target reflect.Type) (interface{}, error) {
file, ok := vc.File(name)
file, _, ok := vc.File(name)
Copy link
Member

Choose a reason for hiding this comment

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

这样的话我怎么才能拿到这个 header

Copy link
Author

Choose a reason for hiding this comment

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

你想从业务函数那里直接拿到吗,那里是拿不到的,那儿只有一个位置,也不可能对应两个参数

service.HTTPContextFrom(ctx) 转换 context 可以取到这个值

Copy link
Member

Choose a reason for hiding this comment

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

那是不是返回 FileHeader 更好,因为 File 可以通过 FileHeader.Open() 拿到

if !ok {
return nil, nil
}
Expand Down
6 changes: 3 additions & 3 deletions service/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ func (f *file) Close() error {
return nil
}

func (v *vc) File(key string) (multipart.File, bool) {
func (v *vc) File(key string) (multipart.File, *multipart.FileHeader, bool) {
if key == testKey {
return &file{[]byte("test file"), 0}, true
return &file{[]byte("test file"), 0}, &multipart.FileHeader{Filename: "test"}, true
}
return nil, false
return nil, nil, false
}

func (v *vc) Body() (reader io.ReadCloser, contentType string, ok bool) {
Expand Down