Skip to content

Commit

Permalink
improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
joy999 committed Oct 27, 2023
1 parent ce45e75 commit f2d05dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
10 changes: 8 additions & 2 deletions util/gconv/gconv_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,14 @@ func bindVarToStructAttr(structReflectValue reflect.Value, attrName string, valu
} else {
// Try to call custom converter.
// Issue: https://github.com/gogf/gf/issues/3099
if ok, err := callCustomConverter(reflect.ValueOf(value), structFieldValue); ok {
return err
if reflectValue, ok := value.(reflect.Value); ok {
if ok, err := callCustomConverter(reflectValue, structFieldValue); ok {
return err
}
} else {
if ok, err := callCustomConverter(reflect.ValueOf(value), structFieldValue); ok {
return err
}
}

// Special handling for certain types:
Expand Down
20 changes: 12 additions & 8 deletions util/gconv/gconv_z_unit_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
"google.golang.org/protobuf/types/known/timestamppb"
)

func TestConverter_Struct(t *testing.T) {
Expand Down Expand Up @@ -275,12 +274,15 @@ func TestConverter_CustomBasicType_ToStruct(t *testing.T) {
}

// fix: https://github.com/gogf/gf/issues/3099
func TestConverter_CustomTimeType_ToPbTime(t *testing.T) {
func TestConverter_CustomTimeType_ToStruct(t *testing.T) {
type timestamppb struct {
S string
}
type CustomGTime struct {
T *gtime.Time
}
type CustomPbTime struct {
T *timestamppb.Timestamp
T *timestamppb
}
gtest.C(t, func(t *gtest.T) {
var (
Expand All @@ -292,16 +294,18 @@ func TestConverter_CustomTimeType_ToPbTime(t *testing.T) {
err := gconv.Scan(a, &b)
t.AssertNil(err)
t.AssertNE(b, nil)
t.Assert(b.T, nil)
t.Assert(b.T.S, "")
})

gtest.C(t, func(t *gtest.T) {
err := gconv.RegisterConverter(func(in gtime.Time) (*timestamppb.Timestamp, error) {
return timestamppb.New(in.Time.Local()), nil
err := gconv.RegisterConverter(func(in gtime.Time) (*timestamppb, error) {
return &timestamppb{
S: in.Local().Format("Y-m-d"),
}, nil
})
t.AssertNil(err)
err = gconv.RegisterConverter(func(in timestamppb.Timestamp) (*gtime.Time, error) {
return gtime.NewFromTime(in.AsTime().Local()), nil
err = gconv.RegisterConverter(func(in timestamppb) (*gtime.Time, error) {
return gtime.NewFromStr(in.S), nil
})
t.AssertNil(err)
})
Expand Down

0 comments on commit f2d05dd

Please sign in to comment.