Skip to content

Commit

Permalink
feat: transform carbon when validation bind request or struct (#542)
Browse files Browse the repository at this point in the history
* feat: transform carbon when validation bind request

* chancel the change of carbon/json.go

* cancel
  • Loading branch information
hwbrzzl authored Jul 8, 2024
1 parent ac8d1e5 commit cd40f44
Show file tree
Hide file tree
Showing 2 changed files with 690 additions and 43 deletions.
110 changes: 108 additions & 2 deletions validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cast"

httpvalidate "github.com/goravel/framework/contracts/validation"
"github.com/goravel/framework/support/carbon"
)

func init() {
Expand Down Expand Up @@ -82,8 +83,11 @@ func (v *Validator) Fails() bool {

func (v *Validator) castValue() mapstructure.DecodeHookFunc {
return func(from reflect.Value, to reflect.Value) (any, error) {
var castedValue any
var err error
var (
err error

castedValue = from.Interface()
)

switch to.Kind() {
case reflect.String:
Expand Down Expand Up @@ -138,6 +142,60 @@ func (v *Validator) castValue() mapstructure.DecodeHookFunc {
default:
castedValue, err = cast.ToStringMapE(from.Interface())
}
case reflect.Struct:
switch to.Type() {
case reflect.TypeOf(carbon.Carbon{}):
castedValue = castCarbon(from, nil)
case reflect.TypeOf(carbon.DateTime{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateTime(c)
})
case reflect.TypeOf(carbon.DateTimeMilli{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateTimeMilli(c)
})
case reflect.TypeOf(carbon.DateTimeMicro{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateTimeMicro(c)
})
case reflect.TypeOf(carbon.DateTimeNano{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateTimeNano(c)
})
case reflect.TypeOf(carbon.Date{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDate(c)
})
case reflect.TypeOf(carbon.DateMilli{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateMilli(c)
})
case reflect.TypeOf(carbon.DateMicro{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateMicro(c)
})
case reflect.TypeOf(carbon.DateNano{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewDateNano(c)
})
case reflect.TypeOf(carbon.Timestamp{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewTimestamp(c)
})
case reflect.TypeOf(carbon.TimestampMilli{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewTimestampMilli(c)
})
case reflect.TypeOf(carbon.TimestampMicro{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewTimestampMicro(c)
})
case reflect.TypeOf(carbon.TimestampNano{}):
castedValue = castCarbon(from, func(c carbon.Carbon) any {
return carbon.NewTimestampNano(c)
})
}

default:
castedValue = from.Interface()
}
Expand All @@ -150,3 +208,51 @@ func (v *Validator) castValue() mapstructure.DecodeHookFunc {
return from.Interface(), nil
}
}

func castCarbon(from reflect.Value, transfrom func(carbon carbon.Carbon) any) any {
var c carbon.Carbon

switch len(cast.ToString(from.Interface())) {
case 10:
fromInt64, err := cast.ToInt64E(from.Interface())
if err != nil {
c = carbon.Parse(cast.ToString(from.Interface()))
}
if fromInt64 > 0 {
c = carbon.FromTimestamp(fromInt64)
}
case 13:
fromInt64, err := cast.ToInt64E(from.Interface())
if err != nil {
c = carbon.ParseByFormat(cast.ToString(from.Interface()), "Y-m-d H")
}
if fromInt64 > 0 {
c = carbon.FromTimestampMilli(fromInt64)
}
case 16:
fromInt64, err := cast.ToInt64E(from.Interface())
if err != nil {
c = carbon.ParseByFormat(cast.ToString(from.Interface()), "Y-m-d H:i")
}
if fromInt64 > 0 {
c = carbon.FromTimestampMicro(fromInt64)
}
case 19:
fromInt64, err := cast.ToInt64E(from.Interface())
if err != nil {
c = carbon.Parse(cast.ToString(from.Interface()))
}

if fromInt64 > 0 {
c = carbon.FromTimestampNano(fromInt64)
}
default:
c = carbon.Parse(cast.ToString(from.Interface()))
}

if transfrom != nil {
return transfrom(c)
}

return c
}
Loading

0 comments on commit cd40f44

Please sign in to comment.