This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
103 lines (81 loc) · 1.84 KB
/
general.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package pgeo
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/volatiletech/sqlboiler/randomize"
)
func iToS(src interface{}) (string, error) {
var val string
var err error
switch src.(type) {
case string:
val = src.(string)
case []byte:
val = string(src.([]byte))
default:
err = errors.New(fmt.Sprintf("incompatible type %v", reflect.ValueOf(src).Kind().String()))
}
return val, err
}
func parseNums(s []string) ([]float64, error) {
var pts = []float64{}
for _, p := range s {
pt, err := strconv.ParseFloat(p, 64)
if err != nil {
return pts, err
}
pts = append(pts, pt)
}
return pts, nil
}
func formatPoint(point Point) string {
return fmt.Sprintf(`(%v,%v)`, point.X, point.Y)
}
func formatPoints(points []Point) string {
var pts = []string{}
for _, p := range points {
pts = append(pts, formatPoint(p))
}
return strings.Join(pts, ",")
}
func parsePoint(pt string) (Point, error) {
var point = Point{}
var err error
pdzs := regexp.MustCompile(`^\((-?[0-9]+(?:\.[0-9]+)?),(-?[0-9]+(?:\.[0-9]+)?)\)$`).FindStringSubmatch(pt)
if len(pdzs) != 3 {
return point, errors.New("wrong point")
}
nums, err := parseNums(pdzs[1:3])
if err != nil {
return point, err
}
point.X = nums[0]
point.Y = nums[1]
return point, nil
}
func parsePoints(pts string) ([]Point, error) {
var points = []Point{}
pdzs := regexp.MustCompile(`\((?:-?[0-9]+(?:\.[0-9]+)?),(?:-?[0-9]+(?:\.[0-9]+)?)\)`).FindAllString(pts, -1)
for _, pt := range pdzs {
point, err := parsePoint(pt)
if err != nil {
return points, err
}
points = append(points, point)
}
return points, nil
}
func parsePointsSrc(src interface{}) ([]Point, error) {
val, err := iToS(src)
if err != nil {
return []Point{}, err
}
return parsePoints(val)
}
func newRandNum(s *randomize.Seed) float64 {
return float64(s.NextInt())
}