| 1 |  |  | package internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	"reflect" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	"strconv" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	"github.com/gotilty/gotil/math" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	"github.com/gotilty/gotil/internal/errs" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | //ToInt returns int(0) with an error if the parameter is unsupported type. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  | //Just works with all primitive types. | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | func ToInt(a interface{}) (int, error) { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	val := reflect.ValueOf(a) | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | 	switch val.Kind() { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 		return fromInt64ToInt(val.Int()) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 	case reflect.Float32, reflect.Float64: | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 		return fromFloat64ToInt(val.Float()) | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 	case reflect.String: | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 		return fromStringToInt(val.String()) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		return fromUint64ToInt(val.Uint()) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 	case reflect.Bool: | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		return fromBoolToInt(val.Bool()) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	default: | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 		return 0, errs.Int16Error(val.Kind().String()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | func fromInt64ToInt(s int64) (int, error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	var maxInt int64 = math.MaxInt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	if s > maxInt { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		return 0, errs.CustomError("The entered number cannot be greater than max int.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	return int(s), nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | func fromFloat64ToInt(s float64) (int, error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	var maxInt int64 = math.MaxInt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	if s > float64(maxInt) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		return 0, errs.CustomError("The entered number cannot be greater than max int.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	return int(s), nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | func fromStringToInt(s string) (int, error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	if s == "" { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		return 0, nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	d, err := strconv.ParseInt(s, 10, 64) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	if err == nil { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		return fromInt64ToInt(d) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	return 0, err | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | func fromUint64ToInt(s uint64) (int, error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	var maxInt int64 = math.MaxInt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	if s > uint64(maxInt) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		return 0, errs.CustomError("The entered number cannot be greater than max int.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	return int(s), nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | func fromBoolToInt(s bool) (int, error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	if s { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return 1, nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		return 0, nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |