Adding method to support int64 slice
Adding small tests to test ToInt64Slice
This commit is contained in:
		
							parent
							
								
									506727ec9a
								
							
						
					
					
						commit
						282f7f4414
					
				
							
								
								
									
										5
									
								
								cast.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								cast.go
									
									
									
									
									
								
							@ -76,3 +76,8 @@ func ToIntSlice(i interface{}) []int {
 | 
				
			|||||||
	v, _ := ToIntSliceE(i)
 | 
						v, _ := ToIntSliceE(i)
 | 
				
			||||||
	return v
 | 
						return v
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ToInt64Slice(i interface{}) []int64 {
 | 
				
			||||||
 | 
						v, _ := ToInt64SliceE(i)
 | 
				
			||||||
 | 
						return v
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -137,6 +137,13 @@ func TestSlices(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, []int{1, 3}, ToIntSlice([]interface{}{1.2, 3.2}))
 | 
						assert.Equal(t, []int{1, 3}, ToIntSlice([]interface{}{1.2, 3.2}))
 | 
				
			||||||
	assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
 | 
						assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
 | 
				
			||||||
	assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
 | 
						assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// ToInt64Slice tests
 | 
				
			||||||
 | 
						assert.Equal(t, []int64{1, 3}, ToInt64Slice([]int{1, 3}))
 | 
				
			||||||
 | 
						assert.Equal(t, []int64{1, 3}, ToInt64Slice([]interface{}{1.2, 3.2}))
 | 
				
			||||||
 | 
						assert.Equal(t, []int64{2, 3}, ToInt64Slice([]string{"2", "3"}))
 | 
				
			||||||
 | 
						assert.Equal(t, []int64{2, 3}, ToInt64Slice([2]string{"2", "3"}))
 | 
				
			||||||
 | 
						assert.Equal(t, []int64{2000000, 392233724}, ToInt64Slice([2]string{"2000000", "392233724"}))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestToBool(t *testing.T) {
 | 
					func TestToBool(t *testing.T) {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										31
									
								
								caste.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								caste.go
									
									
									
									
									
								
							@ -469,6 +469,37 @@ func ToIntSliceE(i interface{}) ([]int, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ToIntSliceE casts an empty interface to a []int.
 | 
				
			||||||
 | 
					func ToInt64SliceE(i interface{}) ([]int64, error) {
 | 
				
			||||||
 | 
						jww.TRACE.Println("ToInt64SliceE called on type:", reflect.TypeOf(i))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if i == nil {
 | 
				
			||||||
 | 
							return []int64{}, fmt.Errorf("Unable to Cast %#v to []int", i)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch v := i.(type) {
 | 
				
			||||||
 | 
						case []int64:
 | 
				
			||||||
 | 
							return v, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						kind := reflect.TypeOf(i).Kind()
 | 
				
			||||||
 | 
						switch kind {
 | 
				
			||||||
 | 
						case reflect.Slice, reflect.Array:
 | 
				
			||||||
 | 
							s := reflect.ValueOf(i)
 | 
				
			||||||
 | 
							a := make([]int64, s.Len())
 | 
				
			||||||
 | 
							for j := 0; j < s.Len(); j++ {
 | 
				
			||||||
 | 
								val, err := ToInt64E(s.Index(j).Interface())
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return []int64{}, fmt.Errorf("Unable to Cast %#v to []int64", i)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								a[j] = val
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return a, nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return []int64{}, fmt.Errorf("Unable to Cast %#v to []int64", i)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// StringToDate casts an empty interface to a time.Time.
 | 
					// StringToDate casts an empty interface to a time.Time.
 | 
				
			||||||
func StringToDate(s string) (time.Time, error) {
 | 
					func StringToDate(s string) (time.Time, error) {
 | 
				
			||||||
	return parseDateWith(s, []string{
 | 
						return parseDateWith(s, []string{
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user