Skip to content

Commit

Permalink
👔 up: update some method usage and update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 24, 2023
1 parent 4f76cdb commit 9a0269c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Run unit tests
# run: go test -v -cover ./...
# must add " for profile.cov on Windows OS
run: go test -v -coverprofile="profile.cov" ./...
run: go test -coverprofile="profile.cov" ./...

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
Expand Down
6 changes: 3 additions & 3 deletions data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func TestFormData(t *testing.T) {
is.Equal("inhere", val)

nval, err := d.Set("newKey", "strVal")
is.NoError(err)
is.NoErr(err)
is.Equal("strVal", nval)
is.Equal("strVal", d.String("newKey"))
_, err = d.Set("newInt", 23)
is.NoError(err)
is.NoErr(err)
is.Equal(23, d.Int("newInt"))
_, err = d.Set("invalid", []int{2})
is.Error(err)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestStructData_Set(t *testing.T) {
dump.P(d.fieldNames)

_, err = d.Set("Extra.0.Github", "new url")
is.NoError(err)
is.NoErr(err)
val, ok := d.Get("Extra.0.Github")
is.True(ok)
is.Equal("new url", val)
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func convTypeByBaseKind(srcVal interface{}, srcKind kind, dstType reflect.Kind)
return srcVal.(string), nil
}
case intKind, uintKind:
i64 := mathutil.MustInt64(srcVal)
i64 := mathutil.SafeInt64(srcVal)
switch dstType {
case reflect.Int64:
return i64, nil
Expand Down
6 changes: 4 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"reflect"
"regexp"
"strings"

"github.com/gookit/goutil/reflects"
)

// M is short name for map[string]interface{}
Expand Down Expand Up @@ -308,7 +310,7 @@ func FromJSONBytes(bs []byte) (*MapData, error) {
}

// FromStruct create a Data from struct
func FromStruct(s interface{}) (*StructData, error) {
func FromStruct(s any) (*StructData, error) {
data := &StructData{
ValidateTag: gOpt.ValidateTag,
// init map
Expand All @@ -320,7 +322,7 @@ func FromStruct(s interface{}) (*StructData, error) {
return data, ErrInvalidData
}

val := reflect.Indirect(reflect.ValueOf(s))
val := reflects.Elem(reflect.ValueOf(s))
typ := val.Type()

if val.Kind() != reflect.Struct || typ == timeType {
Expand Down
14 changes: 7 additions & 7 deletions validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestFromRequest_FileForm(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
w, err := mw.CreateFormFile("file", "test.jpg")
if is.NoError(err) {
if is.NoErr(err).IsOk() {
// write file content, this is jpg file start content
_, _ = w.Write([]byte("\xFF\xD8\xFF"))
}
Expand All @@ -426,19 +426,19 @@ func TestFromRequest_FileForm(t *testing.T) {
r.Header.Set("Content-Type", mw.FormDataContentType())
// - create data
d, err := FromRequest(r, defaultMaxMemory)
is.NoError(err)
is.NoErr(err)
fd, ok := d.(*FormData)
is.True(ok)
is.True(fd.HasFile("file"))
is.Equal("inhere", fd.String("name"))
is.Equal(24, fd.Int("age"))

bts, err := fd.FileBytes("file")
is.NoError(err)
is.NoErr(err)
is.Equal([]byte("\xFF\xD8\xFF"), bts)
bts, err = fd.FileBytes("not-exist")
is.Nil(bts)
is.NoError(err)
is.NoErr(err)
is.Equal("", fd.FileMimeType("not-exist"))
is.Equal("image/jpeg", fd.FileMimeType("file"))

Expand All @@ -464,9 +464,9 @@ func TestFromRequest_FileForm(t *testing.T) {
// clear rules
v.Reset()
v.AddRule("file", "mimes")
is.PanicsWithValue("validate: not enough parameters for validator 'mimes'!", func() {
is.PanicsMsg(func() {
v.Validate()
})
}, "validate: not enough parameters for validator 'mimes'!")
}

func TestFromRequest_JSON(t *testing.T) {
Expand Down Expand Up @@ -572,7 +572,7 @@ func TestFromRequest_JSON(t *testing.T) {
v.Validate() // validate
is.True(v.IsOK())
err = v.BindSafeData(user)
is.NoError(err)
is.NoErr(err)
is.Equal("INHERE", user.Name)
} else {
is.Nil(d)
Expand Down

0 comments on commit 9a0269c

Please sign in to comment.