github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/types/pgeo/circle.go (about) 1 package pgeo 2 3 import ( 4 "database/sql/driver" 5 "errors" 6 "fmt" 7 "strconv" 8 "strings" 9 ) 10 11 // Circle is represented by a center point and radius. 12 type Circle struct { 13 Point 14 Radius float64 `json:"radius"` 15 } 16 17 // Value for the database 18 func (c Circle) Value() (driver.Value, error) { 19 return valueCircle(c) 20 } 21 22 // Scan from sql query 23 func (c *Circle) Scan(src interface{}) error { 24 return scanCircle(c, src) 25 } 26 27 func valueCircle(c Circle) (driver.Value, error) { 28 return fmt.Sprintf(`<%s,%v>`, formatPoint(c.Point), c.Radius), nil 29 } 30 31 func scanCircle(c *Circle, src interface{}) error { 32 if src == nil { 33 *c = NewCircle(Point{}, 0) 34 return nil 35 } 36 37 val, err := iToS(src) 38 if err != nil { 39 return err 40 } 41 42 points, err := parsePoints(val) 43 if err != nil { 44 return err 45 } 46 47 pdzs := strings.Split(val, "),") 48 49 if len(points) != 1 || len(pdzs) != 2 { 50 return errors.New("wrong circle") 51 } 52 53 r, err := strconv.ParseFloat(strings.Trim(pdzs[1], ">"), 64) 54 if err != nil { 55 return err 56 } 57 58 *c = NewCircle(points[0], r) 59 60 return nil 61 } 62 63 func randCircle(nextInt func() int64) Circle { 64 return Circle{randPoint(nextInt), newRandNum(nextInt)} 65 } 66 67 // Randomize for sqlboiler 68 func (c *Circle) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) { 69 *c = randCircle(nextInt) 70 }