Skip to content

Commit 6aa5c2b

Browse files
authored
Fix/1748 issues gogf#1748 (gogf#1817)
1 parent 8c969b2 commit 6aa5c2b

6 files changed

+46
-5
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## This file contains all available configuration options
22
## with their default values.
3-
#
3+
44
# See https://github.com/golangci/golangci-lint#config-file
55
run:
66
issues-exit-code: 1 #Default

net/ghttp/ghttp_request_param_file.go

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/gogf/gf/v2/errors/gcode"
1717
"github.com/gogf/gf/v2/errors/gerror"
1818
"github.com/gogf/gf/v2/internal/intlog"
19+
"github.com/gogf/gf/v2/internal/json"
1920
"github.com/gogf/gf/v2/os/gfile"
2021
"github.com/gogf/gf/v2/os/gtime"
2122
"github.com/gogf/gf/v2/util/grand"
@@ -27,6 +28,11 @@ type UploadFile struct {
2728
ctx context.Context
2829
}
2930

31+
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
32+
func (f UploadFile) MarshalJSON() ([]byte, error) {
33+
return json.Marshal(f.FileHeader)
34+
}
35+
3036
// UploadFiles is an array type of *UploadFile.
3137
type UploadFiles []*UploadFile
3238

net/ghttp/ghttp_z_unit_feature_request_file_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,39 @@ func Test_Params_Strict_Route_File_Single(t *testing.T) {
216216
t.Assert(gfile.GetContents(dstPath), gfile.GetContents(srcPath))
217217
})
218218
}
219+
220+
func Test_Params_File_Upload_Required(t *testing.T) {
221+
type Req struct {
222+
gmeta.Meta `method:"post" mime:"multipart/form-data"`
223+
File *ghttp.UploadFile `type:"file" v:"required#upload file is required"`
224+
}
225+
type Res struct{}
226+
227+
dstDirPath := gfile.Temp(gtime.TimestampNanoStr())
228+
s := g.Server(guid.S())
229+
s.BindHandler("/upload/required", func(ctx context.Context, req *Req) (res *Res, err error) {
230+
var (
231+
r = g.RequestFromCtx(ctx)
232+
)
233+
234+
file := req.File
235+
if name, err := file.Save(dstDirPath); err == nil {
236+
r.Response.WriteExit(name)
237+
}
238+
r.Response.WriteExit("upload failed")
239+
return
240+
})
241+
s.SetDumpRouterMap(false)
242+
s.Start()
243+
defer s.Shutdown()
244+
time.Sleep(100 * time.Millisecond)
245+
// file is empty
246+
gtest.C(t, func(t *gtest.T) {
247+
client := g.Client()
248+
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
249+
_, err := client.Post(ctx, "/upload/required")
250+
if err != nil {
251+
t.Assert(err.Error(), "upload file is required")
252+
}
253+
})
254+
}

util/gconv/gconv.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Bytes(any interface{}) []byte {
7979
ok = true
8080
bytes = make([]byte, originValueAndKind.OriginValue.Len())
8181
)
82-
for i, _ := range bytes {
82+
for i := range bytes {
8383
int32Value := Int32(originValueAndKind.OriginValue.Index(i).Interface())
8484
if int32Value < 0 || int32Value > math.MaxUint8 {
8585
ok = false
@@ -112,7 +112,7 @@ func Runes(any interface{}) []rune {
112112
}
113113

114114
// String converts `any` to string.
115-
// It's most commonly used converting function.
115+
// It's most commonly used converting function
116116
func String(any interface{}) string {
117117
if any == nil {
118118
return ""

util/gconv/gconv_scan.go

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func Scan(params interface{}, pointer interface{}, mapping ...map[string]string)
5353
pointerType,
5454
)
5555
}
56-
5756
}
5857
// Direct assignment checks!
5958
var (

util/gutil/gutil.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func IsEmpty(value interface{}) bool {
6666
func Keys(mapOrStruct interface{}) (keysOrAttrs []string) {
6767
keysOrAttrs = make([]string, 0)
6868
if m, ok := mapOrStruct.(map[string]interface{}); ok {
69-
for k, _ := range m {
69+
for k := range m {
7070
keysOrAttrs = append(keysOrAttrs, k)
7171
}
7272
return

0 commit comments

Comments
 (0)