Skip to content

Commit

Permalink
mapper 支持 内嵌时间转换
Browse files Browse the repository at this point in the history
  • Loading branch information
harlen committed Dec 28, 2023
1 parent 1d1da5c commit 453d5f1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
9 changes: 6 additions & 3 deletions auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ func setStructVal(anonymous bool, fieldType reflect.StructField, fieldVal reflec
continue
}
objType := reflect.TypeOf(objVal)
if itemType.Kind() == objType.Kind() {
if types.IsTime(itemType) && types.IsDateTime(objType) {
fieldVal.Field(j).Set(parse.ConvertValue(objVal, itemType))
} else if types.IsDateTime(itemType) && types.IsTime(objType) {
fieldVal.Field(j).Set(parse.ConvertValue(objVal, itemType))
} else if itemType.Kind() == objType.Kind() {
fieldVal.Field(j).Set(reflect.ValueOf(objVal))
}
}
Expand All @@ -355,8 +359,7 @@ func structAnalysis(anonymous bool, parentName string, fieldName string, fromStr
}
if types.IsDateTime(itemType) {
objMap[itemName] = fieldVal.Interface()
}
if types.IsGoBasicType(itemType) {
} else if types.IsGoBasicType(itemType) {
if fieldVal.CanInterface() {
objMap[itemName] = fieldVal.Interface()
}
Expand Down
61 changes: 33 additions & 28 deletions test/auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,26 @@ type UserVO2 struct {
Count3 map[int]CountVO2
}
type UserVO3 struct {
Id int64
Name string
Time time.Time
Date dateTime.DateTime
Dec decimal.Decimal
Stat State
Ts time.Duration
Id int64
Name string
Time2 dateTime.DateTime
Time3 time.Time
Time time.Time
Date dateTime.DateTime
Dec decimal.Decimal
Stat State
Ts time.Duration
}
type UserVO4 struct {
Id int64
Name string
Time time.Time
Date dateTime.DateTime
Dec decimal.Decimal
Stat State
Ts time.Duration
Id int64
Name string
Time2 time.Time
Time3 dateTime.DateTime
Time time.Time
Date dateTime.DateTime
Dec decimal.Decimal
Stat State
Ts time.Duration
}

type CountVO struct {
Expand All @@ -93,19 +97,18 @@ type CountVO2 struct {
Count int // 出现的次数
}
type TaskDO struct {
UserVO4
TimeInfo2 dateTime.DateTime
TimeInfo time.Time
Time string
Dec decimal.Decimal
LstType ListType
Client ClientVO
List collections.List[CountVO2]
List2 collections.List[CountVO]
Array []UserVO2
ArrayStr []string
Id int

UserVO3 UserVO4
TimeInfo2 dateTime.DateTime
TimeInfo time.Time
Time string
Dec decimal.Decimal
LstType ListType
Client ClientVO
List collections.List[CountVO2]
List2 collections.List[CountVO]
Array []UserVO2
ArrayStr []string
Id int
Status State
UserId int64
UserName string
Expand All @@ -132,7 +135,7 @@ type TaskDO struct {
}

type TaskDTO struct {
UserVO3
UserVO3 UserVO3
TimeInfo2 time.Time
TimeInfo dateTime.DateTime
Time time.Time
Expand Down Expand Up @@ -235,6 +238,8 @@ func TestDtoToDo(t *testing.T) {
dto.UserVO3.Name = "USER03"
dto.UserVO3.Id = 123123
dto.UserVO3.Time = time.Now()
dto.UserVO3.Time2 = dateTime.Now()
dto.UserVO3.Time3 = time.Now()
dto.UserVO3.Date = dateTime.Now()
dto.UserVO3.Stat = Pending
dto.UserVO3.Dec = decimal.NewFromFloat32(12.22)
Expand Down

0 comments on commit 453d5f1

Please sign in to comment.