reflection.go 649 B

1234567891011121314151617181920212223242526272829
  1. package api
  2. import (
  3. "reflect"
  4. "time"
  5. )
  6. // getFieldType extracts type of value
  7. func getFieldType(v reflect.Value) reflect.Type {
  8. t := v.Type()
  9. if t.Kind() == reflect.Ptr {
  10. t = t.Elem()
  11. v = v.Elem()
  12. }
  13. if t.Kind() == reflect.Interface && !v.IsNil() {
  14. t = reflect.ValueOf(v.Interface()).Type()
  15. }
  16. return t
  17. }
  18. // timeType is the exact type for the Time
  19. var timeType = reflect.TypeOf(time.Time{})
  20. // validFieldType validates that t is primitive type or string or interface
  21. func validFieldType(t reflect.Type) bool {
  22. return (t.Kind() > reflect.Invalid && t.Kind() < reflect.Complex64) ||
  23. t.Kind() == reflect.String ||
  24. t == timeType
  25. }