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