Skip to content

Commit

Permalink
fix: multiple interfaces cause the original type to be inaccessible (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
wlynxg authored Sep 4, 2023
1 parent 3da5e5e commit 097b26f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util/gconv/gconv_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func doConvert(in doConvertInput) (convertedValue interface{}) {
}
return Time(in.FromValue)
case "*time.Time":
var v interface{}
var v time.Time
if len(in.Extra) > 0 {
v = Time(in.FromValue, String(in.Extra[0]))
} else {
Expand Down
25 changes: 25 additions & 0 deletions util/gconv/gconv_z_unit_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package gconv_test

import (
"testing"
"time"

"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)
Expand Down Expand Up @@ -43,6 +45,12 @@ func TestConverter_Struct(t *testing.T) {
ValTa tB
}

type tEE struct {
Val1 time.Time `json:"val1"`
Val2 *time.Time `json:"val2"`
Val3 *time.Time `json:"val3"`
}

gtest.C(t, func(t *gtest.T) {
a := &tA{
Val: 1,
Expand Down Expand Up @@ -174,6 +182,23 @@ func TestConverter_Struct(t *testing.T) {
t.Assert(dd.ValTa.Val1, 234)
t.Assert(dd.ValTa.Val2, "abc")
})

// fix: https://github.com/gogf/gf/issues/2665
gtest.C(t, func(t *gtest.T) {
aa := &tEE{}

var tmp = map[string]any{
"val1": "2023-04-15 19:10:00 +0800 CST",
"val2": "2023-04-15 19:10:00 +0800 CST",
"val3": "2006-01-02T15:04:05Z07:00",
}
err := gconv.Struct(tmp, aa)
t.AssertNil(err)
t.AssertNE(aa, nil)
t.Assert(aa.Val1.Local(), gtime.New("2023-04-15 19:10:00 +0800 CST").Local().Time)
t.Assert(aa.Val2.Local(), gtime.New("2023-04-15 19:10:00 +0800 CST").Local().Time)
t.Assert(aa.Val3.Local(), gtime.New("2006-01-02T15:04:05Z07:00").Local().Time)
})
}

func TestConverter_CustomBasicType_ToStruct(t *testing.T) {
Expand Down

0 comments on commit 097b26f

Please sign in to comment.