forked from volatiletech/sqlboiler
/
point.go
68 lines (53 loc) · 1.21 KB
/
point.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
package pgeo
import (
"database/sql/driver"
)
// Point is the fundamental two-dimensional building block for geometric types.
// X and Y are the respective coordinates, as floating-point numbers
type Point struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}
// Value representation for database
func (p Point) Value() (driver.Value, error) {
return valuePoint(p)
}
// Scan from query
func (p *Point) Scan(src interface{}) error {
return scanPoint(p, src)
}
func valuePoint(p Point) (driver.Value, error) {
return formatPoint(p), nil
}
func scanPoint(p *Point, src interface{}) error {
if src == nil {
*p = NewPoint(0, 0)
return nil
}
val, err := iToS(src)
if err != nil {
return err
}
*p, err = parsePoint(val)
if err != nil {
return err
}
return nil
}
func randPoint(nextInt func() int64) Point {
return Point{newRandNum(nextInt), newRandNum(nextInt)}
}
func randPoints(nextInt func() int64, n int) []Point {
var points = []Point{}
if n <= 0 {
return points
}
for i := 0; i < n; i++ {
points = append(points, randPoint(nextInt))
}
return points
}
// Randomize for sqlboiler
func (p *Point) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
*p = randPoint(nextInt)
}