github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/types/pgeo/line.go (about)

     1  package pgeo
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"errors"
     6  	"fmt"
     7  	"regexp"
     8  )
     9  
    10  var parseLineRegexp = regexp.MustCompile(`^\{(-?[0-9]+(?:\.[0-9]+)?),(-?[0-9]+(?:\.[0-9]+)?),(-?[0-9]+(?:\.[0-9]+)?)\}$`)
    11  
    12  // Line represents a infinite line with the linear equation Ax + By + C = 0, where A and B are not both zero.
    13  type Line struct {
    14  	A float64 `json:"a"`
    15  	B float64 `json:"b"`
    16  	C float64 `json:"c"`
    17  }
    18  
    19  // Value for database
    20  func (l Line) Value() (driver.Value, error) {
    21  	return valueLine(l)
    22  }
    23  
    24  // Scan from sql query
    25  func (l *Line) Scan(src interface{}) error {
    26  	return scanLine(l, src)
    27  }
    28  
    29  func valueLine(l Line) (driver.Value, error) {
    30  	return fmt.Sprintf(`{%[1]v,%[2]v,%[3]v}`, l.A, l.B, l.C), nil
    31  }
    32  
    33  func scanLine(l *Line, src interface{}) error {
    34  	if src == nil {
    35  		*l = NewLine(0, 0, 0)
    36  		return nil
    37  	}
    38  
    39  	val, err := iToS(src)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	pdzs := parseLineRegexp.FindStringSubmatch(val)
    45  	if len(pdzs) != 4 {
    46  		return errors.New("wrong line")
    47  	}
    48  
    49  	nums, err := parseNums(pdzs[1:4])
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	*l = NewLine(nums[0], nums[1], nums[2])
    55  
    56  	return nil
    57  }
    58  
    59  func randLine(nextInt func() int64) Line {
    60  	return Line{newRandNum(nextInt), newRandNum(nextInt), 0}
    61  }
    62  
    63  // Randomize for sqlboiler
    64  func (l *Line) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
    65  	*l = randLine(nextInt)
    66  }