forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lseg.go
55 lines (42 loc) · 1.04 KB
/
lseg.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
package pgeo
import (
"database/sql/driver"
"errors"
"fmt"
"github.com/volatiletech/sqlboiler/randomize"
)
// Lseg is a line segment and is represented by pairs of points that are the endpoints of the segment.
type Lseg [2]Point
// Value for the database
func (l Lseg) Value() (driver.Value, error) {
return valueLseg(l)
}
// Scan from sql query
func (l *Lseg) Scan(src interface{}) error {
return scanLseg(l, src)
}
func valueLseg(l Lseg) (driver.Value, error) {
return fmt.Sprintf(`[%s]`, formatPoints(l[:])), nil
}
func scanLseg(l *Lseg, src interface{}) error {
if src == nil {
*l = NewLseg(Point{}, Point{})
return nil
}
points, err := parsePointsSrc(src)
if err != nil {
return err
}
if len(points) != 2 {
return errors.New("wrong lseg")
}
*l = NewLseg(points[0], points[1])
return nil
}
func randLseg(seed *randomize.Seed) Lseg {
return Lseg([2]Point{randPoint(seed), randPoint(seed)})
}
// Randomize for sqlboiler
func (l *Lseg) Randomize(seed *randomize.Seed, fieldType string, shouldBeNull bool) {
*l = randLseg(seed)
}