utils.go 768 B

1234567891011121314151617181920212223242526272829303132
  1. package formbinder
  2. import (
  3. "encoding"
  4. "reflect"
  5. "time"
  6. )
  7. var (
  8. typeTime = reflect.TypeOf(time.Time{})
  9. typeTimePtr = reflect.TypeOf(&time.Time{})
  10. )
  11. // unmarshalText returns a boolean and error. The boolean is true if the
  12. // value implements TextUnmarshaler, and false if not.
  13. func checkUnmarshalText(v reflect.Value, val string) (bool, error) {
  14. // check if implements the interface
  15. m, ok := v.Interface().(encoding.TextUnmarshaler)
  16. addr := v.CanAddr()
  17. if !ok && !addr {
  18. return false, nil
  19. } else if addr {
  20. return checkUnmarshalText(v.Addr(), val)
  21. }
  22. // skip if the type is time.Time
  23. n := v.Type()
  24. if n.ConvertibleTo(typeTime) || n.ConvertibleTo(typeTimePtr) {
  25. return false, nil
  26. }
  27. // return result
  28. return true, m.UnmarshalText([]byte(val))
  29. }