package pgtype

import (
	
	
	
	
	
	

	
)

type Polygon struct {
	P      []Vec2
	Status Status
}
Set converts src to dest. src can be nil, string, []float64, and []pgtype.Vec2. If src is string the format must be ((x1,y1),(x2,y2),...,(xn,yn)). Important that there are no spaces in it.
func ( *Polygon) ( interface{}) error {
	if  == nil {
		.Status = Null
		return nil
	}
	 := fmt.Errorf("cannot convert %v to Polygon", )
	var  *Polygon
	switch value := .(type) {
	case string:
		,  = stringToPolygon()
	case []Vec2:
		 = &Polygon{Status: Present, P: }
		 = nil
	case []float64:
		,  = float64ToPolygon()
	default:
		return 
	}
	if  != nil {
		return 
	}
	* = *
	return nil
}

func ( string) (*Polygon, error) {
	 := &Polygon{}
	 := .DecodeText(nil, []byte())
	return , 
}

func ( []float64) (*Polygon, error) {
	 := &Polygon{Status: Null}
	if len() == 0 {
		return , nil
	}
	if len()%2 != 0 {
		.Status = Undefined
		return , fmt.Errorf("invalid length for polygon: %v", len())
	}
	.Status = Present
	.P = make([]Vec2, 0)
	for  := 0;  < len();  += 2 {
		.P = append(.P, Vec2{X: [], Y: [+1]})
	}
	return , nil
}

func ( Polygon) () interface{} {
	switch .Status {
	case Present:
		return 
	case Null:
		return nil
	default:
		return .Status
	}
}

func ( *Polygon) ( interface{}) error {
	return fmt.Errorf("cannot assign %v to %T", , )
}

func ( *Polygon) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = Polygon{Status: Null}
		return nil
	}

	if len() < 7 {
		return fmt.Errorf("invalid length for Polygon: %v", len())
	}

	 := make([]Vec2, 0)

	 := string([2:])

	for {
		 := strings.IndexByte(, ',')
		,  := strconv.ParseFloat([:], 64)
		if  != nil {
			return 
		}

		 = [+1:]
		 = strings.IndexByte(, ')')

		,  := strconv.ParseFloat([:], 64)
		if  != nil {
			return 
		}

		 = append(, Vec2{, })

		if +3 < len() {
			 = [+3:]
		} else {
			break
		}
	}

	* = Polygon{P: , Status: Present}
	return nil
}

func ( *Polygon) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = Polygon{Status: Null}
		return nil
	}

	if len() < 5 {
		return fmt.Errorf("invalid length for Polygon: %v", len())
	}

	 := int(binary.BigEndian.Uint32())
	 := 4

	if 4+*16 != len() {
		return fmt.Errorf("invalid length for Polygon with %d points: %v", , len())
	}

	 := make([]Vec2, )
	for  := 0;  < len(); ++ {
		 := binary.BigEndian.Uint64([:])
		 += 8
		 := binary.BigEndian.Uint64([:])
		 += 8
		[] = Vec2{math.Float64frombits(), math.Float64frombits()}
	}

	* = Polygon{
		P:      ,
		Status: Present,
	}
	return nil
}

func ( Polygon) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	 = append(, '(')

	for ,  := range .P {
		if  > 0 {
			 = append(, ',')
		}
		 = append(, fmt.Sprintf(`(%s,%s)`,
			strconv.FormatFloat(.X, 'f', -1, 64),
			strconv.FormatFloat(.Y, 'f', -1, 64),
		)...)
	}

	return append(, ')'), nil
}

func ( Polygon) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	 = pgio.AppendInt32(, int32(len(.P)))

	for ,  := range .P {
		 = pgio.AppendUint64(, math.Float64bits(.X))
		 = pgio.AppendUint64(, math.Float64bits(.Y))
	}

	return , nil
}
Scan implements the database/sql Scanner interface.
func ( *Polygon) ( interface{}) error {
	if  == nil {
		* = Polygon{Status: Null}
		return nil
	}

	switch src := .(type) {
	case string:
		return .DecodeText(nil, []byte())
	case []byte:
		 := make([]byte, len())
		copy(, )
		return .DecodeText(nil, )
	}

	return fmt.Errorf("cannot scan %T", )
}
Value implements the database/sql/driver Valuer interface.
func ( Polygon) () (driver.Value, error) {
	return EncodeValueText()