package pgtype
import (
)
type LtreeCodec struct{}
func ( LtreeCodec) ( int16) bool {
return == TextFormatCode || == BinaryFormatCode
}
func ( LtreeCodec) () int16 {
return TextFormatCode
}
func ( LtreeCodec) ( *Map, uint32, int16, any) EncodePlan {
switch {
case TextFormatCode:
return (TextCodec)().PlanEncode(, , , )
case BinaryFormatCode:
switch .(type) {
case string:
return encodeLtreeCodecBinaryString{}
case []byte:
return encodeLtreeCodecBinaryByteSlice{}
case TextValuer:
return encodeLtreeCodecBinaryTextValuer{}
}
}
return nil
}
type encodeLtreeCodecBinaryString struct{}
func (encodeLtreeCodecBinaryString) ( any, []byte) ( []byte, error) {
:= .(string)
= append(, 1)
return append(, ...), nil
}
type encodeLtreeCodecBinaryByteSlice struct{}
func (encodeLtreeCodecBinaryByteSlice) ( any, []byte) ( []byte, error) {
:= .([]byte)
= append(, 1)
return append(, ...), nil
}
type encodeLtreeCodecBinaryTextValuer struct{}
func (encodeLtreeCodecBinaryTextValuer) ( any, []byte) ( []byte, error) {
, := .(TextValuer).TextValue()
if != nil {
return nil,
}
if !.Valid {
return nil, nil
}
= append(, 1)
return append(, .String...), nil
}
func ( LtreeCodec) ( *Map, uint32, int16, any) ScanPlan {
switch {
case TextFormatCode:
return (TextCodec)().PlanScan(, , , )
case BinaryFormatCode:
switch .(type) {
case *string:
return scanPlanBinaryLtreeToString{}
case TextScanner:
return scanPlanBinaryLtreeToTextScanner{}
}
}
return nil
}
type scanPlanBinaryLtreeToString struct{}
func (scanPlanBinaryLtreeToString) ( []byte, any) error {
:= [0]
if != 1 {
return fmt.Errorf("unsupported ltree version %d", )
}
:= ().(*string)
* = string([1:])
return nil
}
type scanPlanBinaryLtreeToTextScanner struct{}
func (scanPlanBinaryLtreeToTextScanner) ( []byte, any) error {
:= [0]
if != 1 {
return fmt.Errorf("unsupported ltree version %d", )
}
:= ().(TextScanner)
return .ScanText(Text{String: string([1:]), Valid: true})
}
func ( LtreeCodec) ( *Map, uint32, int16, []byte) (driver.Value, error) {
return (TextCodec)().DecodeDatabaseSQLValue(, , , )
}
func ( LtreeCodec) ( *Map, uint32, int16, []byte) (any, error) {
return (TextCodec)().DecodeValue(, , , )
}