github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/types/pgeo/path.go (about) 1 package pgeo 2 3 import ( 4 "database/sql/driver" 5 "errors" 6 "fmt" 7 "regexp" 8 ) 9 10 var closedPathRegexp = regexp.MustCompile(`^\(\(`) 11 12 // Path is represented by lists of connected points. 13 // Paths can be open, where the first and last points in the list are considered not connected, 14 // or closed, where the first and last points are considered connected. 15 type Path struct { 16 Points []Point 17 Closed bool `json:"closed"` 18 } 19 20 // Value for database 21 func (p Path) Value() (driver.Value, error) { 22 return valuePath(p) 23 } 24 25 // Scan from sql query 26 func (p *Path) Scan(src interface{}) error { 27 return scanPath(p, src) 28 } 29 30 func valuePath(p Path) (driver.Value, error) { 31 var val string 32 if p.Closed { 33 val = fmt.Sprintf(`(%s)`, formatPoints(p.Points)) 34 } else { 35 val = fmt.Sprintf(`[%s]`, formatPoints(p.Points)) 36 } 37 return val, nil 38 } 39 40 func scanPath(p *Path, src interface{}) error { 41 if src == nil { 42 return nil 43 } 44 45 val, err := iToS(src) 46 if err != nil { 47 return err 48 } 49 50 (*p).Points, err = parsePoints(val) 51 if err != nil { 52 return err 53 } 54 55 if len((*p).Points) < 1 { 56 return errors.New("wrong path") 57 } 58 59 (*p).Closed = closedPathRegexp.MatchString(val) 60 61 return nil 62 } 63 64 func randPath(nextInt func() int64) Path { 65 return Path{randPoints(nextInt, 3), newRandNum(nextInt) < 40} 66 } 67 68 // Randomize for sqlboiler 69 func (p *Path) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) { 70 *p = randPath(nextInt) 71 }