forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.go
51 lines (40 loc) · 953 Bytes
/
polygon.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
package pgeo
import (
"database/sql/driver"
"errors"
"fmt"
)
// Polygon is represented by lists of points (the vertexes of the polygon).
type Polygon []Point
// Value for database
func (p Polygon) Value() (driver.Value, error) {
return valuePolygon(p)
}
// Scan from sql query
func (p *Polygon) Scan(src interface{}) error {
return scanPolygon(p, src)
}
func valuePolygon(p Polygon) (driver.Value, error) {
return fmt.Sprintf(`(%s)`, formatPoints(p[:])), nil
}
func scanPolygon(p *Polygon, src interface{}) error {
if src == nil {
return nil
}
var err error
*p, err = parsePointsSrc(src)
if err != nil {
return err
}
if len(*p) <= 2 {
return errors.New("wrong polygon")
}
return nil
}
func randPolygon(nextInt func() int64) Polygon {
return Polygon(randPoints(nextInt, 3))
}
// Randomize for sqlboiler
func (p *Polygon) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
*p = randPolygon(nextInt)
}