Add int64 and float64 type coercion in ToDuration
This commit is contained in:
		
							parent
							
								
									ee815aaf95
								
							
						
					
					
						commit
						6770a32f65
					
				
							
								
								
									
										13
									
								
								cast_test.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								cast_test.go
									
									
									
									
									
								
							@ -6,9 +6,9 @@
 | 
				
			|||||||
package cast
 | 
					package cast
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"html/template"
 | 
						"html/template"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -151,3 +151,12 @@ func TestIndirectPointers(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, ToInt(y), 13)
 | 
						assert.Equal(t, ToInt(y), 13)
 | 
				
			||||||
	assert.Equal(t, ToInt(z), 13)
 | 
						assert.Equal(t, ToInt(z), 13)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestToDuration(t *testing.T) {
 | 
				
			||||||
 | 
						a := time.Second * 5
 | 
				
			||||||
 | 
						ai := int64(a)
 | 
				
			||||||
 | 
						b := time.Second * 5
 | 
				
			||||||
 | 
						bf := float64(b)
 | 
				
			||||||
 | 
						assert.Equal(t, ToDuration(ai), a)
 | 
				
			||||||
 | 
						assert.Equal(t, ToDuration(bf), b)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										6
									
								
								caste.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								caste.go
									
									
									
									
									
								
							@ -43,6 +43,12 @@ func ToDurationE(i interface{}) (d time.Duration, err error) {
 | 
				
			|||||||
	switch s := i.(type) {
 | 
						switch s := i.(type) {
 | 
				
			||||||
	case time.Duration:
 | 
						case time.Duration:
 | 
				
			||||||
		return s, nil
 | 
							return s, nil
 | 
				
			||||||
 | 
						case int64:
 | 
				
			||||||
 | 
							d = time.Duration(s)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						case float64:
 | 
				
			||||||
 | 
							d = time.Duration(s)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
	case string:
 | 
						case string:
 | 
				
			||||||
		d, err = time.ParseDuration(s)
 | 
							d, err = time.ParseDuration(s)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user