Ejemplo n.º 1
0
func StringToType(src string, dstt reflect.Type) (reflect.Value, error) {
	var srcv reflect.Value
	switch dstt {
	case durationType:
		srcv = reflect.ValueOf(to.Duration(src))
	case timeType:
		// Destination is time.Time
		srcv = reflect.ValueOf(to.Time(src))
	default:
		return StringToKind(src, dstt.Kind())
	}
	return srcv, nil
}
Ejemplo n.º 2
0
// StringToType converts a string value into another type.
func StringToType(src string, dstt reflect.Type) (srcv reflect.Value, err error) {

	// Is destination a pointer?
	if dstt.Kind() == reflect.Ptr {
		if src == "" {
			return
		}
	}

	switch dstt {
	case durationType:
		srcv = reflect.ValueOf(to.Duration(src))
	case timeType:
		srcv = reflect.ValueOf(to.Time(src))
	case ptimeType:
		t := to.Time(src)
		srcv = reflect.ValueOf(&t)
	default:
		return StringToKind(src, dstt.Kind())
	}
	return srcv, nil
}