github.com/tobgu/qframe@v0.4.0/internal/io/sql/coerce.go (about) 1 package sql 2 3 import ( 4 "reflect" 5 "strconv" 6 7 "github.com/tobgu/qframe/qerrors" 8 ) 9 10 // CoerceFunc returns a function that does an explicit 11 // type cast from one input type and sets an internal 12 // column type. 13 type CoerceFunc func(c *Column) func(t interface{}) error 14 15 // Int64ToBool casts an int64 type into a boolean. This 16 // is useful for casting columns in SQLite which stores 17 // BOOL as INT types natively. 18 func Int64ToBool(c *Column) func(t interface{}) error { 19 return func(t interface{}) error { 20 v, ok := t.(int64) 21 if !ok { 22 return qerrors.New( 23 "Coercion Int64ToBool", "type %s is not int64", reflect.TypeOf(t).Kind()) 24 } 25 c.Bool(v != 0) 26 return nil 27 } 28 } 29 30 func StringToFloat(c *Column) func(t interface{}) error { 31 return func(t interface{}) error { 32 v, ok := t.(string) 33 if !ok { 34 return qerrors.New( 35 "Coercion StringToFloat", "type %s is not float", reflect.TypeOf(t).Kind()) 36 } 37 f, err := strconv.ParseFloat(v, 64) 38 if err != nil { 39 return qerrors.New( 40 "Coercion StringToFloat", "Could not convert %s", v) 41 } 42 c.Float(f) 43 return nil 44 } 45 }