yjp 4 ماه پیش
والد
کامیت
dd9f3bf064
2فایلهای تغییر یافته به همراه40 افزوده شده و 8 حذف شده
  1. 32 0
      framework/core/tag/assign/usage.go
  2. 8 8
      test/assign_tag_test.go

+ 32 - 0
framework/core/tag/assign/usage.go

@@ -50,6 +50,38 @@ func defaultCallback(fromFieldElemValue reflect.Value, toFieldElemValue reflect.
 			break
 		}
 
+		if reflectutils.IsSliceValueOf(fromFieldElemValue, reflect.Struct) &&
+			reflectutils.IsTypeTime(fromFieldElemValue.Type().Elem()) &&
+			reflectutils.IsSliceValueOf(toFieldElemValue, reflect.String) {
+			stringSlice := make([]string, 0)
+
+			for i := 0; i < fromFieldElemValue.Len(); i++ {
+				fromString := fromFieldElemValue.Index(i).Interface().(time.Time).Format(tag.TimeLayout)
+				stringSlice = append(stringSlice, trimFromString(fromString, tag))
+			}
+
+			fromAny = stringSlice
+			break
+		}
+
+		if reflectutils.IsSliceValueOf(fromFieldElemValue, reflect.String) &&
+			reflectutils.IsSliceValueOf(toFieldElemValue, reflect.Struct) &&
+			reflectutils.IsTypeTime(toFieldElemValue.Type().Elem()) {
+			timeSlice := make([]time.Time, 0)
+
+			for i := 0; i < fromFieldElemValue.Len(); i++ {
+				timeParsed, err := time.ParseInLocation(tag.TimeLayout, fromFieldElemValue.Index(i).Interface().(string), time.Local)
+				if err != nil {
+					return err
+				}
+
+				timeSlice = append(timeSlice, timeParsed)
+			}
+
+			fromAny = timeSlice
+			break
+		}
+
 		fromAny = fromFieldElemValue.Interface()
 	case reflect.String:
 		fromString := fromFieldElemValue.String()

+ 8 - 8
test/assign_tag_test.go

@@ -784,7 +784,7 @@ type AssignTagToSlice struct {
 	ChanPointerSliceField        []*chan any
 	FuncPointerSliceField        []*func() string
 	FromStringSliceToStringField string
-	FromTimeToStringField        string
+	FromTimeToStringField        []string
 	FromStringToTimeField        []time.Time
 }
 
@@ -881,8 +881,8 @@ func (to AssignTagToSlice) checkFields(t *testing.T, from AssignTagFromSlice) {
 			strings.Join(from.FromStringSliceToStringField, "::"), to.FromStringSliceToStringField))
 	}
 
-	for _, value := range from.FromTimeToStringField {
-		if value.Format(time.DateTime) != to.FromTimeToStringField {
+	for i, value := range from.FromTimeToStringField {
+		if value.Format(time.DateTime) != to.FromTimeToStringField[i] {
 			t.Fatalf("%+v\n", errors.Errorf("FromTimeToStringField not equal: from %v, to %v",
 				value.Format(time.DateTime), to.FromTimeToStringField))
 		}
@@ -946,8 +946,8 @@ func (to AssignTagToSlice) checkNil(t *testing.T) {
 		t.Fatalf("%+v\n", errors.Errorf("FromStringSliceToStringField not zero"))
 	}
 
-	if to.FromTimeToStringField != "" {
-		t.Fatalf("%+v\n", errors.Errorf("FromTimeToStringField not zero"))
+	if to.FromTimeToStringField != nil {
+		t.Fatalf("%+v\n", errors.Errorf("FromTimeToStringField not nil"))
 	}
 }
 
@@ -966,7 +966,7 @@ type AssignTagToSlicePointerField struct {
 	ChanPointerSliceField        *[]*chan any
 	FuncPointerSliceField        *[]*func() string
 	FromStringSliceToStringField *string
-	FromTimeToStringField        *string
+	FromTimeToStringField        *[]string
 	FromStringToTimeField        []time.Time
 }
 
@@ -1063,8 +1063,8 @@ func (to AssignTagToSlicePointerField) checkFields(t *testing.T, from AssignTagF
 			strings.Join(from.FromStringSliceToStringField, "::"), *to.FromStringSliceToStringField))
 	}
 
-	for _, value := range from.FromTimeToStringField {
-		if value.Format(time.DateTime) != *to.FromTimeToStringField {
+	for i, value := range from.FromTimeToStringField {
+		if value.Format(time.DateTime) != (*to.FromTimeToStringField)[i] {
 			t.Fatalf("%+v\n", errors.Errorf("FromTimeToStringField not equal: from %v, to %v",
 				value.Format(time.DateTime), to.FromTimeToStringField))
 		}