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

fix issue #2371 #2429

Merged
merged 1 commit into from
Feb 8, 2023
Merged
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
16 changes: 15 additions & 1 deletion util/gconv/gconv_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,29 @@ func bindVarToStructAttr(structReflectValue reflect.Value, attrName string, valu
if empty.IsNil(value) {
structFieldValue.Set(reflect.Zero(structFieldValue.Type()))
} else {
// Special handling for certain types:
// - Overwrite the default type converting logic of stdlib for time.Time/*time.Time.
var structFieldTypeName = structFieldValue.Type().String()
switch structFieldTypeName {
case "time.Time", "*time.Time":
doConvertWithReflectValueSet(structFieldValue, doConvertInput{
FromValue: value,
ToTypeName: structFieldTypeName,
ReferValue: structFieldValue,
})
return
}

// Common interface check.
var ok bool
if err, ok = bindVarToReflectValueWithInterfaceCheck(structFieldValue, value); ok {
return err
}

// Default converting.
doConvertWithReflectValueSet(structFieldValue, doConvertInput{
FromValue: value,
ToTypeName: structFieldValue.Type().String(),
ToTypeName: structFieldTypeName,
ReferValue: structFieldValue,
})
}
Expand Down
17 changes: 17 additions & 0 deletions util/gconv/gconv_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gconv_test

import (
"testing"
"time"

"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/encoding/gjson"
Expand Down Expand Up @@ -275,3 +276,19 @@ func Test_Issue2395(t *testing.T) {
t.Assert(gconv.Interfaces(obj), []interface{}{obj})
})
}

// https:/gogf/gf/issues/2371
func Test_Issue2371(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
s = struct {
Time time.Time `json:"time"`
}{}
jsonMap = map[string]interface{}{"time": "2022-12-15 16:11:34"}
)

err := gconv.Struct(jsonMap, &s)
t.AssertNil(err)
t.Assert(s.Time.UTC(), `2022-12-15 08:11:34 +0000 UTC`)
})
}